diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-15 17:45:41 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-15 19:06:42 +0200 |
| commit | c95cce04393080a52a54191cff7be8fec68af4b0 (patch) | |
| tree | 1022bc574c8fc8e657be922b26c1cf16cbfd9071 /src/components/organisms | |
| parent | 235fe67d770f83131c9ec10b99012319440db690 (diff) | |
chore: add Article pages
Diffstat (limited to 'src/components/organisms')
9 files changed, 110 insertions, 51 deletions
diff --git a/src/components/organisms/forms/comment-form.stories.tsx b/src/components/organisms/forms/comment-form.stories.tsx index f66d35c..8b11df7 100644 --- a/src/components/organisms/forms/comment-form.stories.tsx +++ b/src/components/organisms/forms/comment-form.stories.tsx @@ -1,13 +1,19 @@ import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { IntlProvider } from 'react-intl'; import CommentForm from './comment-form'; +const saveComment = async () => { + /** Do nothing. */ +}; + /** * CommentForm - Storybook Meta */ export default { title: 'Organisms/Forms', component: CommentForm, + args: { + saveComment, + }, argTypes: { className: { control: { @@ -35,6 +41,16 @@ export default { required: false, }, }, + parentId: { + control: { + type: null, + }, + description: 'The parent id if it is a reply.', + type: { + name: 'number', + required: false, + }, + }, saveComment: { control: { type: null, @@ -74,13 +90,6 @@ export default { }, }, }, - decorators: [ - (Story) => ( - <IntlProvider locale="en"> - <Story /> - </IntlProvider> - ), - ], } as ComponentMeta<typeof CommentForm>; const Template: ComponentStory<typeof CommentForm> = (args) => ( @@ -91,8 +100,3 @@ const Template: ComponentStory<typeof CommentForm> = (args) => ( * Forms Stories - Comment */ export const Comment = Template.bind({}); -Comment.args = { - saveComment: (reset: () => void) => { - reset(); - }, -}; diff --git a/src/components/organisms/forms/comment-form.test.tsx b/src/components/organisms/forms/comment-form.test.tsx index 0d387b5..c67ad6b 100644 --- a/src/components/organisms/forms/comment-form.test.tsx +++ b/src/components/organisms/forms/comment-form.test.tsx @@ -1,17 +1,20 @@ import { render, screen } from '@test-utils'; import CommentForm from './comment-form'; +const saveComment = async () => { + /** Do nothing. */ +}; const title = 'Cum voluptas voluptatibus'; describe('CommentForm', () => { it('renders a form', () => { - render(<CommentForm saveComment={() => null} />); + render(<CommentForm saveComment={saveComment} />); expect(screen.getByRole('form')).toBeInTheDocument(); }); it('renders an optional title', () => { render( - <CommentForm saveComment={() => null} title={title} titleLevel={2} /> + <CommentForm saveComment={saveComment} title={title} titleLevel={2} /> ); expect( screen.getByRole('heading', { level: 2, name: title }) diff --git a/src/components/organisms/forms/comment-form.tsx b/src/components/organisms/forms/comment-form.tsx index d7cb0f5..9e0abdf 100644 --- a/src/components/organisms/forms/comment-form.tsx +++ b/src/components/organisms/forms/comment-form.tsx @@ -7,6 +7,14 @@ import { FC, ReactNode, useState } from 'react'; import { useIntl } from 'react-intl'; import styles from './comment-form.module.scss'; +export type CommentFormData = { + comment: string; + email: string; + name: string; + parentId?: number; + website?: string; +}; + export type CommentFormProps = { /** * Set additional classnames to the form wrapper. @@ -17,10 +25,14 @@ export type CommentFormProps = { */ Notice?: ReactNode; /** + * The comment parent id. + */ + parentId?: number; + /** * A callback function to save comment. It takes a function as parameter to * reset the form. */ - saveComment: (reset: () => void) => void; + saveComment: (data: CommentFormData, reset: () => void) => Promise<void>; /** * The form title. */ @@ -34,6 +46,7 @@ export type CommentFormProps = { const CommentForm: FC<CommentFormProps> = ({ className = '', Notice, + parentId, saveComment, title, titleLevel = 2, @@ -95,7 +108,9 @@ const CommentForm: FC<CommentFormProps> = ({ */ const submitHandler = () => { setIsSubmitting(true); - saveComment(resetForm); + saveComment({ comment, email, name, parentId, website }, resetForm).then( + () => setIsSubmitting(false) + ); }; return ( diff --git a/src/components/organisms/layout/comment.stories.tsx b/src/components/organisms/layout/comment.stories.tsx index 3794b06..c31b77a 100644 --- a/src/components/organisms/layout/comment.stories.tsx +++ b/src/components/organisms/layout/comment.stories.tsx @@ -1,13 +1,19 @@ import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { IntlProvider } from 'react-intl'; import CommentComponent from './comment'; +const saveComment = async () => { + /** Do nothing. */ +}; + /** * Comment - Storybook Meta */ export default { title: 'Organisms/Layout', component: CommentComponent, + args: { + saveComment, + }, argTypes: { author: { description: 'The author data.', @@ -51,6 +57,29 @@ export default { required: true, }, }, + Notice: { + control: { + type: null, + }, + description: 'A component to display a success or error message.', + table: { + category: 'Options', + }, + type: { + name: 'function', + required: false, + }, + }, + parentId: { + control: { + type: null, + }, + description: 'The parent id if it is a reply.', + type: { + name: 'number', + required: false, + }, + }, publication: { description: 'The publication date.', type: { @@ -73,13 +102,6 @@ export default { }, }, }, - decorators: [ - (Story) => ( - <IntlProvider locale="en"> - <Story /> - </IntlProvider> - ), - ], } as ComponentMeta<typeof CommentComponent>; const Template: ComponentStory<typeof CommentComponent> = (args) => ( @@ -100,7 +122,6 @@ Comment.args = { 'Harum aut cumque iure fugit neque sequi cupiditate repudiandae laudantium. Ratione aut assumenda qui illum voluptas accusamus quis officiis exercitationem. Consectetur est harum eius perspiciatis officiis nihil. Aut corporis minima debitis adipisci possimus debitis et.', id: 2, publication: '2021-04-03 23:04:24', - saveComment: () => null, // @ts-ignore - Needed because of the placeholder image. unoptimized: true, }; diff --git a/src/components/organisms/layout/comment.test.tsx b/src/components/organisms/layout/comment.test.tsx index 4961722..02a51dc 100644 --- a/src/components/organisms/layout/comment.test.tsx +++ b/src/components/organisms/layout/comment.test.tsx @@ -11,13 +11,15 @@ const content = 'Harum aut cumque iure fugit neque sequi cupiditate repudiandae laudantium. Ratione aut assumenda qui illum voluptas accusamus quis officiis exercitationem. Consectetur est harum eius perspiciatis officiis nihil. Aut corporis minima debitis adipisci possimus debitis et.'; const publication = '2021-04-03 23:04:24'; const id = 5; - +const saveComment = async () => { + /** Do nothing. */ +}; const data = { author, content, id, publication, - saveComment: () => null, + saveComment, }; const formattedDate = getFormattedDate(publication); diff --git a/src/components/organisms/layout/comment.tsx b/src/components/organisms/layout/comment.tsx index 248efc2..6df393b 100644 --- a/src/components/organisms/layout/comment.tsx +++ b/src/components/organisms/layout/comment.tsx @@ -25,7 +25,7 @@ export type CommentAuthor = { url?: string; }; -export type CommentProps = { +export type CommentProps = Pick<CommentFormProps, 'Notice' | 'saveComment'> & { /** * The comment author data. */ @@ -50,10 +50,6 @@ export type CommentProps = { * The comment date and time separated with a space. */ publication: string; - /** - * A callback function to save comment form data. - */ - saveComment: CommentFormProps['saveComment']; }; /** @@ -66,6 +62,7 @@ const Comment: FC<CommentProps> = ({ canReply = true, content, id, + Notice, parentId, publication, saveComment, @@ -169,7 +166,10 @@ const Comment: FC<CommentProps> = ({ className={styles.date} groupClassName={styles.date__item} /> - <div className={styles.body}>{content}</div> + <div + className={styles.body} + dangerouslySetInnerHTML={{ __html: content }} + /> <footer className={styles.footer}> {canReply && ( <Button kind="tertiary" onClick={() => setIsReplying(!isReplying)}> @@ -180,6 +180,8 @@ const Comment: FC<CommentProps> = ({ </article> {isReplying && ( <CommentForm + Notice={Notice} + parentId={id as number} saveComment={saveComment} title={formTitle} className={`${styles.wrapper} ${styles['wrapper--form']}`} diff --git a/src/components/organisms/layout/comments-list.stories.tsx b/src/components/organisms/layout/comments-list.stories.tsx index 9edf368..4d95205 100644 --- a/src/components/organisms/layout/comments-list.stories.tsx +++ b/src/components/organisms/layout/comments-list.stories.tsx @@ -1,13 +1,19 @@ import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { IntlProvider } from 'react-intl'; import CommentsListComponent, { Comment } from './comments-list'; +const saveComment = async () => { + /** Do nothing. */ +}; + /** * CommentsList - Storybook Meta */ export default { title: 'Organisms/Layout/CommentsList', component: CommentsListComponent, + args: { + saveComment, + }, argTypes: { comments: { control: { @@ -30,6 +36,19 @@ export default { required: true, }, }, + Notice: { + control: { + type: null, + }, + description: 'A component to display a success or error message.', + table: { + category: 'Options', + }, + type: { + name: 'function', + required: false, + }, + }, saveComment: { control: { type: null, @@ -44,13 +63,6 @@ export default { }, }, }, - decorators: [ - (Story) => ( - <IntlProvider locale="en"> - <Story /> - </IntlProvider> - ), - ], } as ComponentMeta<typeof CommentsListComponent>; const Template: ComponentStory<typeof CommentsListComponent> = (args) => ( @@ -130,7 +142,6 @@ export const WithoutChildComments = Template.bind({}); WithoutChildComments.args = { comments, depth: 0, - saveComment: () => null, }; /** @@ -140,5 +151,4 @@ export const WithChildComments = Template.bind({}); WithChildComments.args = { comments, depth: 1, - saveComment: () => null, }; diff --git a/src/components/organisms/layout/comments-list.test.tsx b/src/components/organisms/layout/comments-list.test.tsx index 542b1df..e135ec9 100644 --- a/src/components/organisms/layout/comments-list.test.tsx +++ b/src/components/organisms/layout/comments-list.test.tsx @@ -57,10 +57,14 @@ const comments: Comment[] = [ }, ]; +const saveComment = async () => { + /** Do nothing. */ +}; + describe('CommentsList', () => { it('renders a comments list', () => { render( - <CommentsList comments={comments} depth={1} saveComment={() => null} /> + <CommentsList comments={comments} depth={1} saveComment={saveComment} /> ); }); }); diff --git a/src/components/organisms/layout/comments-list.tsx b/src/components/organisms/layout/comments-list.tsx index 03f508e..f04354c 100644 --- a/src/components/organisms/layout/comments-list.tsx +++ b/src/components/organisms/layout/comments-list.tsx @@ -8,7 +8,7 @@ export type Comment = Omit<CommentProps, 'canReply' | 'saveComment'> & { child?: Comment[]; }; -export type CommentsListProps = { +export type CommentsListProps = Pick<CommentProps, 'Notice' | 'saveComment'> & { /** * An array of comments. */ @@ -17,10 +17,6 @@ export type CommentsListProps = { * The maximum depth. Use `0` to not display nested comments. */ depth: 0 | 1 | 2 | 3 | 4; - /** - * A callback function to save comment form data. - */ - saveComment: CommentProps['saveComment']; }; /** @@ -31,6 +27,7 @@ export type CommentsListProps = { const CommentsList: FC<CommentsListProps> = ({ comments, depth, + Notice, saveComment, }) => { /** @@ -48,8 +45,9 @@ const CommentsList: FC<CommentsListProps> = ({ return commentsList.map(({ child, ...comment }) => ( <li key={comment.id} className={styles.item}> <SingleComment - saveComment={saveComment} canReply={!isLastLevel} + Notice={Notice} + saveComment={saveComment} {...comment} /> {child && !isLastLevel && ( |
