All files / web/modules/components LoginButton.tsx

46.88% Statements 15/32
0% Branches 0/8
0% Functions 0/5
50% Lines 15/30

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 851x 1x 1x 1x   1x 1x 1x 1x 1x 1x             1x 1x             1x                         1x                                                                           1x                
import Modal from 'antd/lib/modal';
import * as React from 'react';
import { connect } from 'react-redux';
import { useBoolean } from '../../customHooks';
import { IState } from '../../types';
import { authSetAuthStateAction } from '../auth/actions';
import ForgotPassword from '../auth/components/ForgotPassword';
import ForgotPasswordSubmit from '../auth/components/ForgotPasswordSubmit';
import Login from '../auth/components/Login';
import SetNewPassword from '../auth/components/SetNewPassword';
import {
  AUTH_STATE,
  AUTH_STATE_DEFAULT,
  AUTH_STATE_FORGOT_PASSWORD,
  AUTH_STATE_FORGOT_PASSWORD_SUBMIT,
  AUTH_STATE_SET_NEW_PASSWORD,
} from '../auth/constants';
import { selectAuthState } from '../auth/reducer';
import Button from './Button';
 
interface IProps {
  authState: AUTH_STATE;
  setAuthState: any;
}
 
const getTitle = (authState: AUTH_STATE) => {
  switch (authState) {
    case AUTH_STATE_FORGOT_PASSWORD:
      return 'Zapomenuté heslo';
    case AUTH_STATE_FORGOT_PASSWORD_SUBMIT:
      return 'Zapomenuté heslo - potvrzení';
    case AUTH_STATE_SET_NEW_PASSWORD:
      return 'Nastavení nového hesla';
    default:
      return 'Přihlášení';
  }
};
 
const LoginButton = ({ authState, setAuthState }: IProps) => {
  const isVisible = useBoolean(false);
 
  const handleCancel = () => {
    isVisible.setFalse();
    setAuthState(AUTH_STATE_DEFAULT);
  };
 
  const getContent = () => {
    switch (authState) {
      case AUTH_STATE_FORGOT_PASSWORD:
        return <ForgotPassword />;
      case AUTH_STATE_FORGOT_PASSWORD_SUBMIT:
        return <ForgotPasswordSubmit handleClose={handleCancel} />;
      case AUTH_STATE_SET_NEW_PASSWORD:
        return <SetNewPassword />;
      default:
        return <Login />;
    }
  };
 
  return (
    <React.Fragment>
      <Button onClick={isVisible.setTrue}>Přihlásit se</Button>
 
      <Modal
        title={getTitle(authState)}
        visible={isVisible.value}
        onCancel={handleCancel}
        destroyOnClose={true}
        footer={null}
      >
        {getContent()}
      </Modal>
    </React.Fragment>
  );
};
 
export default connect(
  (state: IState) => ({
    authState: selectAuthState(state),
  }),
  {
    setAuthState: authSetAuthStateAction,
  },
)(LoginButton);