aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Comment/Comment.tsx
Commit message (Collapse)AuthorAgeFilesLines
* refactor: use formatjs swc pluginArmand Philippot2022-03-231-0/+5
| | | | | I'm not able to configure SWC plugins in Next.js so to make it works, all translation must have an id.
* refactor: update graphql queries (#14)Armand Philippot2022-03-091-5/+8
| | | | | | | | | | | | | | | | | | * refactor: replace postBy query postBy is now deprecated in WPGraphQL v1.7 * refactor: update post comments query PostBy is deprecated and it is now possible to use the post ID to query comments. * refactor: update get topic by slug query topicBy is deprecated * refactor: update get thematic by slug query thematicBy is deprecated
* refactor: import comment form dynamically when reply to a commentArmand Philippot2022-03-011-9/+11
| | | | | The comment form is displayed only if an user click on the reply button so importing it dynamically should improve performances.
* refactor: replace script tags with next/script (#10)Armand Philippot2022-02-211-7/+6
| | | | | | | | * refactor: replace script tags with next/script Since next.js v12.1.0 some warnings was displayed because I was using some script tags. * build(deps): bump next-themes to v0.1.1
* chore: improve comment form user experienceArmand Philippot2022-02-141-8/+12
|
* fix: update comments list when a new comment is sendArmand Philippot2022-02-141-1/+1
| | | | | The comments list was static before. If an user posted a comment, even after it was approved, the comments list was keeping the old state.
* refactor(config): move config from config dir to utilsArmand Philippot2022-01-291-5/+5
|
* chore: replace lingui functions with react-intlArmand Philippot2022-01-291-7/+32
|
* chore: wrap dates with time tagArmand Philippot2022-01-251-11/+10
|
* chore: add structured data using schema.org and JSON-LDArmand Philippot2022-01-191-3/+39
| | | | I also added the featured image on single article.
* chore(comments): handle comment replyArmand Philippot2022-01-151-2/+29
|
* chore: improve comment sectionArmand Philippot2022-01-071-2/+6
| | | | I also adjust styles for all forms and primary buttons.
* chore: display comments as a treeArmand Philippot2021-12-271-7/+15
| | | | | | I was displaying comments without the parent/children link. Now, each child is displayed under its parent. I also remove the reply button for children to avoid too many child depth.
* chore: display comments list on postsArmand Philippot2021-12-171-0/+88
g.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
import Button from '@components/atoms/buttons/button';
import Form, { type FormProps } from '@components/atoms/forms/form';
import Heading, {
  type HeadingProps,
  type HeadingLevel,
} from '@components/atoms/headings/heading';
import Spinner from '@components/atoms/loaders/spinner';
import LabelledField from '@components/molecules/forms/labelled-field';
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 = Pick<FormProps, 'className'> & {
  /**
   * 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: (data: CommentFormData, reset: () => void) => Promise<void>;
  /**
   * The form title.
   */
  title?: string;
  /**
   * The form title alignment. Default: left.
   */
  titleAlignment?: HeadingProps['alignment'];
  /**
   * The title level. Default: 2.
   */
  titleLevel?: HeadingLevel;
};

const CommentForm: FC<CommentFormProps> = ({
  Notice,
  parentId,
  saveComment,
  title,
  titleAlignment,
  titleLevel = 2,
  ...props
}) => {
  const intl = useIntl();
  const [name, setName] = useState<string>('');
  const [email, setEmail] = useState<string>('');
  const [website, setWebsite] = useState<string>('');
  const [comment, setComment] = useState<string>('');
  const [isSubmitting, setIsSubmitting] = useState<boolean>(false);

  /**
   * Reset all the form fields.
   */
  const resetForm = () => {
    setName('');
    setEmail('');
    setWebsite('');
    setComment('');
    setIsSubmitting(false);
  };

  const nameLabel = intl.formatMessage({
    defaultMessage: 'Name:',
    description: 'CommentForm: name label',
    id: 'ZIrTee',
  });

  const emailLabel = intl.formatMessage({
    defaultMessage: 'Email:',
    description: 'CommentForm: email label',
    id: 'Bh7z5v',
  });

  const websiteLabel = intl.formatMessage({
    defaultMessage: 'Website:',
    description: 'CommentForm: website label',
    id: 'u41qSk',
  });

  const commentLabel = intl.formatMessage({
    defaultMessage: 'Comment:',
    description: 'CommentForm: comment label',
    id: 'A8hGaK',
  });

  const formTitle = intl.formatMessage({
    defaultMessage: 'Comment form',
    description: 'CommentForm: aria label',
    id: 'dz2kDV',
  });

  const formAriaLabel = title ? undefined : formTitle;
  const formId = 'comment-form-title';
  const formLabelledBy = title ? formId : undefined;

  /**
   * Handle form submit.
   */
  const submitHandler = () => {
    setIsSubmitting(true);
    saveComment({ comment, email, name, parentId, website }, resetForm).then(
      () => setIsSubmitting(false)
    );
  };

  return (
    <Form
      onSubmit={submitHandler}
      aria-label={formAriaLabel}
      aria-labelledby={formLabelledBy}
      {...props}
    >
      {title && (
        <Heading id={formId} level={titleLevel} alignment={titleAlignment}>
          {title}
        </Heading>
      )}
      <LabelledField
        type="text"
        id="commenter-name"
        name="commenter-name"
        label={nameLabel}
        required={true}
        value={name}
        setValue={setName}
        className={styles.field}
      />
      <LabelledField
        type="email"
        id="commenter-email"
        name="commenter-email"
        label={emailLabel}
        required={true}
        value={email}
        setValue={setEmail}
        className={styles.field}
      />
      <LabelledField
        type="text"
        id="commenter-website"
        name="commenter-website"
        label={websiteLabel}
        required={false}
        value={website}
        setValue={setWebsite}
        className={styles.field}
      />
      <LabelledField
        type="textarea"
        id="commenter-comment"
        name="commenter-comment"
        label={commentLabel}
        required={true}
        value={comment}
        setValue={setComment}
        className={styles.field}
      />
      <Button type="submit" kind="primary" className={styles.button}>
        {intl.formatMessage({
          defaultMessage: 'Publish',
          description: 'CommentForm: submit button',
          id: 'OL0Yzx',
        })}
      </Button>
      {isSubmitting && (
        <Spinner
          message={intl.formatMessage({
            defaultMessage: 'Submitting...',
            description: 'CommentForm: spinner message on submit',
            id: 'IY5ew6',
          })}
        />
      )}
      {Notice}
    </Form>
  );
};

export default CommentForm;