Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation Functions #72

Merged
merged 3 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"react-hot-toast": "^2.4.1",
"react-i18next": "^13.5.0",
"react-multi-carousel": "^2.8.4",
"react-redux": "^9.1.0"
"react-redux": "^9.1.0",
"yup": "^1.4.0"
},
"devDependencies": {
"locize-cli": "7.14.14"
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/configs/validation/changePassSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as yup from "yup";
import { validation } from "@/utils/validation";

export const changePasswordValidation = async (values) => {
const schema = yup.object().shape({
currentPassword: yup.string().required("Current password is required"),
newPassword: yup.string()
.required("New password is required")
.min(8, "Password must be at least 8 characters"),
confirmPassword: yup.string().oneOf(
[yup.ref("newPassword"), null],
"Passwords must match"
),
});

return await validation(schema, values);
};
11 changes: 11 additions & 0 deletions frontend/src/configs/validation/loginSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { validation } from "src/utils/validation";
import * as yup from "yup";

export const loginValidation = async (values) => {
const schema = yup.object().shape({
email: yup.string().email("Invalid email").required("Email is required"),
password: yup.string().required("Password is required"),
});

return await validation(schema, values);
};
15 changes: 15 additions & 0 deletions frontend/src/configs/validation/registerSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as yup from "yup";
import { validation } from "@/utils/validation";

export const registerValidation = async (values) => {
const schema = yup.object().shape({
fullname: yup.string().required("Fullname is required"),
username: yup.string().required("Username is required"),
email: yup.string().email("Invalid email").required("Email is required"),
password: yup.string()
.required("Password is required")
.min(8, "Password must be at least 8 characters"),
});

return await validation(schema, values);
};
53 changes: 52 additions & 1 deletion frontend/src/pages/login.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
import { FormControl, Button, TextField } from "@mui/material";
import { useState, useEffect } from "react";
import { loginValidation } from "src/configs/validation/loginSchema";

const { default: BlankLayout } = require("src/layout/BlankLayout");

const Login = () => {
return <div>login</div>;
const [formData, setFormData] = useState(null);
const [errors, setErrors] = useState({});
const [formSubmit, setFormSubmit] = useState(false);

const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};

const handleSubmit = async () => {
setFormSubmit(true);
};

useEffect(() => {
const fetchData = async () => {
if (formData && formSubmit) {
const errors = await loginValidation(formData);
setErrors(errors);
}
};
fetchData();
}, [formData, formSubmit]);

return (
<div>
<FormControl>
<TextField
name="email"
label="Email"
variant="outlined"
onChange={handleChange}
error={errors.email ? true : false}
helperText={errors.email}
/>
</FormControl>
<FormControl>
<TextField
name="password"
label="Password"
variant="outlined"
type="password"
onChange={handleChange}
error={errors.password ? true : false}
helperText={errors.password}
/>
</FormControl>
<Button onClick={handleSubmit}>Login</Button>
</div>
);
};

Login.guestGuard = true;
Expand Down
30 changes: 16 additions & 14 deletions frontend/src/utils/validation.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { current } from "@reduxjs/toolkit";

export const validation = async (schema, values) => {
let schemaErrors = null
let schemaErrors = null;

try {
const response = await schema.validate(values, { abortEarly: false });
} catch (err) {
let Errors = err.inner?.map((err) => {
return { [err.path]: err.message };
});

if (Errors) schemaErrors = Object.assign({}, ...Errors);
else schemaErrors = null;
}

return { ...schemaErrors };
};

try {
const response = await schema.validate(values, { abortEarly: false })
} catch (err) {
let Errors = err.inner?.map(err => {
return { [err.path]: err.message }
})

if (Errors)
schemaErrors = Object.assign({}, ...Errors)
else
schemaErrors = null
}

return { ...schemaErrors }
}