import { ButtonSubmit } from '@components/Buttons'; import { Form, FormItem, Input, TextArea } from '@components/Form'; import Notice from '@components/Notice/Notice'; import { createComment } from '@services/graphql/mutations'; import { ForwardedRef, forwardRef, useState } from 'react'; import { useIntl } from 'react-intl'; import styles from './CommentForm.module.scss'; const CommentForm = ( { articleId, parentId = 0, isReply = false, }: { articleId: number; parentId?: number; isReply?: boolean; }, ref: ForwardedRef ) => { const intl = useIntl(); const [name, setName] = useState(''); const [email, setEmail] = useState(''); const [website, setWebsite] = useState(''); const [message, setMessage] = useState(''); const [isSuccess, setIsSuccess] = useState(false); const [isApproved, setIsApproved] = useState(false); const resetForm = () => { setName(''); setEmail(''); setWebsite(''); setMessage(''); }; const submitHandler = async (e: SubmitEvent) => { e.preventDefault(); if (name && email && message && articleId) { const data = { author: name, authorEmail: email, authorUrl: website, content: message, parent: parentId, commentOn: articleId, mutationId: 'createComment', }; const createdComment = await createComment(data); if (createdComment.success) setIsSuccess(true); if (isSuccess) { resetForm(); if (createdComment.comment?.approved) setIsApproved(true); setTimeout(() => { setIsSuccess(false); setIsApproved(false); }, 8000); } } else { setIsSuccess(false); } }; const wrapperClasses = `${styles.wrapper} ${ isReply ? styles['wrapper--reply'] : '' }`; return (

{intl.formatMessage({ defaultMessage: 'Leave a comment', description: 'CommentForm: form title', })}