diff --git a/Client/src/Features/Auth/authSlice.js b/Client/src/Features/Auth/authSlice.js index f8bc106e5..d361ffa05 100644 --- a/Client/src/Features/Auth/authSlice.js +++ b/Client/src/Features/Auth/authSlice.js @@ -22,6 +22,10 @@ export const register = createAsyncThunk( } ); +export const login = createAsyncThunk("auth/login", async (form, thunkApi) => { + console.log(form); +}); + const authSlice = createSlice({ name: "auth", initialState, diff --git a/Client/src/Pages/Login/index.jsx b/Client/src/Pages/Login/index.jsx index 19edcf6f0..3732e52c8 100644 --- a/Client/src/Pages/Login/index.jsx +++ b/Client/src/Pages/Login/index.jsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import "./index.css"; import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPattern"; import Logomark from "../../assets/Images/Logomark.png"; @@ -8,23 +8,51 @@ import Button from "../../Components/Button"; import Google from "../../assets/Images/Google.png"; import PasswordTextField from "../../Components/TextFields/Password/PasswordTextField"; +import { loginValidation } from "../../Validation/validation"; +import { login } from "../../Features/Auth/authSlice"; +import { useDispatch } from "react-redux"; + const Login = () => { + const dispatch = useDispatch(); + const idMap = { "login-email-input": "email", "login-password-input": "password", }; + const [errors, setErrors] = useState({}); + const [form, setForm] = useState({ email: "", password: "", }); - const handleInput = (e) => { - const fieldName = idMap[e.target.id]; - setForm({ - ...form, - [fieldName]: e.target.value, + useEffect(() => { + const { error } = loginValidation.validate(form, { + abortEarly: false, }); + + if (error) { + // Creates an error object in the format { field: message } + const validationErrors = error.details.reduce((acc, err) => { + return { ...acc, [err.path[0]]: err.message }; + }, {}); + setErrors(validationErrors); + } + }, []); + + const handleSubmit = async () => { + try { + await loginValidation.validateAsync(form, { abortEarly: false }); + dispatch(login(form)); + } catch (error) { + console.log(error); + } + }; + + const handleInput = (e) => { + const newForm = { ...form, [idMap[e.target.id]]: e.target.value }; + setForm(newForm); }; return ( @@ -44,14 +72,16 @@ const Login = () => {