Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3684 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3682 stevensc 1
/* eslint-disable react/prop-types */
1 www 2
import React from "react";
3
import { useRef, useState, useEffect } from "react";
4
import { useForm } from "react-hook-form";
5
import styled from "styled-components";
3682 stevensc 6
import { axios } from "../../../../utils";
1 www 7
import FormErrorFeedback from "../../../../shared/form-error-feedback/FormErrorFeedback";
8
import Recaptcha from "react-recaptcha";
9
import Spinner from "../../../../shared/loading-spinner/Spinner";
3682 stevensc 10
import { Link } from 'react-router-dom'
662 steven 11
import CryptoJSAesJson from "../../../../utils/crypto-js/cryptojs-aes-format";
1 www 12
 
13
const StyledCheck = styled.div`
14
  display: flex;
15
  flex-direction: column;
16
  justify-content: center;
17
  align-items: center;
18
  img {
19
    width: 100px;
20
    margin-bottom: 1rem;
21
  }
22
  p {
23
    text-align: center;
24
  }
25
`;
26
const StyledSpinnerContainer = styled.div`
27
  position: absolute;
28
  left: 0;
29
  top: 0;
30
  width: 100%;
31
  height: 100%;
32
  background: rgba(255, 255, 255, 0.4);
33
  display: flex;
34
  justify-content: center;
35
  align-items: center;
36
  z-index: 300;
37
`;
3682 stevensc 38
const ForgotPassword = ({ captchaKey, addNotification, aes }) => {
1 www 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
4752 stevensc 50
  const { register, handleSubmit, setError, errors } = useForm({ mode: 'all' });
1 www 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
 
3682 stevensc 68
  const onSubmitHandler = async (data) => {
1 www 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
  };
3682 stevensc 108
  const handleOnRecaptchaLoad = () => reCaptchaToken.current = ""
3684 stevensc 109
  const loginExpiredCallbackHandler = () => {
110
    reCaptchaToken.current = "";
111
    setIsVerified(false);
112
  };
1 www 113
 
114
  if (forgotSent) {
115
    return (
116
      <StyledCheck>
117
        <img src="/images/check.png" alt="check" />
118
        <p>El enlace de recuperación fue enviado a su correo electrónico</p>
119
        <Link to="/signin">
120
          <button id="btn-submit" className="sign_in_sec_button">
121
            Entrar
122
          </button>
123
        </Link>
124
      </StyledCheck>
125
    );
126
  }
3683 stevensc 127
 
1 www 128
  return (
129
    <React.Fragment>
130
      <h3>Olvide mi Clave</h3>
131
      <form onSubmit={handleSubmit(onSubmitHandler)}>
132
        <div className="row">
133
          <div className="col-lg-12 no-pdd">
134
            <div className="inputContainer">
135
              <div className="sn-field">
136
                <input
137
                  type="email"
138
                  name="email"
139
                  placeholder="Correo electrónico"
140
                  ref={register({
141
                    required: "Este campo es requerido",
3682 stevensc 142
                    pattern: {
143
                      value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/i,
144
                      message: "Debe ser una dirección de correo electrónico valida"
145
                    }
1 www 146
                  })}
147
                />
3682 stevensc 148
                <i className="la la-envelope" />
1 www 149
              </div>
3682 stevensc 150
              {errors.email && <FormErrorFeedback>{errors.email.message}</FormErrorFeedback>}
1 www 151
            </div>
152
          </div>
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
          <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>
3682 stevensc 177
      {isLoading &&
1 www 178
        <StyledSpinnerContainer>
179
          <Spinner />
3682 stevensc 180
        </StyledSpinnerContainer>}
1 www 181
    </React.Fragment>
182
  );
183
};
184
 
185
export default ForgotPassword;