Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3694 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useRef, useState, useEffect } from 'react';
2
import { Link } from 'react-router-dom';
3
import { useDispatch, useSelector } from 'react-redux';
4
import { useForm } from 'react-hook-form';
5
import { styled, Typography } from '@mui/material';
6
import Mail from '@mui/icons-material/Mail';
7
import Recaptcha from 'react-recaptcha';
8
 
9
import { axios } from '@utils';
10
import { addNotification } from '@store/notification/notification.actions';
11
import CryptoJSAesJson from '@utils/crypto-js/cryptojs-aes-format';
12
 
13
import Form from '@components/common/form';
14
import Spinner from '@components/UI/Spinner';
15
import Input from '@components/UI/inputs/Input';
16
import Button from '@components/UI/buttons/Buttons';
17
 
18
const StyledCheck = styled('div')`
19
  display: flex;
20
  flex-direction: column;
21
  justify-content: center;
22
  align-items: center;
23
  img {
24
    width: 100px !important;
25
    margin-bottom: 1rem !important;
26
  }
27
  p {
28
    text-align: center;
29
  }
30
`;
31
 
32
const ForgotPassword = () => {
33
  const { site_key, aes } = useSelector(({ auth }) => auth);
34
  const [forgotSent, setForgotSent] = useState(false);
35
  const [isLoading, setIsLoading] = useState(false);
36
  const [isVerified, setIsVerified] = useState(false);
37
  const reCaptchaInstance = useRef(null);
38
  const reCaptchaToken = useRef('');
39
  const dispatch = useDispatch();
40
 
41
  const {
42
    handleSubmit,
43
    control,
44
    formState: { errors }
45
  } = useForm({ mode: 'all' });
46
 
47
  const forgotPasswordVerifyCallbackHandler = (response) => {
48
    if (response) {
49
      reCaptchaToken.current = response;
50
      setIsVerified(true);
51
    }
52
  };
53
 
54
  const forgotPasswordExpiredCallbackHandler = () => {
55
    reCaptchaToken.current = '';
56
    setIsVerified(false);
57
  };
58
 
59
  const handleOnRecaptchaLoad = () => {
60
    reCaptchaToken.current = '';
61
  };
62
 
63
  const loginExpiredCallbackHandler = () => {
64
    reCaptchaToken.current = '';
65
    setIsVerified(false);
66
  };
67
 
68
  const onSubmitHandler = handleSubmit(async ({ email }) => {
69
    setIsLoading(true);
70
    const formData = new FormData();
71
 
72
    formData.append('email', CryptoJSAesJson.encrypt(email, aes));
73
    formData.append('captcha', reCaptchaToken.current);
74
 
75
    axios
76
      .post('/forgot-password', formData)
77
      .then((response) => {
78
        const { success, data } = response.data;
79
 
80
        if (!success) {
81
          throw new Error(data);
82
        }
83
 
84
        reCaptchaInstance.current.reset();
85
        forgotPasswordExpiredCallbackHandler();
86
        loginExpiredCallbackHandler();
87
        setForgotSent(true);
88
      })
89
      .catch((err) => {
90
        console.log(`Error: ${err}`);
91
        dispatch(addNotification({ style: 'danger', msg: err.message }));
92
      })
93
      .finally(() => setIsLoading(false));
94
  });
95
 
96
  useEffect(() => {
97
    reCaptchaInstance.current?.reset();
98
  }, []);
99
 
100
  if (forgotSent) {
101
    return (
102
      <StyledCheck>
103
        <img src='/images/check.png' alt='check' />
104
 
105
        <p>El enlace de recuperación fue enviado a su correo electrónico</p>
106
 
107
        <Link to='/signin'>
108
          <Button color='secondary' type='button'>
109
            Volver a Iniciar Sesión
110
          </Button>
111
        </Link>
112
      </StyledCheck>
113
    );
114
  }
115
 
116
  return (
117
    <Form onSubmit={onSubmitHandler}>
118
      {isLoading && <Spinner absolute />}
119
 
120
      <Typography variant='h3'>Olvide mi Clave</Typography>
121
 
122
      <Input
123
        type='email'
124
        name='email'
125
        placeholder='Correo electrónico'
126
        icon={<Mail />}
127
        control={control}
128
        rules={{
129
          required: 'Este campo es requerido',
130
          pattern: {
131
            value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,}$/i,
132
            message: 'Debe ser una dirección de correo electrónico valida'
133
          }
134
        }}
135
        error={errors.email?.message}
136
      />
137
 
138
      <Recaptcha
139
        sitekey={site_key}
140
        verifyCallback={forgotPasswordVerifyCallbackHandler}
141
        verifyCallbackName='forgotPasswordVerifyCallbackHandler'
142
        expiredCallback={forgotPasswordExpiredCallbackHandler}
143
        expiredCallbackName='forgotPasswordExpiredCallbackHandler'
144
        ref={reCaptchaInstance}
145
        render='explicit'
146
        onloadCallback={handleOnRecaptchaLoad}
147
        hl='es'
148
      />
149
 
150
      <Button color='secondary' type='submit' disabled={!isVerified}>
151
        Nueva Clave
152
      </Button>
153
    </Form>
154
  );
155
};
156
 
157
export default ForgotPassword;