From f699802b837d7d9fcf150ff2bf00cd3c5475c87a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 9 Nov 2023 17:18:46 +0100 Subject: refactor(components): rewrite CommentsList component * use ApprovedCommentProps to make CommentData type * add the author name of the parent on reply form heading * add tests --- .../comments-list/comments-list.module.scss | 3 + .../comments-list/comments-list.stories.tsx | 177 ++++++++++++++ .../organisms/comments-list/comments-list.test.tsx | 264 +++++++++++++++++++++ .../organisms/comments-list/comments-list.tsx | 147 ++++++++++++ src/components/organisms/comments-list/index.ts | 1 + 5 files changed, 592 insertions(+) create mode 100644 src/components/organisms/comments-list/comments-list.module.scss create mode 100644 src/components/organisms/comments-list/comments-list.stories.tsx create mode 100644 src/components/organisms/comments-list/comments-list.test.tsx create mode 100644 src/components/organisms/comments-list/comments-list.tsx create mode 100644 src/components/organisms/comments-list/index.ts (limited to 'src/components/organisms/comments-list') diff --git a/src/components/organisms/comments-list/comments-list.module.scss b/src/components/organisms/comments-list/comments-list.module.scss new file mode 100644 index 0000000..e690250 --- /dev/null +++ b/src/components/organisms/comments-list/comments-list.module.scss @@ -0,0 +1,3 @@ +.reply { + margin-top: var(--spacing-sm); +} diff --git a/src/components/organisms/comments-list/comments-list.stories.tsx b/src/components/organisms/comments-list/comments-list.stories.tsx new file mode 100644 index 0000000..f6ad58e --- /dev/null +++ b/src/components/organisms/comments-list/comments-list.stories.tsx @@ -0,0 +1,177 @@ +import type { ComponentMeta, ComponentStory } from '@storybook/react'; +import { type CommentData, CommentsList } from './comments-list'; + +const saveComment = () => undefined; + +/** + * CommentsList - Storybook Meta + */ +export default { + title: 'Organisms/CommentsList', + component: CommentsList, + argTypes: { + comments: { + control: { + type: null, + }, + description: 'An array of comments.', + type: { + name: 'object', + required: true, + value: {}, + }, + }, + depth: { + control: { + type: 'number', + min: 0, + max: 4, + }, + description: 'The maximum depth. Use `0` to hide replies.', + table: { + category: 'Options', + defaultValue: { summary: 0 }, + }, + type: { + name: 'number', + required: false, + }, + }, + onSubmit: { + control: { + type: null, + }, + description: 'A callback function to save the comment form data.', + table: { + category: 'Events', + }, + type: { + name: 'function', + required: false, + }, + }, + }, +} as ComponentMeta; + +const Template: ComponentStory = (args) => ( + +); + +const comments = [ + { + author: { + name: 'Milan0', + avatar: { + alt: 'Milan0 avatar', + src: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/976.jpg', + }, + }, + content: 'Fugit veniam quas qui dolor explicabo.', + id: 1, + isApproved: true, + publicationDate: '2023-01-23', + replies: [ + { + author: { name: 'Haskell42' }, + content: 'Error quas accusamus nesciunt enim quae a.', + id: 25, + isApproved: true, + publicationDate: '2023-02-04', + }, + { + author: { name: 'Hanna49', website: 'https://www.armandphilippot.com' }, + content: 'Ut ducimus neque aliquam soluta sed totam commodi cum sit.', + id: 30, + isApproved: true, + publicationDate: '2023-03-10', + }, + ], + }, + { + author: { + name: 'Corrine9', + avatar: { + alt: 'Corrine9 avatar', + src: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/539.jpg', + }, + }, + content: + 'Dolore hic iure voluptatum quam error minima. Quas ut aperiam sit commodi cumque consequatur. Voluptas debitis veritatis officiis in voluptas ea et laborum animi. Voluptatem qui enim neque. Et sunt quo neque assumenda iure. Non vel ut consectetur.', + id: 2, + isApproved: true, + publicationDate: '2023-04-20', + }, + { + author: { name: 'Presley12' }, + content: + 'Nulla eaque similique recusandae enim aut eligendi iure consequatur. Et aut qui. Voluptatem a voluptatem consequatur aliquid distinctio ex culpa. Adipisci animi amet reprehenderit autem quia commodi voluptatum commodi.', + id: 3, + isApproved: true, + publicationDate: '2023-05-01', + replies: [ + { + author: { + name: 'Ana_Haley33', + avatar: { + alt: 'Ana_Haley33 avatar', + src: 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/881.jpg', + }, + }, + content: 'Ab ea et fugit autem.', + id: 17, + isApproved: true, + publicationDate: '2023-05-01', + }, + { + author: { name: 'Santos.Harris17' }, + content: + 'Illo dolores voluptatem similique voluptas quasi hic aspernatur ab nisi.', + id: 18, + isApproved: false, + publicationDate: '2023-05-02', + }, + ], + }, + { + author: { name: 'Julius.Borer' }, + content: 'Ea fugit totam et voluptatum quidem laborum explicabo fuga quod.', + id: 4, + isApproved: true, + publicationDate: '2023-06-15', + }, + { + author: { name: 'Geo87' }, + content: + 'Enim consequatur deleniti aliquid adipisci. Et mollitia saepe vel rerum totam praesentium assumenda repellat fuga. Ipsum ut architecto consequatur. Ut laborum suscipit sed corporis quas aliquid. Et et omnis quo. Dolore quia ipsum ut corporis eum et corporis qui.', + id: 5, + isApproved: false, + publicationDate: '2023-06-16', + }, + { + author: { name: 'Kurt.Keeling' }, + content: 'Eligendi repellat officiis amet.', + id: 6, + isApproved: true, + publicationDate: '2023-06-17', + }, +] satisfies CommentData[]; + +/** + * Layout Stories - Without nested comments + */ +export const WithoutReplies = Template.bind({}); +WithoutReplies.args = { + comments, + depth: 0, + onSubmit: saveComment, +}; + +/** + * Layout Stories - With nested comments + */ +export const WithReplies = Template.bind({}); +WithReplies.args = { + comments, + depth: 2, + onSubmit: saveComment, +}; diff --git a/src/components/organisms/comments-list/comments-list.test.tsx b/src/components/organisms/comments-list/comments-list.test.tsx new file mode 100644 index 0000000..6706362 --- /dev/null +++ b/src/components/organisms/comments-list/comments-list.test.tsx @@ -0,0 +1,264 @@ +import { describe, expect, it } from '@jest/globals'; +import { userEvent } from '@testing-library/user-event'; +import { render, screen as rtlScreen } from '../../../../tests/utils'; +import { type CommentData, CommentsList } from './comments-list'; + +describe('CommentsList', () => { + it('renders a list of approved comments', () => { + const comments = [ + { + author: { name: 'Milan0' }, + content: 'Fugit veniam quas qui dolor explicabo.', + id: 1, + isApproved: true, + publicationDate: '2023-01-23', + }, + { + author: { name: 'Haskell42' }, + content: 'Error quas accusamus nesciunt enim quae a.', + id: 2, + isApproved: true, + publicationDate: '2023-02-04', + }, + ] satisfies CommentData[]; + + render(); + + expect(rtlScreen.getAllByRole('listitem')).toHaveLength(comments.length); + expect(rtlScreen.getByText(comments[0].author.name)).toBeInTheDocument(); + expect(rtlScreen.getByText(comments[0].content)).toBeInTheDocument(); + expect(rtlScreen.getByText(comments[1].author.name)).toBeInTheDocument(); + expect(rtlScreen.getByText(comments[1].content)).toBeInTheDocument(); + }); + + it('renders a list of pending comments', () => { + const comments = [ + { + author: { name: 'Milan0' }, + content: 'Fugit veniam quas qui dolor explicabo.', + id: 1, + isApproved: false, + publicationDate: '2023-01-23', + }, + { + author: { name: 'Haskell42' }, + content: 'Error quas accusamus nesciunt enim quae a.', + id: 2, + isApproved: false, + publicationDate: '2023-02-04', + }, + ] satisfies CommentData[]; + + render(); + + expect(rtlScreen.getAllByRole('listitem')).toHaveLength(comments.length); + expect( + rtlScreen.queryByText(comments[0].author.name) + ).not.toBeInTheDocument(); + expect(rtlScreen.queryByText(comments[0].content)).not.toBeInTheDocument(); + expect( + rtlScreen.queryByText(comments[1].author.name) + ).not.toBeInTheDocument(); + expect(rtlScreen.queryByText(comments[1].content)).not.toBeInTheDocument(); + }); + + it('renders a mixed list of approved and pending comments', () => { + const comments = [ + { + author: { name: 'Milan0' }, + content: 'Fugit veniam quas qui dolor explicabo.', + id: 1, + isApproved: true, + publicationDate: '2023-01-23', + }, + { + author: { name: 'Haskell42' }, + content: 'Error quas accusamus nesciunt enim quae a.', + id: 2, + isApproved: false, + publicationDate: '2023-02-04', + }, + ] satisfies CommentData[]; + + render(); + + expect(rtlScreen.getAllByRole('listitem')).toHaveLength(comments.length); + expect(rtlScreen.getByText(comments[0].author.name)).toBeInTheDocument(); + expect(rtlScreen.getByText(comments[0].content)).toBeInTheDocument(); + expect( + rtlScreen.queryByText(comments[1].author.name) + ).not.toBeInTheDocument(); + expect(rtlScreen.queryByText(comments[1].content)).not.toBeInTheDocument(); + }); + + it('does not render the replies by default', () => { + const comments = [ + { + author: { name: 'Milan0' }, + content: 'Fugit veniam quas qui dolor explicabo.', + id: 1, + isApproved: true, + publicationDate: '2023-01-23', + replies: [ + { + author: { name: 'Haskell42' }, + content: 'Error quas accusamus nesciunt enim quae a.', + id: 2, + isApproved: true, + publicationDate: '2023-02-04', + }, + { + author: { name: 'Hanna49' }, + content: + 'Ut ducimus neque aliquam soluta sed totam commodi cum sit.', + id: 3, + isApproved: true, + publicationDate: '2023-03-10', + }, + ], + }, + ] satisfies CommentData[]; + + render(); + + expect(rtlScreen.getAllByRole('listitem')).toHaveLength(1); + expect(rtlScreen.getByText(comments[0].author.name)).toBeInTheDocument(); + expect(rtlScreen.getByText(comments[0].content)).toBeInTheDocument(); + expect( + rtlScreen.queryByText(comments[0].replies[0].author.name) + ).not.toBeInTheDocument(); + expect( + rtlScreen.queryByText(comments[0].replies[0].content) + ).not.toBeInTheDocument(); + expect(rtlScreen.queryByText(/Reply/)).not.toBeInTheDocument(); + }); + + it('can render the replies by providing a depth', () => { + const comments = [ + { + author: { name: 'Milan0' }, + content: 'Fugit veniam quas qui dolor explicabo.', + id: 1, + isApproved: true, + publicationDate: '2023-01-23', + replies: [ + { + author: { name: 'Haskell42' }, + content: 'Error quas accusamus nesciunt enim quae a.', + id: 2, + isApproved: true, + publicationDate: '2023-02-04', + }, + { + author: { name: 'Hanna49' }, + content: + 'Ut ducimus neque aliquam soluta sed totam commodi cum sit.', + id: 3, + isApproved: true, + publicationDate: '2023-03-10', + }, + ], + }, + ] satisfies CommentData[]; + + render(); + + const totalComments = + comments.length + + comments.reduce( + (accumulator, currentValue) => + accumulator + currentValue.replies.length, + 0 + ); + + expect(rtlScreen.getAllByRole('listitem')).toHaveLength(totalComments); + expect(rtlScreen.getByText(comments[0].author.name)).toBeInTheDocument(); + expect(rtlScreen.getByText(comments[0].content)).toBeInTheDocument(); + expect( + rtlScreen.getByText(comments[0].replies[0].author.name) + ).toBeInTheDocument(); + expect( + rtlScreen.getByText(comments[0].replies[0].content) + ).toBeInTheDocument(); + expect(rtlScreen.getByText(/Reply/)).toBeInTheDocument(); + }); + + it('can allow replies on replies by providing a depth', () => { + const comments = [ + { + author: { name: 'Milan0' }, + content: 'Fugit veniam quas qui dolor explicabo.', + id: 1, + isApproved: true, + publicationDate: '2023-01-23', + replies: [ + { + author: { name: 'Haskell42' }, + content: 'Error quas accusamus nesciunt enim quae a.', + id: 2, + isApproved: true, + publicationDate: '2023-02-04', + }, + { + author: { name: 'Hanna49' }, + content: + 'Ut ducimus neque aliquam soluta sed totam commodi cum sit.', + id: 3, + isApproved: true, + publicationDate: '2023-03-10', + }, + ], + }, + ] satisfies CommentData[]; + + render(); + + const totalComments = + comments.length + + comments.reduce( + (accumulator, currentValue) => + accumulator + currentValue.replies.length, + 0 + ); + + expect(rtlScreen.getAllByRole('listitem')).toHaveLength(totalComments); + expect(rtlScreen.getAllByText(/Reply/)).toHaveLength(totalComments); + }); + + it('can render a reply form when clicking on the reply button', async () => { + const user = userEvent.setup(); + const comments = [ + { + author: { name: 'Milan0' }, + content: 'Fugit veniam quas qui dolor explicabo.', + id: 1, + isApproved: true, + publicationDate: '2023-01-23', + }, + { + author: { name: 'Haskell42' }, + content: 'Error quas accusamus nesciunt enim quae a.', + id: 2, + isApproved: true, + publicationDate: '2023-02-04', + }, + ] satisfies CommentData[]; + + render(); + + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + expect.assertions(3); + + const replyButtons = rtlScreen.getAllByText(/Reply/); + + expect(rtlScreen.queryByRole('form')).not.toBeInTheDocument(); + + await user.click(replyButtons[0]); + + expect(rtlScreen.getByRole('form')).toHaveAccessibleName(/Leave a reply/); + + await user.click(replyButtons[0]); + + expect(rtlScreen.queryByRole('form')).not.toBeInTheDocument(); + }); +}); diff --git a/src/components/organisms/comments-list/comments-list.tsx b/src/components/organisms/comments-list/comments-list.tsx new file mode 100644 index 0000000..0470f99 --- /dev/null +++ b/src/components/organisms/comments-list/comments-list.tsx @@ -0,0 +1,147 @@ +import { + forwardRef, + useCallback, + useState, + type ForwardRefRenderFunction, +} from 'react'; +import { useIntl } from 'react-intl'; +import type { Nullable } from '../../../types'; +import { Heading, List, ListItem, type ListProps } from '../../atoms'; +import { + ApprovedComment, + type ApprovedCommentProps, + PendingComment, + ReplyCommentForm, + type ReplyCommentFormProps, +} from '../comment'; +import styles from './comments-list.module.scss'; + +export type CommentData = Pick< + ApprovedCommentProps, + 'author' | 'content' | 'id' | 'publicationDate' +> & { + isApproved: boolean; + replies?: CommentData[]; +}; + +export type CommentsListProps = Omit< + ListProps, + | 'children' + | 'hideMarker' + | 'isHierarchical' + | 'isInline' + | 'isOrdered' + | 'onSubmit' + | 'spacing' +> & + Pick & { + /** + * The comments. + */ + comments: CommentData[]; + /** + * A positive integer. When depth is set to `0`, replies are not used. + * + * @default 0 + */ + depth?: number; + }; + +const CommentsListWithRef: ForwardRefRenderFunction< + HTMLOListElement, + CommentsListProps +> = ({ comments, depth = 0, onSubmit, ...props }, ref) => { + const [replyingTo, setReplyingTo] = useState>(null); + const intl = useIntl(); + + const toggleReply = useCallback((id: number) => { + setReplyingTo((prevId) => { + if (prevId === id) return null; + return id; + }); + }, []); + + const getComments = useCallback( + (data: CommentData[], currentDepth = 0) => { + const isLastLevel = depth === currentDepth; + + return data.map(({ isApproved, replies, ...comment }) => { + const replyFormHeading = intl.formatMessage( + { + defaultMessage: 'Leave a reply to {name}', + description: 'CommentsList: comment form title', + id: 'c1Ju/q', + }, + { name: comment.author.name } + ); + const replyBtnLabel = + replyingTo === comment.id + ? intl.formatMessage({ + defaultMessage: 'Cancel reply', + description: 'CommentsList: cancel reply button', + id: 'uZj4QI', + }) + : intl.formatMessage({ + defaultMessage: 'Reply', + description: 'CommentsList: reply button', + id: 'Qa9twM', + }); + + return ( + + {isApproved ? ( + <> + + {replyingTo === comment.id ? ( + {replyFormHeading}} + onSubmit={onSubmit} + /> + ) : null} + {replies?.length && !isLastLevel ? ( + + {getComments(replies, currentDepth + 1)} + + ) : null} + + ) : ( + + )} + + ); + }); + }, + [depth, intl, onSubmit, replyingTo, toggleReply] + ); + + return ( + + {getComments(comments)} + + ); +}; + +/** + * CommentsList component + * + * Render a list of comments. + */ +export const CommentsList = forwardRef(CommentsListWithRef); diff --git a/src/components/organisms/comments-list/index.ts b/src/components/organisms/comments-list/index.ts new file mode 100644 index 0000000..5d7fb9f --- /dev/null +++ b/src/components/organisms/comments-list/index.ts @@ -0,0 +1 @@ +export * from './comments-list'; -- cgit v1.2.3