Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2802 | Ir a la última revisión | | Ultima modificación | Ver Log |

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