| 2633 |
stevensc |
1 |
import { axios } from "../../../utils";
|
| 1 |
www |
2 |
import React, { useEffect, useState } from "react";
|
|
|
3 |
import { useForm } from "react-hook-form";
|
|
|
4 |
import { connect } from "react-redux";
|
|
|
5 |
import styled from "styled-components";
|
|
|
6 |
import { addNotification } from "../../../redux/notification/notification.actions";
|
|
|
7 |
import FormErrorFeedback from "../../../shared/form-error-feedback/FormErrorFeedback";
|
|
|
8 |
import Spinner from "../../../shared/loading-spinner/Spinner";
|
|
|
9 |
|
|
|
10 |
const StyledSpinnerContainer = styled.div`
|
|
|
11 |
position: absolute;
|
|
|
12 |
left: 0;
|
|
|
13 |
top: 0;
|
|
|
14 |
width: 100%;
|
|
|
15 |
height: 100%;
|
|
|
16 |
background: rgba(255, 255, 255, 0.4);
|
|
|
17 |
display: flex;
|
|
|
18 |
justify-content: center;
|
|
|
19 |
align-items: center;
|
|
|
20 |
z-index: 300;
|
|
|
21 |
`;
|
|
|
22 |
|
|
|
23 |
const ChangePassword = (props) => {
|
|
|
24 |
// redux destructuring
|
|
|
25 |
const { addNotification } = props;
|
|
|
26 |
|
|
|
27 |
const {
|
|
|
28 |
register,
|
|
|
29 |
handleSubmit,
|
|
|
30 |
getValues,
|
|
|
31 |
errors,
|
|
|
32 |
reset,
|
|
|
33 |
formState: { isSubmitSuccessful },
|
|
|
34 |
} = useForm();
|
|
|
35 |
|
|
|
36 |
// states
|
|
|
37 |
const [loading, setLoading] = useState(false);
|
|
|
38 |
|
|
|
39 |
// Error password message
|
|
|
40 |
const [isErrorPassword, setIsErrorPassword] = useState(false);
|
|
|
41 |
const [isErrorConfirmation, setIsErrorConfirmation] = useState(false);
|
|
|
42 |
|
|
|
43 |
const handleOnSubmit = async (data) => {
|
|
|
44 |
setLoading(true);
|
|
|
45 |
|
|
|
46 |
let errorPassword = false;
|
|
|
47 |
|
|
|
48 |
const validPassword = new RegExp('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$^x%x*-]).{6,16}$');
|
| 2633 |
stevensc |
49 |
|
| 1 |
www |
50 |
// se verifica si la clave cumple la expresion regular
|
|
|
51 |
if (!validPassword.test(data.password)) {
|
|
|
52 |
setIsErrorPassword(true);
|
|
|
53 |
setTimeout(() => {
|
|
|
54 |
setIsErrorPassword(false);
|
|
|
55 |
}, 10000);
|
|
|
56 |
setLoading(false);
|
|
|
57 |
errorPassword = true;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
// se verifica si las dos claves son identicas
|
|
|
61 |
if (data.password !== data.confirmation) {
|
|
|
62 |
setIsErrorConfirmation(true);
|
|
|
63 |
setTimeout(() => {
|
|
|
64 |
setIsErrorConfirmation(false);
|
|
|
65 |
}, 10000);
|
|
|
66 |
setLoading(false);
|
|
|
67 |
errorPassword = true;
|
|
|
68 |
}
|
| 2633 |
stevensc |
69 |
|
|
|
70 |
if (!errorPassword) {
|
| 1 |
www |
71 |
const formData = new FormData();
|
|
|
72 |
Object.entries(data).map(([key, value]) => {
|
|
|
73 |
formData.append(key, value);
|
|
|
74 |
});
|
|
|
75 |
await axios
|
|
|
76 |
.post("/account-settings/password", formData)
|
|
|
77 |
.then((response) => {
|
|
|
78 |
const resData = response.data;
|
|
|
79 |
if (resData.success) {
|
|
|
80 |
addNotification({
|
|
|
81 |
style: "success",
|
|
|
82 |
msg: resData.data,
|
|
|
83 |
});
|
|
|
84 |
} else {
|
|
|
85 |
if (typeof resData.data === "object") {
|
|
|
86 |
resData.data;
|
|
|
87 |
Object.entries(resData.data).map(([key, value]) => {
|
|
|
88 |
setError(key, { type: "manual", message: value[0] });
|
|
|
89 |
});
|
|
|
90 |
} else {
|
|
|
91 |
const errorMessage =
|
|
|
92 |
typeof resData.data === "string"
|
|
|
93 |
? resData.data
|
|
|
94 |
: "Ha ocurrido un error, Por favor intente mas tarde";
|
|
|
95 |
addNotification({
|
|
|
96 |
style: "danger",
|
|
|
97 |
msg: errorMessage,
|
|
|
98 |
});
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
});
|
| 2633 |
stevensc |
102 |
setLoading(false);
|
| 1 |
www |
103 |
}
|
| 2633 |
stevensc |
104 |
|
| 1 |
www |
105 |
errorPassword = false;
|
|
|
106 |
};
|
|
|
107 |
|
|
|
108 |
useEffect(() => {
|
|
|
109 |
reset({ ...getValues });
|
|
|
110 |
}, [isSubmitSuccessful]);
|
|
|
111 |
|
|
|
112 |
return (
|
| 2633 |
stevensc |
113 |
<div className="settings-container">
|
|
|
114 |
<h2>Cambiar clave</h2>
|
| 1 |
www |
115 |
<form onSubmit={handleSubmit(handleOnSubmit)}>
|
|
|
116 |
<div className="cp-field">
|
|
|
117 |
<label htmlFor="password">Clave</label>
|
|
|
118 |
<div className="cpp-fiel">
|
|
|
119 |
<input
|
|
|
120 |
type="password"
|
|
|
121 |
name="password"
|
|
|
122 |
minLength="6"
|
|
|
123 |
maxLength="16"
|
|
|
124 |
id="password"
|
|
|
125 |
ref={register({
|
|
|
126 |
required: "Por favor ingrese la contraseña",
|
|
|
127 |
})}
|
|
|
128 |
pattern="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$^x%x*-]).{6,16}$"
|
|
|
129 |
title="La clave debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-"
|
|
|
130 |
/>
|
|
|
131 |
<i className="fa fa-lock"></i>
|
|
|
132 |
</div>
|
| 2633 |
stevensc |
133 |
{isErrorPassword && (
|
| 1 |
www |
134 |
<p className="text-danger">Disculpe, La clave debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-</p>
|
|
|
135 |
)}
|
|
|
136 |
{<FormErrorFeedback>{errors?.password?.message}</FormErrorFeedback>}
|
|
|
137 |
</div>
|
|
|
138 |
<div className="cp-field">
|
|
|
139 |
<label htmlFor="confirmation">Confirmación</label>
|
|
|
140 |
<div className="cpp-fiel">
|
|
|
141 |
<input
|
|
|
142 |
type="password"
|
|
|
143 |
name="confirmation"
|
|
|
144 |
minLength="6"
|
|
|
145 |
maxLength="16"
|
|
|
146 |
id="confirmation"
|
|
|
147 |
ref={register({
|
|
|
148 |
required: "Por favor ingrese la contraseña",
|
|
|
149 |
validate: (value) =>
|
|
|
150 |
value === getValues("password") ||
|
|
|
151 |
"La contraseña no coincide",
|
|
|
152 |
})}
|
|
|
153 |
pattern="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$^x%x*-]).{6,16}$"
|
|
|
154 |
title="La clave debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-"
|
|
|
155 |
/>
|
|
|
156 |
<i className="fa fa-lock"></i>
|
|
|
157 |
</div>
|
| 2633 |
stevensc |
158 |
{isErrorConfirmation && (
|
| 1 |
www |
159 |
<p className="text-danger">Disculpe, las claves tienen que coincidir</p>
|
|
|
160 |
)}
|
|
|
161 |
{
|
|
|
162 |
<FormErrorFeedback>
|
|
|
163 |
{errors?.confirmation?.message}
|
|
|
164 |
</FormErrorFeedback>
|
|
|
165 |
}
|
|
|
166 |
</div>
|
|
|
167 |
<div className="save-stngs pd2">
|
|
|
168 |
<ul>
|
|
|
169 |
<li>
|
|
|
170 |
<button type="submit" className="btn-save-basic">
|
|
|
171 |
Guardar
|
|
|
172 |
</button>
|
|
|
173 |
</li>
|
|
|
174 |
</ul>
|
|
|
175 |
</div>
|
|
|
176 |
{/* <!--save-stngs end--> */}
|
|
|
177 |
{/* <?php echo $this->form()->closeTag($form); ?> */}
|
|
|
178 |
</form>
|
|
|
179 |
{loading && (
|
|
|
180 |
<StyledSpinnerContainer>
|
|
|
181 |
<Spinner></Spinner>
|
|
|
182 |
</StyledSpinnerContainer>
|
|
|
183 |
)}
|
|
|
184 |
</div>
|
|
|
185 |
);
|
|
|
186 |
};
|
|
|
187 |
|
|
|
188 |
// const mapStateToProps = (state) => ({
|
|
|
189 |
|
|
|
190 |
// })
|
|
|
191 |
|
|
|
192 |
const mapDispatchToProps = {
|
|
|
193 |
addNotification: (notification) => addNotification(notification),
|
|
|
194 |
};
|
|
|
195 |
|
|
|
196 |
export default connect(null, mapDispatchToProps)(ChangePassword);
|