Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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