Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6509 stevensc 1
import React, { useRef, useState, useEffect } from 'react'
2
import { axios } from '../../../utils'
3
import { Link } from 'react-router-dom'
4
import { useForm } from 'react-hook-form'
5
import { useSelector } from 'react-redux'
6
import { addNotification } from '../../../redux/notification/notification.actions'
7
import styled from 'styled-components'
8
import Recaptcha from 'react-recaptcha'
9
import CryptoJSAesJson from '../../../utils/crypto-js/cryptojs-aes-format'
10
 
11
import Spinner from '../../UI/Spinner'
12
import FormErrorFeedback from '../../UI/FormErrorFeedback'
13
 
14
const StyledCheck = styled.div`
15
  display: flex;
16
  flex-direction: column;
17
  justify-content: center;
18
  align-items: center;
19
  img {
20
    width: 100px;
21
    margin-bottom: 1rem;
22
  }
23
  p {
24
    text-align: center;
25
  }
26
`
27
const StyledSpinnerContainer = styled.div`
28
  position: absolute;
29
  left: 0;
30
  top: 0;
31
  width: 100%;
32
  height: 100%;
33
  background: rgba(255, 255, 255, 0.4);
34
  display: flex;
35
  justify-content: center;
36
  align-items: center;
37
  z-index: 300;
38
`
39
const ForgotPassword = () => {
40
  const { site_key, aes } = useSelector(({ auth }) => auth)
41
  const [forgotSent, setForgotSent] = useState(false)
42
  const [isLoading, setIsLoading] = useState(false)
43
  const [isVerified, setIsVerified] = useState(false)
44
 
45
  const reCaptchaInstance = useRef()
46
  const reCaptchaToken = useRef('')
47
 
48
  const { register, handleSubmit, setError, errors } = useForm({ mode: 'all' })
49
 
50
  const forgotPasswordVerifyCallbackHandler = (response) => {
51
    if (response) {
52
      reCaptchaToken.current = response
53
      setIsVerified(true)
54
    }
55
  }
56
 
57
  const forgotPasswordExpiredCallbackHandler = () => {
58
    reCaptchaToken.current = ''
59
    setIsVerified(false)
60
  }
61
 
62
  const onSubmitHandler = async (data) => {
63
    setIsLoading(true)
64
 
65
    const formData = new FormData()
66
    Object.entries(data).map(([key, value]) => {
67
      if (key === 'email' && value) {
68
        formData.append(key, CryptoJSAesJson.encrypt(value, aes))
69
        return
70
      }
71
 
72
      formData.append(key, value)
73
    })
74
    formData.append('captcha', reCaptchaToken.current)
75
 
76
    await axios
77
      .post('/forgot-password', formData)
78
      .then(({ data: response }) => {
79
        const { success, data } = response
80
        if (!success) {
81
          const resError = data
82
 
83
          if (typeof resError === 'object') {
84
            Object.entries(resError).map(([key, value]) => {
85
              setError(key, {
86
                message: Array.isArray(value) ? value[0] : value,
87
              })
88
            })
89
            return
90
          }
91
 
92
          addNotification({ style: 'danger', msg: resError })
93
        }
94
        reCaptchaInstance.current.reset()
95
        forgotPasswordExpiredCallbackHandler()
96
        setForgotSent(true)
97
 
98
        reCaptchaInstance.current.reset()
99
        loginExpiredCallbackHandler()
100
      })
101
      .finally(() => setIsLoading(false))
102
  }
103
 
104
  const handleOnRecaptchaLoad = () => {
105
    reCaptchaToken.current = ''
106
  }
107
 
108
  const loginExpiredCallbackHandler = () => {
109
    reCaptchaToken.current = ''
110
    setIsVerified(false)
111
  }
112
 
113
  useEffect(() => {
114
    reCaptchaInstance.current?.reset()
115
  }, [])
116
 
117
  if (forgotSent) {
118
    return (
119
      <StyledCheck>
120
        <img src="/images/check.png" alt="check" />
121
        <p>El enlace de recuperación fue enviado a su correo electrónico</p>
122
        <Link to="/signin">
123
          <button id="btn-submit" className="sign_in_sec_button">
124
            Entrar
125
          </button>
126
        </Link>
127
      </StyledCheck>
128
    )
129
  }
130
 
131
  return (
6510 stevensc 132
    <>
6509 stevensc 133
      <h3>Olvide mi Clave</h3>
134
      <form onSubmit={handleSubmit(onSubmitHandler)}>
6510 stevensc 135
        <div className="inputContainer">
136
          <div className="sn-field">
137
            <input
138
              type="email"
139
              name="email"
140
              placeholder="Correo electrónico"
141
              ref={register({
142
                required: 'Este campo es requerido',
143
                pattern: {
144
                  value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/i,
145
                  message:
146
                    'Debe ser una dirección de correo electrónico valida',
147
                },
148
              })}
149
            />
150
            <i className="la la-envelope" />
6509 stevensc 151
          </div>
6510 stevensc 152
          {errors.email && (
153
            <FormErrorFeedback>{errors.email.message}</FormErrorFeedback>
154
          )}
155
        </div>
156
        <div className="inputContainer">
157
          <div className="sn-field">
158
            <Recaptcha
159
              sitekey={site_key}
160
              verifyCallback={forgotPasswordVerifyCallbackHandler}
161
              verifyCallbackName="forgotPasswordVerifyCallbackHandler"
162
              expiredCallback={forgotPasswordExpiredCallbackHandler}
163
              expiredCallbackName="forgotPasswordExpiredCallbackHandler"
164
              ref={reCaptchaInstance}
165
              render="explicit"
166
              onloadCallback={handleOnRecaptchaLoad}
167
              hl="es"
168
            />
6509 stevensc 169
          </div>
170
        </div>
6510 stevensc 171
        <button type="submit" value="submit" disabled={!isVerified}>
172
          Nueva Clave
173
        </button>
6509 stevensc 174
      </form>
175
      {isLoading && (
176
        <StyledSpinnerContainer>
177
          <Spinner />
178
        </StyledSpinnerContainer>
179
      )}
6510 stevensc 180
    </>
6509 stevensc 181
  )
182
}
183
 
184
export default ForgotPassword