From c95cce04393080a52a54191cff7be8fec68af4b0 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Sun, 15 May 2022 17:45:41 +0200 Subject: chore: add Article pages --- .../organisms/forms/comment-form.stories.tsx | 30 ++++++++++++---------- .../organisms/forms/comment-form.test.tsx | 7 +++-- src/components/organisms/forms/comment-form.tsx | 19 ++++++++++++-- 3 files changed, 39 insertions(+), 17 deletions(-) (limited to 'src/components/organisms/forms') 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) => ( - - - - ), - ], } as ComponentMeta; const Template: ComponentStory = (args) => ( @@ -91,8 +100,3 @@ const Template: ComponentStory = (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( null} />); + render(); expect(screen.getByRole('form')).toBeInTheDocument(); }); it('renders an optional title', () => { render( - null} 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. @@ -16,11 +24,15 @@ export type CommentFormProps = { * Pass a component to print a success/error message. */ 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; /** * The form title. */ @@ -34,6 +46,7 @@ export type CommentFormProps = { const CommentForm: FC = ({ className = '', Notice, + parentId, saveComment, title, titleLevel = 2, @@ -95,7 +108,9 @@ const CommentForm: FC = ({ */ const submitHandler = () => { setIsSubmitting(true); - saveComment(resetForm); + saveComment({ comment, email, name, parentId, website }, resetForm).then( + () => setIsSubmitting(false) + ); }; return ( -- cgit v1.2.3