From fd9831446ff87414da772b17327368cb291192e6 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 21 Apr 2022 18:36:45 +0200 Subject: chore: add a Comment component --- src/components/organisms/layout/comment.tsx | 175 ++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 src/components/organisms/layout/comment.tsx (limited to 'src/components/organisms/layout/comment.tsx') diff --git a/src/components/organisms/layout/comment.tsx b/src/components/organisms/layout/comment.tsx new file mode 100644 index 0000000..f4b822a --- /dev/null +++ b/src/components/organisms/layout/comment.tsx @@ -0,0 +1,175 @@ +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 Image from 'next/image'; +import { FC, useState } from 'react'; +import { useIntl } from 'react-intl'; +import CommentForm, { type CommentFormProps } from '../forms/comment-form'; +import styles from './comment.module.scss'; + +export type CommentAuthor = { + /** + * The author avatar. + */ + avatar: string; + /** + * The author name. + */ + name: string; + /** + * The author website. + */ + url?: string; +}; + +export type CommentProps = { + /** + * 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 post id. + */ + postId: number | string; + /** + * The comment date. + */ + publication: string; + /** + * A callback function to save comment form data. + */ + saveComment: CommentFormProps['saveComment']; +}; + +/** + * Comment component + * + * Render a single comment. + */ +const Comment: FC = ({ + author, + canReply = true, + content, + id, + 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 avatarAltText = intl.formatMessage( + { + defaultMessage: '{author} avatar', + description: 'Comment: avatar alternative text', + id: 'T/hUiO', + }, + { author: author.name } + ); + const buttonLabel = isReplying + ? intl.formatMessage({ + defaultMessage: 'Cancel reply', + description: 'Comment: cancel reply button', + id: 'LCorTC', + }) + : intl.formatMessage({ + defaultMessage: 'Reply', + 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} + + ); + + return ( + <> +
+
+
+ {avatarAltText} +
+ {author.url ? ( + + {author.name} + + ) : ( + {author.name} + )} +
+ +
{content}
+
+ {canReply && ( + + )} +
+
+ {isReplying && ( + + )} + + ); +}; + +export default Comment; -- cgit v1.2.3