From 7478a1782233a3a4b428ecec09aaea912fd81a0d Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Wed, 14 Feb 2024 15:03:13 -0800 Subject: [PATCH] sort author documents by date --- .../author/[author]/findDocumentsByAuthor.ts | 25 +++++++++---------- src/app/db/data.ts | 7 +++++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/app/author/[author]/findDocumentsByAuthor.ts b/src/app/author/[author]/findDocumentsByAuthor.ts index de7454b..5a2d904 100644 --- a/src/app/author/[author]/findDocumentsByAuthor.ts +++ b/src/app/author/[author]/findDocumentsByAuthor.ts @@ -8,21 +8,20 @@ interface DocumentWithSlug { doc: Document } -export default function findDocumentsByAuthor( +export default function findDocumentsByAuthorSorted( authorId: string ): DocumentWithSlug[] { - // Initialize an empty array to store the results - const result: DocumentWithSlug[] = [] + // Filter documents by author + const filteredDocuments = Object.entries(documents) + .filter(([_, doc]) => doc.manifest.authors.includes(authorId)) + .map(([slug, doc]) => ({ slug, doc })) - // Iterate over the documents object entries - for (const [slug, doc] of Object.entries(documents)) { - // Check if the authorId is present in the document's authors array - if (doc.manifest.authors.includes(authorId)) { - // If present, push the document along with its slug to the results array - result.push({ slug, doc }) - } - } + // Sort the filtered documents by the latest date in descending order + const sortedDocuments = filteredDocuments.sort((a, b) => { + const latestDateA = a.doc.manifest.dates[a.doc.manifest.dates.length - 1] + const latestDateB = b.doc.manifest.dates[b.doc.manifest.dates.length - 1] + return latestDateB - latestDateA // For descending order + }) - // Return the results array - return result + return sortedDocuments } diff --git a/src/app/db/data.ts b/src/app/db/data.ts index 8f5c6c4..a5a1c73 100644 --- a/src/app/db/data.ts +++ b/src/app/db/data.ts @@ -532,7 +532,7 @@ export const authors: Readonly<{ [key: string]: Author }> = { }, affiliation: ['Wage Slave@primos', 'Student@srvhs'], image: '/img/profiles/cbordalo.jpg', - nationality: ['stateless', 'unknown'], + nationality: ['phl', 'usa'], }, } @@ -763,6 +763,11 @@ export const nationalities: Readonly<{ [key: string]: Nationality }> = { demonym: 'Israeli', flag: 'https://upload.wikimedia.org/wikipedia/commons/5/5c/Israel_flag_300.png?20050629023821', }, + phl: { + name: 'Republic of the Philippines', + demonym: 'Filipino', + flag: 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Flag_of_the_Philippines.svg/2880px-Flag_of_the_Philippines.svg.png', + }, unknown: { name: 'Undetermined', demonym: 'Undetermined',