feat(reading/index.php.liquid): update reading progress on page load

This commit is contained in:
Cory Dransfeldt 2025-06-15 13:35:43 -07:00
parent 320f5e53db
commit fc32f457c0
No known key found for this signature in database
7 changed files with 75 additions and 9 deletions

View 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
View file

@ -1,12 +1,12 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "10.5.6", "version": "10.6.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "coryd.dev", "name": "coryd.dev",
"version": "10.5.6", "version": "10.6.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"minisearch": "^7.1.2", "minisearch": "^7.1.2",

View file

@ -1,6 +1,6 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "10.5.6", "version": "10.6.0",
"description": "The source for my personal site. Built using 11ty (and other tools).", "description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module", "type": "module",
"engines": { "engines": {

View file

@ -17,6 +17,7 @@ SELECT
CONCAT(globals.cdn_url, '/', df.filename_disk) AS image, CONCAT(globals.cdn_url, '/', df.filename_disk) AS image,
b.favorite, b.favorite,
b.tattoo, b.tattoo,
b.isbn,
( (
SELECT SELECT
array_agg(t.name) array_agg(t.name)

View file

@ -0,0 +1,8 @@
CREATE OR REPLACE VIEW optimized_books_progress AS
SELECT
isbn,
progress
FROM
optimized_books
WHERE
status = 'started';

View file

@ -1,9 +1,22 @@
--- ---
title: Reading title: Reading
description: Here's what I'm reading at the moment. description: Here's what I'm reading at the moment.
permalink: "/reading/index.html" permalink: "/reading/index.php"
schema: books 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 currentYear = 'now' | date: "%Y" -%}
{%- assign bookData = books.all | filterBooksByStatus: 'started' | reverse -%} {%- assign bookData = books.all | filterBooksByStatus: 'started' | reverse -%}
{%- assign currentBookCount = books.currentYear | size -%} {%- assign currentBookCount = books.currentYear | size -%}
@ -45,14 +58,17 @@ schema: books
{% if book.author %} {% if book.author %}
<span class="sub-meta">By {{ book.author }}</span> <span class="sub-meta">By {{ book.author }}</span>
{% endif %} {% endif %}
{% if book.progress %} <?php
{% render "static/media/progress-bar.liquid", $isbn = '{{ book.isbn | escape }}';
percentage:book.progress $progress = $progressData[$isbn] ?? null;
%} if (isset($progress)):
{% endif %} ?>
<progress value="<?= htmlspecialchars($progress) ?>" max="100"><?= htmlspecialchars($progress) ?>%</progress>
<?php endif; ?>
{% if book.description %} {% if book.description %}
<div class="description">{{ book.description | normalize_whitespace | markdown | htmlTruncate }}</div> <div class="description">{{ book.description | normalize_whitespace | markdown | htmlTruncate }}</div>
{% endif %} {% endif %}
</div> </div>
</article> </article>
{% endfor %} {% endfor %}