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

37.5% Statements 12/32
100% Branches 0/0
0% Functions 0/3
37.5% Lines 12/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 842x 1x   1x 1x 1x 1x 1x   1x 1x 1x                 1x                                                                                                           1x                  
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 * as React from 'react';
import { connect } from 'react-redux';
import { useBoolean } from '../../../customHooks';
import { IState } from '../../../types';
import { authSetAuthDataAction, authSetAuthStateAction } from '../actions';
import { forgotPasswordSubmit } from '../api';
import { selectUsername } from '../reducer';
 
interface IProps extends FormComponentProps {
  setAuthState: any;
  setAuthData: any;
  username: string;
  handleClose: () => void;
}
 
const ForgotPasswordSubmit = ({
  form,
  setAuthState,
  setAuthData,
  username,
  handleClose,
}: IProps) => {
  const isLoading = useBoolean(false);
  const handleSubmit = (e: any) => {
    e.preventDefault();
    form.validateFields(async (err: any, values: any) => {
      if (!err) {
        try {
          isLoading.setTrue();
          await forgotPasswordSubmit(username, values.code, values.password);
          message.success('Heslo bylo úspěšně změněno');
          form.resetFields();
          isLoading.setFalse();
          handleClose();
        } catch (e) {
          message.error(e.message);
        }
      }
    });
  };
 
  const { getFieldDecorator } = form;
  return (
    <Form onSubmit={handleSubmit}>
      <Form.Item>
        {getFieldDecorator('code', {
          rules: [
            {
              required: true,
              message:
                'Prosím zadejte bezpečnostní kód který jste obdrželi na email!',
            },
          ],
        })(<Input placeholder="Bezpečnostní kód" />)}
      </Form.Item>
      <Form.Item>
        {getFieldDecorator('password', {
          rules: [{ required: true, message: 'Prosím zadejte nové heslo!' }],
        })(<Input.Password placeholder="Heslo" />)}
      </Form.Item>
      <Form.Item>
        <Button type="primary" htmlType="submit" loading={isLoading.value}>
          Potvrdit
        </Button>
      </Form.Item>
    </Form>
  );
};
 
export default connect(
  (state: IState) => ({
    username: selectUsername(state),
  }),
  {
    setAuthState: authSetAuthStateAction,
    setAuthData: authSetAuthDataAction,
  },
)(Form.create()(ForgotPasswordSubmit));