All files / web/modules/comment/create CreateCommentForm.tsx

0% Statements 0/33
0% Branches 0/2
0% Functions 0/2
0% Lines 0/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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                                                                                                                                                                                               
import Form from 'antd/lib/form';
import { FormComponentProps } from 'antd/lib/form/Form';
import Icon from 'antd/lib/icon';
import Input from 'antd/lib/input';
import message from 'antd/lib/message';
import Modal from 'antd/lib/modal';
import * as React from 'react';
import styled from 'styled-components';
import { IArticle } from '../../../../shared';
import { useBoolean } from '../../../customHooks';
import Button from '../../components/Button';
 
interface IProps extends FormComponentProps {
  createComment: any;
  loading: boolean;
  article: IArticle;
  username: string;
  userSub: string;
}
 
const Link = styled.div`
  display: flex;
  justify-content: center;
  padding: 10px;
`;
 
const CreateCommentForm = ({
  form,
  createComment,
  loading,
  article,
  userSub,
  username,
}: IProps) => {
  const isVisible = useBoolean(false);
 
  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    form.validateFieldsAndScroll(async (err: any, values: any) => {
      if (!err) {
        try {
          const variables = {
            data: {
              ...values,
              username,
              userSub,
              articleId: article._id,
            },
          };
 
          const createdComment = await createComment({ variables });
 
          if (createdComment) {
            message.success('Komentář úspěšně přidán');
            form.resetFields();
            isVisible.setFalse();
          }
        } catch (e) {
          message.error(e.message);
          form.resetFields();
          isVisible.setFalse();
        }
      }
    });
  };
 
  return (
    <React.Fragment>
      <Link>
        <Button onClick={isVisible.setTrue}>
          <Icon type="plus" style={{ marginRight: '5px' }} />
          Přidat komentář
        </Button>
      </Link>
 
      <Modal
        title="Nový komentář"
        visible={isVisible.value}
        onOk={handleSubmit}
        okText="Uložit"
        onCancel={isVisible.setFalse}
        cancelText="Zrušit"
        confirmLoading={loading}
      >
        <Form onSubmit={handleSubmit}>
          <Form.Item label="Text">
            {form.getFieldDecorator('text')(<Input.TextArea />)}
          </Form.Item>
        </Form>
      </Modal>
    </React.Fragment>
  );
};
 
export default Form.create()(CreateCommentForm);