import React, { useState } from "react"; import { apiRequest } from "@api/request"; import { useNotification } from "@hooks/useNotification"; import { Loader } from "@components/Common/Loader/Loader"; import ModalLayout from "@components/Common/ModalLayout/ModalLayout"; import arrow from "assets/icons/arrows/arrowCalendar.png"; import close from "assets/icons/close.png"; import "./modalResetPassword.scss"; export const ModalResetPassword = ({ active, setActive }) => { const [step, setStep] = useState(false); const [inputsValue, setInputsValue] = useState({ email: "", token: "", password: "", }); const [inputsError, setInputsError] = useState({ email: false, password: false, token: false, }); const validateEmail = (email) => { // регулярное выражение для проверки email const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; // возвращаем true, если email проходит проверку, и false, если нет return re.test(email); }; const [loader, setLoader] = useState(false); const resetInputsValue = () => { setInputsValue({ email: "", token: "", password: "", }); }; const { showNotification } = useNotification(); const submitHandler = () => { if (!validateEmail(inputsValue.email)) { setInputsError((prevValue) => ({ ...prevValue, email: true })); return showNotification({ show: true, text: "Введите коректный email", type: "error", }); } setLoader(true); apiRequest("/register/request-password-reset", { method: "POST", data: { email: inputsValue.email, }, }).then((data) => { setLoader(false); if (data) { showNotification({ show: true, text: "Письмо отправлено Вам на почту", type: "success", }); setStep(true); } }); }; const resetPassword = () => { if (!inputsValue.password || !inputsValue.token) { setInputsError((prevValue) => ({ ...prevValue, password: true, token: true, })); return showNotification({ show: true, text: "Введите данные", type: "error", }); } if (inputsValue.password.length < 6) { setInputsError((prevValue) => ({ ...prevValue, password: true })); return; } setLoader(true); apiRequest("/register/reset-password", { method: "POST", data: { token: inputsValue.token, password: inputsValue.password, }, }).then((data) => { setLoader(false); if (data.code === 0) { showNotification({ show: true, text: "Введите коректные данные", type: "error", }); } else { setActive(false); resetInputsValue(); showNotification({ show: true, text: "Пароль изменён", type: "success", }); } }); }; return (
close setActive(false)} />

Восстановление пароля

{!step ? (
Введите email:
{ setInputsValue((prevValue) => ({ ...prevValue, email: e.target.value, })); setInputsError({ email: false, password: false, token: false, }); }} placeholder="Email" value={inputsValue.email} className={inputsError.email ? "error" : ""} /> {inputsError.email && ( Введите коректный email )} {loader ? ( ) : ( )}
) : (
setStep(false)} className="resetPassword__email__arrow" />
Введите код подтверждения:
{ setInputsError({ email: false, password: false, token: false, }); setInputsValue((prevValue) => ({ ...prevValue, token: e.target.value, })); }} value={inputsValue.token} className={inputsError.token ? "error" : ""} placeholder="token" /> {inputsError.token && ( Введите данные )}
Введите новый пароль:
{ setInputsValue((prevValue) => ({ ...prevValue, password: e.target.value, })); setInputsError({ email: false, password: false, token: false, }); }} placeholder="password" value={inputsValue.password} className={inputsError.password ? "error" : ""} /> {inputsError.password && ( Минимум 6 символов )} {loader ? ( ) : ( )}
)}
); }; export default ModalResetPassword;