feat(reading/index.php.liquid): update reading progress on page load
This commit is contained in:
parent
320f5e53db
commit
fc32f457c0
7 changed files with 75 additions and 9 deletions
41
app/Classes/BookProgressHandler.php
Normal file
41
app/Classes/BookProgressHandler.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class BookProgressHandler extends BaseHandler
|
||||
{
|
||||
protected int $cacheTTL = 300;
|
||||
|
||||
public function getAllProgress(): array
|
||||
{
|
||||
try {
|
||||
$cacheKey = 'all_books_progress';
|
||||
|
||||
if ($this->cache) {
|
||||
$cached = $this->cache->get($cacheKey);
|
||||
if ($cached !== false) {
|
||||
return json_decode($cached, true) ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
$response = $this->makeRequest('GET', 'optimized_books_progress?select=isbn,progress');
|
||||
$result = [];
|
||||
|
||||
foreach ($response as $row) {
|
||||
if (!empty($row['isbn'])) {
|
||||
$result[$row['isbn']] = (int) $row['progress'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->cache) {
|
||||
$this->cache->set($cacheKey, json_encode($result), $this->cacheTTL);
|
||||
}
|
||||
|
||||
return $result;
|
||||
} catch (\Throwable $e) {
|
||||
error_log('BookProgressHandler::getAllProgress error: ' . $e->getMessage());
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "coryd.dev",
|
||||
"version": "10.5.6",
|
||||
"version": "10.6.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "coryd.dev",
|
||||
"version": "10.5.6",
|
||||
"version": "10.6.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"minisearch": "^7.1.2",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "coryd.dev",
|
||||
"version": "10.5.6",
|
||||
"version": "10.6.0",
|
||||
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
||||
"type": "module",
|
||||
"engines": {
|
||||
|
|
|
@ -17,6 +17,7 @@ SELECT
|
|||
CONCAT(globals.cdn_url, '/', df.filename_disk) AS image,
|
||||
b.favorite,
|
||||
b.tattoo,
|
||||
b.isbn,
|
||||
(
|
||||
SELECT
|
||||
array_agg(t.name)
|
8
queries/views/media/books/books_progress.sql
Normal file
8
queries/views/media/books/books_progress.sql
Normal file
|
@ -0,0 +1,8 @@
|
|||
CREATE OR REPLACE VIEW optimized_books_progress AS
|
||||
SELECT
|
||||
isbn,
|
||||
progress
|
||||
FROM
|
||||
optimized_books
|
||||
WHERE
|
||||
status = 'started';
|
|
@ -1,9 +1,22 @@
|
|||
---
|
||||
title: Reading
|
||||
description: Here's what I'm reading at the moment.
|
||||
permalink: "/reading/index.html"
|
||||
permalink: "/reading/index.php"
|
||||
schema: books
|
||||
eleventyComputed:
|
||||
page:
|
||||
activeUrl: "/reading"
|
||||
---
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../bootstrap.php';
|
||||
|
||||
use App\Classes\BookProgressHandler;
|
||||
|
||||
$progressHandler = new BookProgressHandler();
|
||||
$progressData = $progressHandler->getAllProgress();
|
||||
|
||||
?>
|
||||
{%- assign currentYear = 'now' | date: "%Y" -%}
|
||||
{%- assign bookData = books.all | filterBooksByStatus: 'started' | reverse -%}
|
||||
{%- assign currentBookCount = books.currentYear | size -%}
|
||||
|
@ -45,14 +58,17 @@ schema: books
|
|||
{% if book.author %}
|
||||
<span class="sub-meta">By {{ book.author }}</span>
|
||||
{% endif %}
|
||||
{% if book.progress %}
|
||||
{% render "static/media/progress-bar.liquid",
|
||||
percentage:book.progress
|
||||
%}
|
||||
{% endif %}
|
||||
<?php
|
||||
$isbn = '{{ book.isbn | escape }}';
|
||||
$progress = $progressData[$isbn] ?? null;
|
||||
if (isset($progress)):
|
||||
?>
|
||||
<progress value="<?= htmlspecialchars($progress) ?>" max="100"><?= htmlspecialchars($progress) ?>%</progress>
|
||||
<?php endif; ?>
|
||||
{% if book.description %}
|
||||
<div class="description">{{ book.description | normalize_whitespace | markdown | htmlTruncate }}</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</article>
|
||||
{% endfor %}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue