From 0d59a6d2995b4119865271ed1908ede0bb96497c Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 9 May 2022 18:19:38 +0200 Subject: refactor: rewrite DescriptionList and Meta components The meta can have different layout. The previous implementation was not enough to easily change the layout. Also, I prefer to restrict the meta types and it prevents me to repeat myself for the labels. --- .../organisms/layout/cards-list.stories.tsx | 16 ++--- .../organisms/layout/cards-list.test.tsx | 26 +++---- .../organisms/layout/comment.module.scss | 12 ++-- src/components/organisms/layout/comment.test.tsx | 2 +- src/components/organisms/layout/comment.tsx | 83 ++++++++++++++-------- .../organisms/layout/overview.module.scss | 34 ++++++++- .../organisms/layout/overview.stories.tsx | 29 +++++--- src/components/organisms/layout/overview.test.tsx | 19 +++-- src/components/organisms/layout/overview.tsx | 41 +++++++++-- .../organisms/layout/posts-list.stories.tsx | 80 +++++++-------------- .../organisms/layout/posts-list.test.tsx | 76 ++++++++------------ src/components/organisms/layout/posts-list.tsx | 2 +- .../organisms/layout/summary.module.scss | 13 ++++ .../organisms/layout/summary.stories.tsx | 33 +++------ src/components/organisms/layout/summary.test.tsx | 27 ++++--- src/components/organisms/layout/summary.tsx | 33 ++++----- 16 files changed, 281 insertions(+), 245 deletions(-) (limited to 'src/components/organisms') diff --git a/src/components/organisms/layout/cards-list.stories.tsx b/src/components/organisms/layout/cards-list.stories.tsx index fe0ebfd..522d068 100644 --- a/src/components/organisms/layout/cards-list.stories.tsx +++ b/src/components/organisms/layout/cards-list.stories.tsx @@ -93,9 +93,7 @@ const items: CardsListItem[] = [ // @ts-ignore - Needed because of the placeholder image. unoptimized: true, }, - meta: [ - { id: 'meta-1', term: 'Quibusdam', value: ['Velit', 'Ex', 'Alias'] }, - ], + meta: { thematics: ['Velit', 'Ex', 'Alias'] }, tagline: 'Molestias ut error', title: 'Et alias omnis', url: '#', @@ -110,7 +108,7 @@ const items: CardsListItem[] = [ // @ts-ignore - Needed because of the placeholder image. unoptimized: true, }, - meta: [{ id: 'meta-1', term: 'Est', value: ['Voluptas'] }], + meta: { thematics: ['Voluptas'] }, tagline: 'Quod vel accusamus', title: 'Laboriosam doloremque mollitia', url: '#', @@ -125,13 +123,9 @@ const items: CardsListItem[] = [ // @ts-ignore - Needed because of the placeholder image. unoptimized: true, }, - meta: [ - { - id: 'meta-1', - term: 'Omnis', - value: ['Quisquam', 'Quia', 'Sapiente', 'Perspiciatis'], - }, - ], + meta: { + thematics: ['Quisquam', 'Quia', 'Sapiente', 'Perspiciatis'], + }, tagline: 'Quo error eum', title: 'Magni rem nulla', url: '#', diff --git a/src/components/organisms/layout/cards-list.test.tsx b/src/components/organisms/layout/cards-list.test.tsx index 2df3f59..7d98844 100644 --- a/src/components/organisms/layout/cards-list.test.tsx +++ b/src/components/organisms/layout/cards-list.test.tsx @@ -1,7 +1,7 @@ import { render, screen } from '@test-utils'; -import CardsList from './cards-list'; +import CardsList, { type CardsListItem } from './cards-list'; -const items = [ +const items: CardsListItem[] = [ { id: 'card-1', cover: { @@ -9,10 +9,10 @@ const items = [ src: 'http://placeimg.com/640/480', width: 640, height: 480, + // @ts-ignore - Needed because of the placeholder image. + unoptimized: true, }, - meta: [ - { id: 'meta-1', term: 'Quibusdam', value: ['Velit', 'Ex', 'Alias'] }, - ], + meta: { thematics: ['Velit', 'Ex', 'Alias'] }, tagline: 'Molestias ut error', title: 'Et alias omnis', url: '#', @@ -24,8 +24,10 @@ const items = [ src: 'http://placeimg.com/640/480', width: 640, height: 480, + // @ts-ignore - Needed because of the placeholder image. + unoptimized: true, }, - meta: [{ id: 'meta-1', term: 'Est', value: ['Voluptas'] }], + meta: { thematics: ['Voluptas'] }, tagline: 'Quod vel accusamus', title: 'Laboriosam doloremque mollitia', url: '#', @@ -37,14 +39,12 @@ const items = [ src: 'http://placeimg.com/640/480', width: 640, height: 480, + // @ts-ignore - Needed because of the placeholder image. + unoptimized: true, + }, + meta: { + thematics: ['Quisquam', 'Quia', 'Sapiente', 'Perspiciatis'], }, - meta: [ - { - id: 'meta-1', - term: 'Omnis', - value: ['Quisquam', 'Quia', 'Sapiente', 'Perspiciatis'], - }, - ], tagline: 'Quo error eum', title: 'Magni rem nulla', url: '#', diff --git a/src/components/organisms/layout/comment.module.scss b/src/components/organisms/layout/comment.module.scss index 54201de..d2b68e1 100644 --- a/src/components/organisms/layout/comment.module.scss +++ b/src/components/organisms/layout/comment.module.scss @@ -60,16 +60,20 @@ } .date { - flex-flow: row wrap; - justify-content: center; margin: var(--spacing-sm) 0; font-size: var(--font-size-sm); - text-align: center; + + &__item { + justify-content: center; + } @include mix.media("screen") { @include mix.dimensions("sm") { - justify-content: left; margin: 0 0 var(--spacing-sm); + + &__item { + justify-content: left; + } } } } diff --git a/src/components/organisms/layout/comment.test.tsx b/src/components/organisms/layout/comment.test.tsx index 9e537e5..4961722 100644 --- a/src/components/organisms/layout/comment.test.tsx +++ b/src/components/organisms/layout/comment.test.tsx @@ -1,5 +1,5 @@ import { render, screen } from '@test-utils'; -import { getFormattedDate, getFormattedTime } from '@utils/helpers/format'; +import { getFormattedDate, getFormattedTime } from '@utils/helpers/dates'; import Comment from './comment'; const author = { diff --git a/src/components/organisms/layout/comment.tsx b/src/components/organisms/layout/comment.tsx index 6d41c00..248efc2 100644 --- a/src/components/organisms/layout/comment.tsx +++ b/src/components/organisms/layout/comment.tsx @@ -1,10 +1,12 @@ import Button from '@components/atoms/buttons/button'; import Link from '@components/atoms/links/link'; -import DescriptionList from '@components/atoms/lists/description-list'; -import { getFormattedDate, getFormattedTime } from '@utils/helpers/format'; +import Meta from '@components/molecules/layout/meta'; +import useSettings from '@utils/hooks/use-settings'; import Image from 'next/image'; +import Script from 'next/script'; import { FC, useState } from 'react'; import { useIntl } from 'react-intl'; +import { type Comment as CommentSchema, type WithContext } from 'schema-dts'; import CommentForm, { type CommentFormProps } from '../forms/comment-form'; import styles from './comment.module.scss'; @@ -41,7 +43,11 @@ export type CommentProps = { */ id: number | string; /** - * The comment date. + * The comment parent id. + */ + parentId?: number | string; + /** + * The comment date and time separated with a space. */ publication: string; /** @@ -60,15 +66,14 @@ const Comment: FC = ({ canReply = true, content, id, + parentId, publication, saveComment, ...props }) => { const intl = useIntl(); const [isReplying, setIsReplying] = useState(false); - const commentDate = getFormattedDate(publication); - const commentTime = getFormattedTime(publication); - const commentDateTime = new Date(publication).toISOString(); + const [publicationDate, publicationTime] = publication.split(' '); const avatarAltText = intl.formatMessage( { @@ -89,37 +94,46 @@ const Comment: FC = ({ description: 'Comment: reply button', id: 'hzHuCc', }); - const dateLabel = intl.formatMessage({ - defaultMessage: 'Published on:', - description: 'Comment: publication date label', - id: 'soj7do', - }); - const dateValue = intl.formatMessage( - { - defaultMessage: '{date} at {time}', - description: 'Comment: publication date and time', - id: 'Ld6yMP', - }, - { - date: commentDate, - time: commentTime, - } - ); const formTitle = intl.formatMessage({ defaultMessage: 'Leave a reply', description: 'Comment: comment form title', id: '2fD5CI', }); - const dateLink = ( - - - {dateValue} - - ); + const { website } = useSettings(); + + const commentSchema: WithContext = { + '@context': 'https://schema.org', + '@id': `${website.url}/#comment-${id}`, + '@type': 'Comment', + parentItem: parentId + ? { '@id': `${website.url}/#comment-${parentId}` } + : undefined, + about: { '@type': 'Article', '@id': `${website.url}/#article` }, + author: { + '@type': 'Person', + name: author.name, + image: author.avatar, + url: author.url, + }, + creator: { + '@type': 'Person', + name: author.name, + image: author.avatar, + url: author.url, + }, + dateCreated: publication, + datePublished: publication, + text: content, + }; return ( <> +