From aa4f4bf8088790aeaf340ce35b01d42727b7c8d2 Mon Sep 17 00:00:00 2001 From: folland87 Date: Tue, 12 Nov 2024 11:48:48 +0100 Subject: [PATCH] fix(search): PublicationItem display authors and source correctly --- .../publications/publication-item.tsx | 157 +++++++++++------- 1 file changed, 101 insertions(+), 56 deletions(-) diff --git a/client/src/pages/search/components/publications/publication-item.tsx b/client/src/pages/search/components/publications/publication-item.tsx index 95092765..96bcf862 100644 --- a/client/src/pages/search/components/publications/publication-item.tsx +++ b/client/src/pages/search/components/publications/publication-item.tsx @@ -5,7 +5,99 @@ import { publicationTypeMapping, encode } from "../../../../utils/string"; import { getPublicationById } from "../../../../api/publications/[id]"; import { LightPublication } from "../../../../types/publication"; import { ItemProps } from "../../types"; -import { useIntl } from "react-intl"; +import { IntlShape, useIntl } from "react-intl"; + +const AUTHOR_DISPLAY_LIMIT = 3; + +const isThesis = (publication: LightPublication) => publication.type === "thesis"; + +type TAuthors = LightPublication["authors"] + +const AuthorDisplay = ({ author }: { author: TAuthors[0] }) => { + if (!author?.person) return author.fullName; + return ( + + {author.fullName} + + ); +} +type AuthorListDisplayProps = { + authors: TAuthors; + intl: IntlShape; + limit?: number; +} + +const AuthorListDisplay = ( + { authors, intl, limit = AUTHOR_DISPLAY_LIMIT }: AuthorListDisplayProps +) => { + if (authors.length === 0) return null; + if (authors.length === 1) return ; + + const shouldShowAllAuthors = !limit || authors.length <= limit; + const displayedAuthors = authors.slice(0, shouldShowAllAuthors ? authors.length : 1); + + return ( + <> + {displayedAuthors.map((author, index) => ( + + {index > 0 && index === displayedAuthors.length - 1 && authors.length <= limit + ? ` ${intl.formatMessage({ id: "search.publications.thesis.and" })} ` + : index > 0 + ? ', ' + : ''} + + + ))} + {(!shouldShowAllAuthors && authors.length > limit) && ( + {' '}et al. + )} + + ); +}; + +const ThesisAuthors = ( + { intl, authors }: { authors: LightPublication["authors"], intl: IntlShape } +) => { + const _authors = authors?.filter((author) => author.role === "author") || []; + const _directors = authors?.filter((author) => author.role === "directeurthese") || []; + + return ( + <> + + {_authors.length > 0 && ( + <> + {intl.formatMessage({ id: "search.publications.thesis.by" })} + + + )} + {_directors.length > 0 && ( + <> + {intl.formatMessage({ id: "search.publication.thesis.directed" })} + + + )} + + + ); +} + +const ArticleAuthors = ( + { intl, authors }: { authors: LightPublication["authors"], intl: IntlShape } +) => { + return ( + + + + ) +} + +const Authors = ( + { intl, authors, isThesis }: { authors: LightPublication["authors"], intl: IntlShape, isThesis: boolean } +) => { + return isThesis + ? + : +} export default function PublicationItem({ data: publication, @@ -22,12 +114,6 @@ export default function PublicationItem({ }); } - const authors = - publication.authors?.filter((author) => author.role === "author") || []; - const directors = - publication.authors?.filter((author) => author.role === "directeurthese") || - []; - return (
@@ -64,57 +150,16 @@ export default function PublicationItem({ )} - - {authors.map((author, index) => ( - - - {" "} - {intl.formatMessage({ - id: "search.publications.thesis.by", - })} - - {index > 0 && ", "} - {author.person ? ( - - {author.fullName} - - ) : ( - author.fullName - )} - - ))} - {authors.length > 5 && ( - - et al. - - )} - {!!directors.length && - intl.formatMessage({ id: "search.publication.thesis.directed" })} - {directors.map((director, index) => ( - - - {director.fullName} - - {(directors.length === 2 && index === 0) || - index === directors.length - 2 - ? intl.formatMessage({ - id: "search.publications.thesis.and", - }) - : index < directors.length - 1 - ? ", " - : ""} - - ))} - + - {publication.source?.title && `${publication.source?.title}`} - {publication.source?.volume && `, ${publication.source?.volume}`} - {publication.source?.issue && ` (${publication.source?.issue})`} - {publication.year && publication.source?.title && ", "} - {publication.year && `${publication.year}`} - {publication.source?.publisher && - `, ${publication.source?.publisher}`} + {[ + publication.source?.title, + publication.source?.volume && `${publication.source?.volume}`, + publication.source?.issue && `(${publication.source?.issue})`, + publication.year, + publication.source?.publisher + ].filter(Boolean).join(', ')} {Object.values(highlight || {}).map((value, i) => (