From f6e0e444820f67f212e362c54816df5d0e4d4cf0 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 25 Jan 2022 15:53:43 +0100 Subject: chore: wrap dates with time tag --- src/components/Comment/Comment.tsx | 21 ++++---- src/components/PostMeta/PostMeta.tsx | 61 ++++++++++++---------- src/components/ProjectSummary/ProjectSummary.tsx | 45 ++++++++-------- src/components/Widgets/RecentPosts/RecentPosts.tsx | 17 +++--- src/utils/helpers/format.ts | 16 ++++++ 5 files changed, 90 insertions(+), 70 deletions(-) (limited to 'src') diff --git a/src/components/Comment/Comment.tsx b/src/components/Comment/Comment.tsx index 11300fc..6eb0184 100644 --- a/src/components/Comment/Comment.tsx +++ b/src/components/Comment/Comment.tsx @@ -3,6 +3,7 @@ import CommentForm from '@components/CommentForm/CommentForm'; import { config } from '@config/website'; import { t } from '@lingui/macro'; import { Comment as CommentData } from '@ts/types/comments'; +import { getFormattedDate } from '@utils/helpers/format'; import Head from 'next/head'; import Image from 'next/image'; import Link from 'next/link'; @@ -21,6 +22,7 @@ const Comment = ({ isNested?: boolean; }) => { const router = useRouter(); + const locale = router.locale ? router.locale : config.locales.defaultLocale; const [isReply, setIsReply] = useState(false); const firstFieldRef = useRef(null); @@ -39,14 +41,9 @@ const Comment = ({ }; const getLocaleDate = () => { - const commentDate = new Date(comment.date); - const date = commentDate.toLocaleDateString(router.locale, { - year: 'numeric', - month: 'long', - day: 'numeric', - }); - const time = commentDate - .toLocaleTimeString(router.locale, { + const date = getFormattedDate(comment.date, locale); + const time = new Date(comment.date) + .toLocaleTimeString(locale, { hour: 'numeric', minute: 'numeric', }) @@ -73,9 +70,11 @@ const Comment = ({
{t`Published on:`}
- - {getLocaleDate()} - +
asPath.includes('/thematique/'); - const isArticle = () => asPath.includes('/article/'); - - const dateOptions: Intl.DateTimeFormatOptions = { - day: 'numeric', - month: 'long', - year: 'numeric', - }; + const router = useRouter(); + const locale = router.locale ? router.locale : config.locales.defaultLocale; + const isThematic = () => router.asPath.includes('/thematique/'); + const isArticle = () => router.asPath.includes('/article/'); const getTopics = () => { return ( @@ -85,6 +82,32 @@ const PostMeta = ({ }); }; + const getDates = () => { + if (!dates) return <>; + + const publicationDate = getFormattedDate(dates.publication, locale); + const updateDate = getFormattedDate(dates.update, locale); + + return ( + <> +
+
{t`Published on:`}
+
+ +
+
+ {publicationDate !== updateDate && ( +
+
{t`Updated on:`}
+
+ +
+
+ )} + + ); + }; + const wrapperClass = styles[`wrapper--${mode}`]; return ( @@ -95,25 +118,7 @@ const PostMeta = ({
{author.name}
)} - {dates && ( -
-
{t`Published on:`}
-
- {new Date(dates.publication).toLocaleDateString( - locale, - dateOptions - )} -
-
- )} - {dates && dates.publication !== dates.update && ( -
-
{t`Updated on:`}
-
- {new Date(dates.update).toLocaleDateString(locale, dateOptions)} -
-
- )} + {getDates()} {readingTime !== undefined && wordsCount !== undefined && (
{t`Reading time:`}
diff --git a/src/components/ProjectSummary/ProjectSummary.tsx b/src/components/ProjectSummary/ProjectSummary.tsx index 5a4f9c8..1b07eb1 100644 --- a/src/components/ProjectSummary/ProjectSummary.tsx +++ b/src/components/ProjectSummary/ProjectSummary.tsx @@ -1,7 +1,9 @@ import GithubIcon from '@assets/images/social-media/github.svg'; import GitlabIcon from '@assets/images/social-media/gitlab.svg'; +import { config } from '@config/website'; import { t } from '@lingui/macro'; import { ProjectMeta } from '@ts/types/app'; +import { getFormattedDate } from '@utils/helpers/format'; import { slugify } from '@utils/helpers/slugify'; import useGithubApi from '@utils/hooks/useGithubApi'; import Image from 'next/image'; @@ -18,40 +20,41 @@ const ProjectSummary = ({ meta: ProjectMeta; }) => { const { license, repos, technologies } = meta; - const { locale } = useRouter(); + const router = useRouter(); + const locale = router.locale ? router.locale : config.locales.defaultLocale; const { data } = useGithubApi(repos?.github ? repos.github : ''); - const getFormattedDate = (date: string) => { - const dateOptions: Intl.DateTimeFormatOptions = { - day: 'numeric', - month: 'long', - year: 'numeric', - }; - - return new Date(date).toLocaleDateString(locale, dateOptions); - }; - return (
-
- {t`${title} -
+ {cover && ( +
+ {t`${title} +
+ )}
{data && (
{t`Created on`}
-
{t`${getFormattedDate(data.created_at)}`}
+
+ +
)} {data && (
{t`Last updated on`}
-
{t`${getFormattedDate(data.updated_at)}`}
+
+ +
)}
diff --git a/src/components/Widgets/RecentPosts/RecentPosts.tsx b/src/components/Widgets/RecentPosts/RecentPosts.tsx index 9c13aa2..8022bff 100644 --- a/src/components/Widgets/RecentPosts/RecentPosts.tsx +++ b/src/components/Widgets/RecentPosts/RecentPosts.tsx @@ -1,7 +1,9 @@ import Spinner from '@components/Spinner/Spinner'; +import { config } from '@config/website'; import { t } from '@lingui/macro'; import { getPublishedPosts } from '@services/graphql/queries'; import { ArticlePreview } from '@ts/types/articles'; +import { getFormattedDate } from '@utils/helpers/format'; import Image from 'next/image'; import Link from 'next/link'; import { useRouter } from 'next/router'; @@ -12,12 +14,8 @@ const RecentPosts = () => { const { data, error } = useSWR('/recent-posts', () => getPublishedPosts({ first: 3 }) ); - const { locale } = useRouter(); - const dateOptions: Intl.DateTimeFormatOptions = { - day: 'numeric', - month: 'long', - year: 'numeric', - }; + const router = useRouter(); + const locale = router.locale ? router.locale : config.locales.defaultLocale; const getPost = (post: ArticlePreview) => { return ( @@ -40,10 +38,9 @@ const RecentPosts = () => {
{t`Published on:`}
- {new Date(post.dates.publication).toLocaleDateString( - locale, - dateOptions - )} +
diff --git a/src/utils/helpers/format.ts b/src/utils/helpers/format.ts index 2be1844..e45a6a0 100644 --- a/src/utils/helpers/format.ts +++ b/src/utils/helpers/format.ts @@ -267,3 +267,19 @@ export const getFormattedPost = (rawPost: RawArticle): Article => { return formattedPost; }; + +/** + * Converts a date to a string by using the specified locale. + * @param {string} date The date. + * @param {string} locale A locale. + * @returns {string} The formatted date to locale date string. + */ +export const getFormattedDate = (date: string, locale: string) => { + const dateOptions: Intl.DateTimeFormatOptions = { + day: 'numeric', + month: 'long', + year: 'numeric', + }; + + return new Date(date).toLocaleDateString(locale, dateOptions); +}; -- cgit v1.2.3