Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3719 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React from 'react';
2
import { useNavigate, Link } from 'react-router-dom';
3
import { useDispatch, useSelector } from 'react-redux';
4
import { Controller, useForm } from 'react-hook-form';
5
import { Button, Typography } from '@mui/material';
6
import Mail from '@mui/icons-material/Mail';
7
import Lock from '@mui/icons-material/Lock';
8
 
9
import { useMobile } from '@hooks';
10
import { asyncLogin } from '@store/auth/auth.actions';
11
import { addNotification } from '@store/notification/notification.actions';
12
import CryptoJSAesJson from '@utils/crypto-js/cryptojs-aes-format';
13
 
14
import Captcha from './captcha';
15
import Row from '@components/common/Row';
16
import Form from '@components/common/form';
17
import Input from '@components/UI/inputs/Input';
18
import CheckboxInput from '@components/UI/inputs/Checkbox';
19
import LoadingWrapper from '@components/common/loading-wrapper';
20
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback';
21
 
3740 stevensc 22
const ENV = import.meta.env.VITE_ENVIROMENT;
23
 
3719 stevensc 24
export default function LoginForm() {
25
  const navigate = useNavigate();
26
  const dispatch = useDispatch();
27
 
28
  const { aes } = useSelector((state) => state.auth);
29
  const isMobile = useMobile();
30
 
31
  const {
32
    formState: { errors, isSubmitting, isValid },
33
    control,
34
    handleSubmit,
35
    reset
36
  } = useForm({ mode: 'all' });
37
 
38
  const onSubmitHandler = handleSubmit(async ({ email, password, remember, captcha }) => {
39
    try {
40
      const response = await dispatch(
41
        asyncLogin({
42
          email: CryptoJSAesJson.encrypt(email, aes),
43
          password: CryptoJSAesJson.encrypt(password, aes),
44
          captcha,
45
          remember: remember ? 1 : 0
46
        })
47
      );
48
      reset();
49
 
50
      isMobile ? navigate('/apps-navigation') : navigate(response.redirect);
51
    } catch (error) {
52
      dispatch(addNotification({ style: 'danger', msg: error.message }));
53
    }
54
  });
55
 
56
  return (
57
    <Form onSubmit={onSubmitHandler}>
58
      <LoadingWrapper loading={isSubmitting} displayChildren></LoadingWrapper>
59
 
60
      <Typography variant='h3'>Entrar</Typography>
61
 
62
      <Input
63
        type='email'
64
        name='email'
65
        icon={<Mail />}
66
        placeholder='Correo electrónico'
67
        rules={{
68
          required: 'Este campo es requerido',
69
          pattern: {
70
            value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,}$/i,
71
            message: 'Debe ser una dirección de correo electrónico valida'
72
          },
73
          maxLength: {
74
            value: 64,
75
            message: 'Debe ser menor a 64 caracteres'
76
          }
77
        }}
78
        control={control}
79
        error={errors.email?.message}
80
      />
81
 
82
      <Input
83
        type='password'
84
        name='password'
85
        icon={<Lock />}
86
        placeholder='Clave'
87
        control={control}
88
        rules={{
89
          required: 'Este campo es requerido',
90
          pattern: {
91
            value: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$^x%x*-]).{6,16}$/i,
92
            message:
93
              'Debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-'
94
          }
95
        }}
96
        error={errors.password?.message}
97
      />
98
 
99
      <CheckboxInput control={control} name='remember' label='Recuerdame' />
100
 
3740 stevensc 101
      {ENV === 'production' && (
102
        <Controller
103
          name='captcha'
104
          control={control}
105
          render={({ field: { ref, onChange } }) => (
106
            <>
107
              <Captcha instanceRef={ref} onVerify={onChange} onExpired={onChange} />
108
              {errors.captcha && <FormErrorFeedback>{errors.captcha.message}</FormErrorFeedback>}
109
            </>
110
          )}
111
        />
112
      )}
3719 stevensc 113
 
114
      <Row>
115
        <Link to='/forgot-password'>¿Has olvidado tu contraseña?</Link>
116
        <Link to='/signup'>¿No tienes cuenta?</Link>
117
      </Row>
118
 
119
      <Button color='secondary' type='submit' disabled={!isValid}>
120
        Entrar
121
      </Button>
122
    </Form>
123
  );
124
}