Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2865 | Rev 3190 | Ir a la última revisión | | Comparar con el anterior | 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'
2865 stevensc 5
import { styled } from '@mui/material'
6
import { Mail } from '@mui/icons-material'
2630 stevensc 7
import Recaptcha from 'react-recaptcha'
8
 
2865 stevensc 9
import { axios } from '@utils'
10
import CryptoJSAesJson from '@utils/crypto-js/cryptojs-aes-format'
11
import { addNotification } from '@store/notification/notification.actions'
2630 stevensc 12
 
2865 stevensc 13
import Input from '@components/UI/inputs/Input'
2860 stevensc 14
import Button from '@components/UI/buttons/Buttons'
2865 stevensc 15
import Spinner from '@components/UI/Spinner'
16
import AuthForm from '@components/auth/AuthForm'
2630 stevensc 17
 
2865 stevensc 18
const StyledCheck = styled('div')`
2630 stevensc 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
 
2865 stevensc 32
const StyledSpinnerContainer = styled('div')`
2630 stevensc 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
 
2802 stevensc 54
  const {
55
    handleSubmit,
56
    control,
57
    formState: { errors }
58
  } = useForm({ mode: 'all' })
2630 stevensc 59
 
60
  const forgotPasswordVerifyCallbackHandler = (response) => {
61
    if (response) {
62
      reCaptchaToken.current = response
63
      setIsVerified(true)
64
    }
65
  }
66
 
67
  const forgotPasswordExpiredCallbackHandler = () => {
68
    reCaptchaToken.current = ''
69
    setIsVerified(false)
70
  }
71
 
72
  const handleOnRecaptchaLoad = () => {
73
    reCaptchaToken.current = ''
74
  }
75
 
76
  const loginExpiredCallbackHandler = () => {
77
    reCaptchaToken.current = ''
78
    setIsVerified(false)
79
  }
80
 
81
  const onSubmitHandler = handleSubmit(async ({ email }) => {
82
    setIsLoading(true)
83
    const formData = new FormData()
84
 
85
    formData.append('email', CryptoJSAesJson.encrypt(email, aes))
86
    formData.append('captcha', reCaptchaToken.current)
87
 
88
    axios
89
      .post('/forgot-password', formData)
90
      .then(({ data: response }) => {
91
        const { success, data } = response
92
 
93
        if (!success) {
94
          throw new Error(data)
95
        }
96
 
97
        reCaptchaInstance.current.reset()
98
        forgotPasswordExpiredCallbackHandler()
99
        loginExpiredCallbackHandler()
100
        setForgotSent(true)
101
      })
102
      .catch((err) => {
103
        console.log(`Error: ${err}`)
104
        dispatch(addNotification({ style: 'danger', msg: err.message }))
105
      })
106
      .finally(() => setIsLoading(false))
107
  })
108
 
109
  useEffect(() => {
110
    reCaptchaInstance.current?.reset()
111
  }, [])
112
 
113
  if (forgotSent) {
114
    return (
115
      <StyledCheck>
116
        <img src='/images/check.png' alt='check' />
117
 
118
        <p>El enlace de recuperación fue enviado a su correo electrónico</p>
119
 
120
        <Link to='/signin'>
3156 stevensc 121
          <Button color='secondary' type='button'>
2630 stevensc 122
            Volver a Iniciar Sesión
123
          </Button>
124
        </Link>
125
      </StyledCheck>
126
    )
127
  }
128
 
129
  return (
130
    <AuthForm onSubmit={onSubmitHandler}>
131
      <h3>Olvide mi Clave</h3>
132
 
133
      <Input
134
        control={control}
135
        name='email'
136
        rules={{
137
          required: 'Este campo es requerido',
138
          pattern: {
139
            value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/i,
140
            message: 'Debe ser una dirección de correo electrónico valida'
141
          }
142
        }}
143
        placeholder='Correo electrónico'
144
        type='email'
145
        error={errors.email?.message}
2865 stevensc 146
        icon={<Mail />}
2630 stevensc 147
      />
148
 
149
      <Recaptcha
150
        sitekey={site_key}
151
        verifyCallback={forgotPasswordVerifyCallbackHandler}
152
        verifyCallbackName='forgotPasswordVerifyCallbackHandler'
153
        expiredCallback={forgotPasswordExpiredCallbackHandler}
154
        expiredCallbackName='forgotPasswordExpiredCallbackHandler'
155
        ref={reCaptchaInstance}
156
        render='explicit'
157
        onloadCallback={handleOnRecaptchaLoad}
158
        hl='es'
159
      />
160
 
3156 stevensc 161
      <Button color='secondary' type='submit' disabled={!isVerified}>
2630 stevensc 162
        Nueva Clave
163
      </Button>
164
 
165
      {isLoading && (
166
        <StyledSpinnerContainer>
167
          <Spinner />
168
        </StyledSpinnerContainer>
169
      )}
170
    </AuthForm>
171
  )
172
}
173
 
174
export default ForgotPassword