import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPattern"; import "./index.css"; import React from "react"; import Logomark from "../../assets/Images/key-password.png"; import Button from "../../Components/Button"; import LeftArrow from "../../assets/Images/arrow-left.png"; import { useState, useEffect } from "react"; import { recoveryValidation } from "../../Validation/validation"; import axiosInstance from "../../Utils/axiosConfig"; import { useNavigate } from "react-router-dom"; import Field from "../../Components/Inputs/Field"; const ForgotPassword = () => { const navigate = useNavigate(); const [isLoading, setIsLoading] = useState(false); // Used to disable the button while loading so user doesn't submit multiple times const [errors, setErrors] = useState({}); const [form, setForm] = useState({ email: "", }); const idMap = { "forgot-password-email-input": "email", }; useEffect(() => { const { error } = recoveryValidation.validate(form); if (error === undefined) { setErrors({}); } else { const validationErrors = error.details.reduce((acc, err) => { return { ...acc, [err.path[0]]: err.message }; }, {}); setErrors(validationErrors); } }, [form]); const handleInput = (e) => { const fieldName = idMap[e.target.id]; setForm({ ...form, [fieldName]: e.target.value }); }; const handleSubmit = async () => { //TODO show loading spinner setIsLoading(true); try { const { error } = recoveryValidation.validate(form); if (error !== undefined) { throw error; } await axiosInstance.post(`/auth/recovery/request`, form); navigate("/check-email"); } catch (error) { //TODO display error (Toast?) alert(error); } finally { setIsLoading(false); } }; return (
Logomark
Forgot password?
No worries, we’ll send you reset instructions.
LeftArrow
Back to log in
); }; export default ForgotPassword;