Show related affiliation documents
This commit is contained in:
parent
8501d13827
commit
17361b092a
3 changed files with 81 additions and 2 deletions
|
@ -0,0 +1,61 @@
|
||||||
|
import { Document, documents, authors } from '@/app/db/data'
|
||||||
|
|
||||||
|
// Assuming the types Document, DocumentStatus, and reviewer are defined as provided in your question
|
||||||
|
|
||||||
|
// Interface for the return output
|
||||||
|
interface DocumentWithSlug {
|
||||||
|
slug: string
|
||||||
|
doc: Document
|
||||||
|
}
|
||||||
|
|
||||||
|
const findDocumentsByAuthor = (
|
||||||
|
authorId: string
|
||||||
|
): DocumentWithSlug[] => {
|
||||||
|
// Filter documents by author
|
||||||
|
return Object.entries(documents)
|
||||||
|
.filter(([_, doc]) => doc.manifest.authors.includes(authorId))
|
||||||
|
.map(([slug, doc]) => ({ slug, doc }))
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkAffiliation = (affiliationStr: string[], affiliationShort: string) => {
|
||||||
|
for (const affiliation of affiliationStr) {
|
||||||
|
if (affiliation.split('@')[1] === affiliationShort) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function findDocumentsByAffiliationSorted(
|
||||||
|
affiliationShort: string
|
||||||
|
): DocumentWithSlug[] {
|
||||||
|
// Find authors with the given affiliation
|
||||||
|
const results: DocumentWithSlug[] = []
|
||||||
|
for (const [key, value] of Object.entries(authors)) {
|
||||||
|
if (
|
||||||
|
checkAffiliation(value.affiliation, affiliationShort)
|
||||||
|
|| (
|
||||||
|
value.formerAffiliations
|
||||||
|
&& checkAffiliation(value.formerAffiliations, affiliationShort)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
// Check if document is already in results
|
||||||
|
const additions = findDocumentsByAuthor(key)
|
||||||
|
for (const addition of additions) {
|
||||||
|
const existing = results.find((doc) => doc.slug === addition.slug)
|
||||||
|
if (!existing) {
|
||||||
|
results.push(addition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort the filtered documents by the latest date in descending order
|
||||||
|
const sortedDocuments = results.toSorted((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 sortedDocuments
|
||||||
|
}
|
|
@ -1,6 +1,9 @@
|
||||||
import { affiliations } from '@/app/db/data'
|
import { affiliations } from '@/app/db/data'
|
||||||
import { notFound } from 'next/navigation'
|
import { notFound } from 'next/navigation'
|
||||||
import { Zilla_Slab } from 'next/font/google'
|
import { Zilla_Slab } from 'next/font/google'
|
||||||
|
import findDocumentsByAffiliation from './findDocumentsByAffiliation'
|
||||||
|
import { Fragment } from 'react'
|
||||||
|
import DocumentCard from '@/app/components/DocumentCard'
|
||||||
|
|
||||||
const zillaSlab = Zilla_Slab({ subsets: ['latin'], weight: ['500'] })
|
const zillaSlab = Zilla_Slab({ subsets: ['latin'], weight: ['500'] })
|
||||||
|
|
||||||
|
@ -18,12 +21,14 @@ export default function Page({
|
||||||
notFound()
|
notFound()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const affiliationDocuments = findDocumentsByAffiliation(shortName)
|
||||||
|
|
||||||
const Description = () => {
|
const Description = () => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{description.split('[linebreak]').map((d, i) => (
|
{description.split('[linebreak]').map((d, i) => (
|
||||||
<>
|
<>
|
||||||
<div key={i} className='text-lg sm:text-md font-serif'>
|
<div className='text-lg sm:text-md font-serif'>
|
||||||
{d}
|
{d}
|
||||||
</div>
|
</div>
|
||||||
<br className='m-1' />
|
<br className='m-1' />
|
||||||
|
@ -56,6 +61,19 @@ export default function Page({
|
||||||
<br />
|
<br />
|
||||||
<Description />
|
<Description />
|
||||||
</div>
|
</div>
|
||||||
|
{affiliationDocuments.length > 0 && (
|
||||||
|
<>
|
||||||
|
<hr className='mx-auto w-full h-1 border-0 bg-slate-200 my-2 rounded-md mt-8' />
|
||||||
|
<h1 className='text-3xl md:my-6 my-4 font-serif'>
|
||||||
|
Related documents {`(${affiliationDocuments.length})`}
|
||||||
|
</h1>
|
||||||
|
{affiliationDocuments.map((d) => (
|
||||||
|
<Fragment key={d.slug}>
|
||||||
|
<DocumentCard doc={d.doc} href={`/document/view/${d.slug}`} />
|
||||||
|
</Fragment>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ export default function findDocumentsByAuthorSorted(
|
||||||
.map(([slug, doc]) => ({ slug, doc }))
|
.map(([slug, doc]) => ({ slug, doc }))
|
||||||
|
|
||||||
// Sort the filtered documents by the latest date in descending order
|
// Sort the filtered documents by the latest date in descending order
|
||||||
const sortedDocuments = filteredDocuments.sort((a, b) => {
|
const sortedDocuments = filteredDocuments.toSorted((a, b) => {
|
||||||
const latestDateA = a.doc.manifest.dates[a.doc.manifest.dates.length - 1]
|
const latestDateA = a.doc.manifest.dates[a.doc.manifest.dates.length - 1]
|
||||||
const latestDateB = b.doc.manifest.dates[b.doc.manifest.dates.length - 1]
|
const latestDateB = b.doc.manifest.dates[b.doc.manifest.dates.length - 1]
|
||||||
return latestDateB - latestDateA // For descending order
|
return latestDateB - latestDateA // For descending order
|
||||||
|
|
Loading…
Reference in a new issue