Added validation for eamil field in forgot-password page. Make axios request to backend
This commit is contained in:
1 parent
7bd4ce9e6a
commit
d1427010c2
3 files changed
+60
-3
No files matched your search
@@ -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 (
|
||||
<div className="forgot-password-page">
|
||||
<BackgroundPattern></BackgroundPattern>
|
||||
@@ -27,12 +71,15 @@ const ForgotPassword = () => {
|
||||
<div className="forgot-password-v-gap-large"></div>
|
||||
<div className="forgot-password-body">
|
||||
<EmailTextField
|
||||
error={false}
|
||||
onChange={handleInput}
|
||||
error={errors.email ? true : false}
|
||||
helperText={errors.email ? errors.email : ""}
|
||||
placeholder="Enter your email"
|
||||
id="forgot-password-email-input"
|
||||
/>
|
||||
<div className="forgot-password-v-gap-medium"></div>
|
||||
<Button
|
||||
onClick={handleSubmit}
|
||||
level="primary"
|
||||
label="Reset password"
|
||||
sx={{
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import axios from "axios";
|
||||
import "./index.css";
|
||||
import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPattern";
|
||||
import Logomark from "../../assets/Images/Logomark.png";
|
||||
|
||||
@@ -40,4 +40,15 @@ const loginValidation = joi.object({
|
||||
}),
|
||||
});
|
||||
|
||||
export { registerValidation, loginValidation };
|
||||
const recoveryValidation = joi.object({
|
||||
email: joi
|
||||
.string()
|
||||
.email({ tlds: { allow: false } })
|
||||
.required()
|
||||
.messages({
|
||||
"string.email": "Email must be a valid email",
|
||||
"string.empty": "Email is required",
|
||||
}),
|
||||
});
|
||||
|
||||
export { registerValidation, loginValidation, recoveryValidation };
|
||||
Reference in new issue
Block a user