summaryrefslogtreecommitdiffstats
path: root/src/components/MetaItems
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/MetaItems')
0 files changed, 0 insertions, 0 deletions
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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<HTMLInputElement>
) => {
  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 (
    <div className={wrapperClasses}>
      <h2 className={styles.title}>
        {intl.formatMessage({
          defaultMessage: 'Leave a comment',
          description: 'CommentForm: form title',
        })}
      </h2>
      <Form
        submitHandler={submitHandler}
        modifier={isReply ? 'centered' : undefined}
      >
        <FormItem>
          <Input
            id="commenter-name"
            name="commenter-name"
            label={intl.formatMessage({
              defaultMessage: 'Name',
              description: 'CommentForm: Name field label',
            })}
            required={true}
            value={name}
            setValue={setName}
            ref={ref}
          />
        </FormItem>
        <FormItem>
          <Input
            id="commenter-email"
            name="commenter-email"
            label={intl.formatMessage({
              defaultMessage: 'Email',
              description: 'CommentForm: Email field label',
            })}
            required={true}
            value={email}
            setValue={setEmail}
          />
        </FormItem>
        <FormItem>
          <Input
            id="commenter-website"
            name="commenter-website"
            label={intl.formatMessage({
              defaultMessage: 'Website',
              description: 'CommentForm: Website field label',
            })}
            value={website}
            setValue={setWebsite}
          />
        </FormItem>
        <FormItem>
          <TextArea
            id="commenter-message"
            name="commenter-message"
            label={intl.formatMessage({
              defaultMessage: 'Comment',
              description: 'CommentForm: Comment field label',
            })}
            value={message}
            setValue={setMessage}
            required={true}
          />
        </FormItem>
        <FormItem>
          <ButtonSubmit>
            {intl.formatMessage({
              defaultMessage: 'Send',
              description: 'CommentForm: Send button',
            })}
          </ButtonSubmit>
        </FormItem>
        {isSuccess && !isApproved && (
          <Notice type="success">
            {intl.formatMessage({
              defaultMessage:
                'Thanks for your comment! It is now awaiting moderation.',
              description: 'CommentForm: Comment sent success message',
            })}
          </Notice>
        )}
      </Form>
    </div>
  );
};

export default forwardRef(CommentForm);