All files / web/modules/auth/components SignupForm.tsx

37.5% Statements 9/24
100% Branches 0/0
0% Functions 0/2
45% Lines 9/20

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 772x 1x   1x   1x 1x 1x 1x                   1x                                                                                                                 1x  
import { Form, Input, message, Modal } from 'antd';
import Alert from 'antd/lib/alert';
import { FormComponentProps } from 'antd/lib/form';
import * as React from 'react';
import { MutationFn, OperationVariables } from 'react-apollo';
import { Groups } from '../../../../shared';
import { useBoolean } from '../../../customHooks';
import Link from '../../components/Link';
import RULES from '../../form/rules';
 
interface IProps extends FormComponentProps {
  signup: MutationFn<
    { username: string; email: string; role: string },
    OperationVariables
  >;
  isLoading: boolean;
}
 
const SignupForm = ({ signup, isLoading, form }: IProps) => {
  const isVisible = useBoolean(false);
 
  const handleSubmit = () => {
    form.validateFieldsAndScroll(async (err, values) => {
      if (!err) {
        try {
          const variables = { ...values, role: Groups.registered };
          const createdUser = await signup({ variables });
 
          if (createdUser) {
            message.success(
              'Registrace proběhla úspěšně. Informace pro přihlášení vám byly odeslány na email.',
            );
            form.resetFields();
            isVisible.setFalse();
          }
        } catch (e) {
          message.error(e.message);
        }
      }
    });
  };
 
  return (
    <React.Fragment>
      <Link onClick={isVisible.setTrue}>Registrovat</Link>
 
      <Modal
        title="Registrace"
        visible={isVisible.value}
        onOk={handleSubmit}
        cancelText="Zrušit"
        okText="Potvrdit"
        onCancel={isVisible.setFalse}
        confirmLoading={isLoading}
      >
        <Alert
          message="Pro registraci si zvolte uživatelské jméno a vyplňte Váš email, na který Vám budou zaslány údaje pro první přihlášení."
          type="info"
          showIcon={true}
        />
        <Form>
          <Form.Item label="Uživatelské jméno">
            {form.getFieldDecorator('username', RULES.SIGNUP_USERNAME)(
              <Input />,
            )}
          </Form.Item>
          <Form.Item label="E-mail">
            {form.getFieldDecorator('email', RULES.SIGNUP_EMAIL)(<Input />)}
          </Form.Item>
        </Form>
      </Modal>
    </React.Fragment>
  );
};
 
export default Form.create()(SignupForm);