Added downloading posts into json files, filtering by year.

This commit is contained in:
Krzysztof Płaczek
2025-07-31 16:37:16 +02:00
parent ce08be27a7
commit 01c3f59085
3 changed files with 51 additions and 45 deletions

View File

@@ -8,13 +8,16 @@ Alpine.plugin(persist)
Alpine.data('bookmeterList', function () {
return {
items: [],
items: {},
filteredItems: [],
username: '',
publisher: '',
title: '',
author: '',
nameFilter: '',
years: [2023, 2024, 2025],
year: this.$persist(['2025']).as('year-filter'),
orderBy: [],
advancedSearch: this.$persist(0).as('advanced-search'),
async init() {
this.$watch('nameFilter', () => this.updateFilter());
@@ -22,22 +25,29 @@ Alpine.data('bookmeterList', function () {
this.$watch('title', () => this.updateFilter());
this.$watch('author', () => this.updateFilter());
this.$watch('publisher', () => this.updateFilter());
this.$watch('year', () => this.updateFilter());
let qp = new URLSearchParams(window.location.search);
if(qp.get('filter')) this.nameFilter = qp.get('filter');
const res = await fetch('/posts.json')
this.items = await res.json();
const [data1, data2, data3] = await Promise.all([
fetch('/posts_2025.json').then(res => res.json()),
fetch('/posts_2024.json').then(res => res.json()),
fetch('/posts_2023.json').then(res => res.json())
])
this.items = [...data1, ...data2, ...data3]
this.updateFilter();
},
stats(){
return {
'pages_total': this.filteredItems.reduce((a, b) => a + b.book.pages, 0) || '-',
'likes_total': this.filteredItems.reduce((a, b) => a + b.likes, 0) || '-',
'pages_avg': Math.round(this.filteredItems.reduce((a, b) => a + b.book.pages, 0)/this.filteredItems.length) || '-',
'likes_avg': Math.round(this.filteredItems.reduce((a, b) => a + b.likes, 0)/this.filteredItems.length) || '-',
'rating_avg': (this.filteredItems.reduce((a, b) => a + b.book.rating, 0)/this.filteredItems.length).toPrecision(2) || '-',
'formats': this.filteredItems.reduce((acc, item) => {
pages_total: this.filteredItems.reduce((a, b) => a + b.book.pages, 0) || '-',
likes_total: this.filteredItems.reduce((a, b) => a + b.likes, 0) || '-',
pages_avg: Math.round(this.filteredItems.reduce((a, b) => a + b.book.pages, 0)/this.filteredItems.length) || '-',
likes_avg: Math.round(this.filteredItems.reduce((a, b) => a + b.likes, 0)/this.filteredItems.length) || '-',
rating_avg: (this.filteredItems.reduce((a, b) => a + b.book.rating, 0)/this.filteredItems.length).toPrecision(2) || '-',
formats: this.filteredItems.reduce((acc, item) => {
let val = item.book.format.toLowerCase() || 'brak danych';
acc[val] = acc[val] === undefined ? 1 : acc[val] += 1;
return acc;
@@ -46,8 +56,9 @@ Alpine.data('bookmeterList', function () {
},
updateFilter(){
let filter = this.nameFilter.toLowerCase();
this.filteredItems = this.items.filter(i => {
return this.year.includes(i.created_at.substring(0,4))
}).filter(i => {
if(this.advancedSearch) {
return (
(!this.username || i.username.toLowerCase().includes(this.username.toLowerCase())) &&