From 8bd9784acdee6871ad70e86d0d7120299bf76969 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 1 Mar 2022 22:05:08 +0100 Subject: refactor: split posts meta into smaller components --- src/components/PostMeta/PostMeta.tsx | 236 +++++------------------------------ 1 file changed, 29 insertions(+), 207 deletions(-) (limited to 'src/components/PostMeta/PostMeta.tsx') diff --git a/src/components/PostMeta/PostMeta.tsx b/src/components/PostMeta/PostMeta.tsx index b951c44..e89e0e2 100644 --- a/src/components/PostMeta/PostMeta.tsx +++ b/src/components/PostMeta/PostMeta.tsx @@ -1,19 +1,24 @@ +import { + Author, + CommentsCount, + Dates, + PostsCount, + ReadingTime, + Thematics, + Topics, + Website, +} from '@components/MetaItems'; +import { MetaKind } from '@ts/types/app'; import { ArticleMeta } from '@ts/types/articles'; -import { settings } from '@utils/config'; -import { getFormattedDate } from '@utils/helpers/format'; -import Link from 'next/link'; import { useRouter } from 'next/router'; -import { useIntl } from 'react-intl'; import styles from './PostMeta.module.scss'; -type PostMetaMode = 'list' | 'single'; - const PostMeta = ({ meta, - mode = 'list', + kind = 'list', }: { meta: ArticleMeta; - mode?: PostMetaMode; + kind?: MetaKind; }) => { const { author, @@ -26,217 +31,34 @@ const PostMeta = ({ website, wordsCount, } = meta; - const intl = useIntl(); - const router = useRouter(); - const locale = router.locale ? router.locale : settings.locales.defaultLocale; - const isThematic = () => router.asPath.includes('/thematique/'); - const isArticle = () => router.asPath.includes('/article/'); - - const getTopics = () => { - return ( - topics && - topics.map((topic) => { - return ( -
- - {topic.title} - -
- ); - }) - ); - }; - - const getThematics = () => { - return ( - thematics && - thematics.map((thematic) => { - return ( -
- - {thematic.title} - -
- ); - }) - ); - }; - - const getCommentsCount = () => { - return intl.formatMessage( - { - defaultMessage: - '{commentCount, plural, =0 {No comments} one {# comment} other {# comments}}', - description: 'PostMeta: comment count value', - }, - { commentCount } - ); - }; - - const getReadingTime = () => { - if (!readingTime) return; - if (readingTime < 0) - return intl.formatMessage({ - defaultMessage: 'less than 1 minute', - description: 'PostMeta: Reading time value', - }); - return intl.formatMessage( - { - defaultMessage: - '{readingTime, plural, =0 {# minutes} one {# minute} other {# minutes}}', - description: 'PostMeta: reading time value', - }, - { readingTime } - ); - }; + const { asPath } = useRouter(); + const isThematic = () => asPath.includes('/thematique/'); - const getDates = () => { - if (!dates) return <>; - - const publicationDate = getFormattedDate(dates.publication, locale); - const updateDate = getFormattedDate(dates.update, locale); - - return ( - <> -
-
- {intl.formatMessage({ - defaultMessage: 'Published on:', - description: 'PostMeta: publication date label', - })} -
-
- -
-
- {publicationDate !== updateDate && ( -
-
- {intl.formatMessage({ - defaultMessage: 'Updated on:', - description: 'PostMeta: update date label', - })} -
-
- -
-
- )} - - ); - }; - - const wrapperClass = styles[`wrapper--${mode}`]; + const wrapperClass = styles[`wrapper--${kind}`]; return (
- {author && ( -
-
- {intl.formatMessage({ - defaultMessage: 'Written by:', - description: 'Article meta', - })} -
-
{author.name}
-
+ {author && } + {dates && ( + )} - {getDates()} {readingTime !== undefined && wordsCount !== undefined && ( -
-
- {intl.formatMessage({ - defaultMessage: 'Reading time:', - description: 'Article meta', - })} -
-
- {getReadingTime()} -
-
- )} - {results && ( -
-
- {intl.formatMessage({ - defaultMessage: 'Total:', - description: 'Article meta', - })} -
-
- {intl.formatMessage( - { - defaultMessage: - '{results, plural, =0 {No articles} one {# article} other {# articles}}', - description: 'PostMeta: total found articles', - }, - { results } - )} -
-
+ )} + {results && } {!isThematic() && thematics && thematics.length > 0 && ( -
-
- {intl.formatMessage( - { - defaultMessage: - '{thematicsCount, plural, =0 {Thematics:} one {Thematic:} other {Thematics:}}', - description: 'PostMeta: thematics list label', - }, - { thematicsCount: thematics.length } - )} -
- {getThematics()} -
+ )} {isThematic() && topics && topics.length > 0 && ( -
-
- {intl.formatMessage( - { - defaultMessage: - '{topicsCount, plural, =0 {Topics:} one {Topic:} other {Topics:}}', - description: 'PostMeta: topics list label', - }, - { topicsCount: topics.length } - )} -
- {getTopics()} -
- )} - {website && ( -
-
- {intl.formatMessage({ - defaultMessage: 'Website:', - description: 'PostMeta: website label', - })} -
-
- {website} -
-
+ )} + {website && } {commentCount !== undefined && ( -
-
- {intl.formatMessage({ - defaultMessage: 'Comments:', - description: 'PostMeta: comment count label', - })} -
-
- {isArticle() ? ( - {getCommentsCount()} - ) : ( - getCommentsCount() - )} -
-
+ )}
); -- cgit v1.2.3