Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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