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

40% Statements 14/35
100% Branches 0/0
0% Functions 0/3
41.18% Lines 14/34

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 852x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x               1x                                                                                                             1x                  
import Button from 'antd/lib/button';
import Form, { FormComponentProps } from 'antd/lib/form';
import Icon from 'antd/lib/icon';
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 { setNewPassword } from '../api';
import { AUTH_STATE_AUTHENTICATED } from '../constants';
import { IAuthData, selectAuthData } from '../reducer';
 
interface IProps extends FormComponentProps {
  setAuthState: any;
  setAuthData: any;
  authData: IAuthData;
}
 
const SetNewPassword = ({
  form,
  authData,
  setAuthData,
  setAuthState,
}: IProps) => {
  const isLoading = useBoolean(false);
 
  const handleSubmit = (e: any) => {
    e.preventDefault();
    form.validateFields(async (err: any, values: any) => {
      if (!err) {
        try {
          isLoading.setTrue();
          const data = await setNewPassword(authData, values.password);
          await setAuthData(data);
          setAuthState(AUTH_STATE_AUTHENTICATED);
          message.success('Heslo úspěšně nastaveno');
        } catch (e) {
          if (e.code === 'InvalidPasswordException') {
            message.info(
              'Heslo musí být alespoň 8 znaků dlouhé a musí obsahovat čísla',
            );
          } else {
            message.error(e.message);
          }
        } finally {
          isLoading.setFalse();
        }
      }
    });
  };
 
  const { getFieldDecorator } = form;
  return (
    <Form onSubmit={handleSubmit}>
      <Form.Item>
        {getFieldDecorator('password', {
          rules: [{ required: true, message: 'Prosím zadejte nové heslo!' }],
        })(
          <Input.Password
            prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
            placeholder="Nové Heslo"
          />,
        )}
      </Form.Item>
      <Form.Item>
        <Button type="primary" htmlType="submit" loading={isLoading.value}>
          Potvrdit
        </Button>
      </Form.Item>
    </Form>
  );
};
 
export default connect(
  (state: IState) => ({
    authData: selectAuthData(state),
  }),
  {
    setAuthState: authSetAuthStateAction,
    setAuthData: authSetAuthDataAction,
  },
)(Form.create()(SetNewPassword));