From 34e216546151eaf8a0a3cbb0bc8b65dae4c63bf2 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 23 May 2022 14:07:02 +0200 Subject: refactor: reduce the number of data transformation --- src/components/organisms/layout/comment.tsx | 98 ++++++++++++++--------------- 1 file changed, 49 insertions(+), 49 deletions(-) (limited to 'src/components/organisms/layout/comment.tsx') diff --git a/src/components/organisms/layout/comment.tsx b/src/components/organisms/layout/comment.tsx index 6df393b..1dc2c71 100644 --- a/src/components/organisms/layout/comment.tsx +++ b/src/components/organisms/layout/comment.tsx @@ -1,6 +1,7 @@ import Button from '@components/atoms/buttons/button'; import Link from '@components/atoms/links/link'; import Meta from '@components/molecules/layout/meta'; +import { type Comment as CommentType } from '@ts/types/app'; import useSettings from '@utils/hooks/use-settings'; import Image from 'next/image'; import Script from 'next/script'; @@ -25,32 +26,16 @@ export type CommentAuthor = { url?: string; }; -export type CommentProps = Pick & { - /** - * The comment author data. - */ - author: CommentAuthor; - /** - * Enable or disable the reply button. Default: true. - */ - canReply?: boolean; - /** - * The comment body. - */ - content: string; - /** - * The comment id. - */ - id: number | string; - /** - * The comment parent id. - */ - parentId?: number | string; - /** - * The comment date and time separated with a space. - */ - publication: string; -}; +export type CommentProps = Pick< + CommentType, + 'approved' | 'content' | 'id' | 'meta' | 'parentId' +> & + Pick & { + /** + * Enable or disable the reply button. Default: true. + */ + canReply?: boolean; + }; /** * Comment component @@ -58,19 +43,34 @@ export type CommentProps = Pick & { * Render a single comment. */ const Comment: FC = ({ - author, + approved, canReply = true, content, id, + meta, Notice, parentId, - publication, saveComment, ...props }) => { const intl = useIntl(); + const { website } = useSettings(); const [isReplying, setIsReplying] = useState(false); - const [publicationDate, publicationTime] = publication.split(' '); + + if (!approved) { + return ( +
+ {intl.formatMessage({ + defaultMessage: 'This comment is awaiting moderation...', + description: 'Comment: awaiting moderation', + id: '6a1Uo6', + })} +
+ ); + } + + const { author, date } = meta; + const [publicationDate, publicationTime] = date.split(' '); const avatarAltText = intl.formatMessage( { @@ -97,8 +97,6 @@ const Comment: FC = ({ id: '2fD5CI', }); - const { website } = useSettings(); - const commentSchema: WithContext = { '@context': 'https://schema.org', '@id': `${website.url}/#comment-${id}`, @@ -110,17 +108,17 @@ const Comment: FC = ({ author: { '@type': 'Person', name: author.name, - image: author.avatar, - url: author.url, + image: author.avatar?.src, + url: author.website, }, creator: { '@type': 'Person', name: author.name, - image: author.avatar, - url: author.url, + image: author.avatar?.src, + url: author.website, }, - dateCreated: publication, - datePublished: publication, + dateCreated: date, + datePublished: date, text: content, }; @@ -136,17 +134,19 @@ const Comment: FC = ({ className={`${styles.wrapper} ${styles['wrapper--comment']}`} >
-
- {avatarAltText} -
- {author.url ? ( - + {author.avatar && ( +
+ {avatarAltText} +
+ )} + {author.website ? ( + {author.name} ) : ( @@ -181,7 +181,7 @@ const Comment: FC = ({ {isReplying && (