All files / web/modules/admin/category/create CreateCategoryForm.tsx

0% Statements 0/26
100% Branches 0/0
0% Functions 0/2
0% Lines 0/22

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                                                                                                                                 
import Button from 'antd/lib/button';
import Form from 'antd/lib/form';
import { FormComponentProps } from 'antd/lib/form/Form';
import Input from 'antd/lib/input';
import message from 'antd/lib/message';
import Modal from 'antd/lib/modal';
import * as React from 'react';
import { MutationFn, OperationVariables } from 'react-apollo';
import { useBoolean } from '../../../../customHooks';
import RULES from '../../../form/rules';
 
interface IProps extends FormComponentProps {
  createCategory: MutationFn<{ title: string }, OperationVariables>;
  isLoading: boolean;
}
 
const CreateCategoryForm = ({ createCategory, isLoading, form }: IProps) => {
  const isVisible = useBoolean(false);
 
  const handleSubmit = () => {
    form.validateFieldsAndScroll(async (err, values) => {
      if (!err) {
        try {
          const createdUser = await createCategory({ variables: values });
 
          if (createdUser) {
            message.success('Kategorie vytvořena');
            form.resetFields();
            isVisible.setFalse();
          }
        } catch (e) {
          message.error(e.message);
          form.resetFields();
          isVisible.setFalse();
        }
      }
    });
  };
 
  return (
    <React.Fragment>
      <Button icon="add" onClick={isVisible.setTrue} style={{ margin: 10 }}>
        Vytvořit kategorii
      </Button>
 
      <Modal
        title="Nová kategorie"
        visible={isVisible.value}
        onOk={handleSubmit}
        okText="Uložit"
        onCancel={isVisible.setFalse}
        confirmLoading={isLoading}
      >
        <Form>
          <Form.Item label="Název">
            {form.getFieldDecorator('title', RULES.CATEGORY_TITLE)(<Input />)}
          </Form.Item>
        </Form>
      </Modal>
    </React.Fragment>
  );
};
 
export default Form.create()(CreateCategoryForm);