Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6509 stevensc 1
import React, { useEffect, useState, useRef } from 'react'
2
import { useForm } from 'react-hook-form'
3
import Recaptcha from 'react-recaptcha'
4
import { axios } from '../../../utils'
5
import FormErrorFeedback from '../../../../shared/form-error-feedback/FormErrorFeedback'
6
import CryptoJSAesJson from '../../../utils/crypto-js/cryptojs-aes-format'
7
import { useSelector } from 'react-redux'
8
 
9
const ResetPassword = () => {
10
  const { site_key, aes } = useSelector(({ auth }) => auth)
11
  const { register, handleSubmit, errors, setError, watch } = useForm({
12
    mode: 'all',
13
  })
14
 
15
  const reCaptchaToken = useRef('')
16
  const [isVerified, setIsVerified] = useState(false)
17
  const reCaptchaInstance = useRef()
18
 
19
  const loginVerifyCallbackHandler = (response) => {
20
    reCaptchaToken.current = response
21
    setIsVerified(true)
22
  }
23
 
24
  const loginExpiredCallbackHandler = () => {
25
    reCaptchaToken.current = ''
26
    setIsVerified(false)
27
  }
28
  const handleOnRecaptchaLoad = () => {
29
    reCaptchaToken.current = ''
30
  }
31
 
32
  const resetCaptcha = () => {
33
    reCaptchaInstance.current?.reset()
34
  }
35
 
36
  useEffect(() => {
37
    resetCaptcha()
38
  }, [])
39
 
40
  const submit = (data) => {
41
    const formData = new FormData()
42
 
43
    Object.entries(data).map(([key, value]) =>
44
      (key === 'confirmation' || key === 'password') && value
45
        ? formData.append(key, CryptoJSAesJson.encrypt(value, aes))
46
        : formData.append(key, value)
47
    )
48
    formData.append('captcha', reCaptchaToken.current)
49
 
50
    axios
51
      .post(window.location.href, formData)
52
      .then(({ data: response }) => {
53
        const { data, success } = response
54
        if (!success) {
55
          setError('password', {
56
            type: 'manual',
57
            message: typeof data === 'string' ? data : data.confirmation[0],
58
          })
59
          return
60
        }
61
        window.location.href = '/'
62
      })
63
      .catch((err) => {
64
        console.log('Error: ', err)
65
        throw new Error(err)
66
      })
67
      .finally(() => {
68
        formData.append('captcha', reCaptchaToken.current)
69
        resetCaptcha()
70
      })
71
  }
72
 
73
  return (
74
    <div className="sign_in_sec current">
75
      <div className="row">
76
        <div className="col-lg-12">
77
          <div className="login-sec">
78
            <div className="sign_in_sec current" id="tab-1">
79
              <h3>Reiniciar clave</h3>
80
              <form onSubmit={handleSubmit(submit)}>
81
                <div className="row">
82
                  <div className="col-lg-12 no-pdd">
83
                    <div className="inputContainer">
84
                      <div className="sn-field">
85
                        <input
86
                          type="password"
87
                          name="password"
88
                          ref={register({
89
                            required: 'Este campo es requerido',
90
                            pattern: {
91
                              value:
92
                                /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$^x%x*-]).{6,16}$/i,
93
                              message:
94
                                'Debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-',
95
                            },
96
                          })}
97
                          title="La clave debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-"
98
                          placeholder="Nueva clave"
99
                        />
100
                        <i className="la la-lock" />
101
                      </div>
102
                      {errors.password && (
103
                        <FormErrorFeedback>
104
                          {errors.password.message}
105
                        </FormErrorFeedback>
106
                      )}
107
                    </div>
108
                  </div>
109
                  <div className="col-lg-12 no-pdd">
110
                    <div className="inputContainer">
111
                      <div className="sn-field">
112
                        <input
113
                          type="password"
114
                          name="confirmation"
115
                          ref={register({
116
                            required: 'Este campo es requerido',
117
                            validate: (v) =>
118
                              v === watch('password') ||
119
                              'Disculpe, las claves tienen que coincidir',
120
                          })}
121
                          title="La clave debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-"
122
                          placeholder="Repita nueva clave"
123
                        />
124
                        <i className="la la-lock" />
125
                      </div>
126
                      {errors.confirmation && (
127
                        <FormErrorFeedback>
128
                          {errors.confirmation.message}
129
                        </FormErrorFeedback>
130
                      )}
131
                    </div>
132
                  </div>
133
                  <div className="col-lg-12 no-pdd">
134
                    <div className="sn-field">
135
                      <Recaptcha
136
                        sitekey={site_key}
137
                        verifyCallback={loginVerifyCallbackHandler}
138
                        verifyCallbackName="loginVerifyCallbackHandler"
139
                        expiredCallback={loginExpiredCallbackHandler}
140
                        expiredCallbackName="loginExpiredCallbackHandler"
141
                        ref={reCaptchaInstance}
142
                        render="explicit"
143
                        onloadCallback={handleOnRecaptchaLoad}
144
                        hl="es"
145
                      />
146
                    </div>
147
                  </div>
148
                  <div className="col-lg-12 no-pdd">
149
                    <button
150
                      type="submit"
151
                      value="submit"
152
                      id="btn-submit"
153
                      disabled={!isVerified}
154
                    >
155
                      Guardar
156
                    </button>
157
                  </div>
158
                </div>
159
              </form>
160
            </div>
161
          </div>
162
        </div>
163
      </div>
164
    </div>
165
  )
166
}
167
 
168
export default ResetPassword