From d1427010c26e6c72d8c89016582512c932718e8c Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 11 Jun 2024 12:58:21 -0700 Subject: [PATCH 1/8] Added validation for eamil field in forgot-password page. Make axios request to backend --- Client/src/Pages/ForgotPassword/index.jsx | 49 ++++++++++++++++++++++- Client/src/Pages/Register/index.jsx | 1 - Client/src/Validation/validation.js | 13 +++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Client/src/Pages/ForgotPassword/index.jsx b/Client/src/Pages/ForgotPassword/index.jsx index b94546251..d5ec84088 100644 --- a/Client/src/Pages/ForgotPassword/index.jsx +++ b/Client/src/Pages/ForgotPassword/index.jsx @@ -5,8 +5,52 @@ import Logomark from "../../assets/Images/key-password.png"; import EmailTextField from "../../Components/TextFields/Email/EmailTextField"; import Button from "../../Components/Button"; import LeftArrow from "../../assets/Images/arrow-left.png"; +import { useState, useEffect } from "react"; +import { recoveryValidation } from "../../Validation/validation"; +import axios from "axios"; + +const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL; const ForgotPassword = () => { + 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 () => { + const { error } = recoveryValidation.validate(form); + if (error === undefined) { + try { + const res = await axios.post(`${BASE_URL}/auth/recovery/request`, form); + console.log(res); + } catch (error) { + console.log(error); + } + } else { + alert(error); + } + }; return (
@@ -27,12 +71,15 @@ const ForgotPassword = () => {