diff --git a/package.json b/package.json index 110a940..fcac415 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@hookform/resolvers": "^3.6.0", "@reduxjs/toolkit": "^2.2.5", "@types/react-redux": "^7.1.33", + "axios": "^1.7.2", "clsx": "^2.1.1", "react": "^18.2.0", "react-dom": "^18.2.0", @@ -25,6 +26,7 @@ "react-icons": "^5.2.1", "react-redux": "^9.1.2", "react-router-dom": "^6.23.1", + "react-toastify": "^10.0.5", "sass": "^1.77.2", "tailwind-merge": "^2.3.0", "zod": "^3.23.8" diff --git a/src/components/Counter.tsx b/src/components/Counter.tsx deleted file mode 100644 index 9a9c909..0000000 --- a/src/components/Counter.tsx +++ /dev/null @@ -1,20 +0,0 @@ -import { useDispatch, useSelector } from 'react-redux'; -import { AppDispatch, RootState } from '../state/store'; -import { decrement, increment } from '../state/counter/counterSlice'; - -const Counter = () => { - const count = useSelector((state: RootState) => state.counter.value); - const dispatch = useDispatch(); - - return ( -
-

{count}

-
- - -
-
- ); -}; - -export default Counter; diff --git a/src/components/LoginComponent.tsx b/src/components/LoginComponent.tsx new file mode 100644 index 0000000..56aa612 --- /dev/null +++ b/src/components/LoginComponent.tsx @@ -0,0 +1,109 @@ +import { useEffect } from 'react'; +import { useForm, SubmitHandler } from 'react-hook-form'; +import { zodResolver } from '@hookform/resolvers/zod'; +import GoogleIcon from './../assets/googleIcon.svg'; +import Button from './Button'; +import Input from './Input'; +import { loginSchema, LoginData } from './../utils/schemas'; +import { useNavigate, useLocation } from 'react-router-dom'; +import { useDispatch } from 'react-redux'; +import { setToken, setUser } from '../redux/reducers/userSlice'; +import { toast } from 'react-toastify'; +import { useLoginUserMutation } from '../services/authenticationAPI'; + +const LoginComponent = () => { + const navigate = useNavigate(); + const location = useLocation(); + const dispatch = useDispatch(); + + const { + register, + handleSubmit, + setError, + formState: { errors, isSubmitting }, + } = useForm({ resolver: zodResolver(loginSchema) }); + + const [loginUser, { data, error, isLoading, isSuccess, isError }] = useLoginUserMutation(); + + const onSubmit: SubmitHandler = async (userCredentials) => { + await loginUser(userCredentials); + }; + + useEffect(() => { + if (isError && error) { + setError('root', { + message: 'Invalid email or password', + }); + return; + } + if (isSuccess && data) { + const token = data.token; + + dispatch(setUser(data.user)); + dispatch(setToken(token)); + + toast.success('Login successful'); + const from = location.state?.from?.pathname || '/'; + navigate(from); + } + }, [isError, isSuccess, error, data, navigate, location.state, dispatch]); + + const handleGoogleAuthentication = (e: any) => { + e.preventDefault(); + document.location.assign('https://e-commerce-mavericcks-bn-staging-istf.onrender.com/api/auth/google'); + }; + + return ( + <> +
+
+

Existing Customer?

+

Sign in to continue

+ {errors.root && ( +

{errors.root.message}

+ )} +
+ + + + {isSuccess &&

Login successful!

} + or + +
+
+
+

New Customer?

+

Create an account here

+
+
+ + ); +}; + +export default LoginComponent; diff --git a/src/components/register/RegisterSection.tsx b/src/components/register/RegisterSection.tsx index 6736205..46a3d93 100644 --- a/src/components/register/RegisterSection.tsx +++ b/src/components/register/RegisterSection.tsx @@ -7,10 +7,10 @@ import { SubmitHandler, useForm } from 'react-hook-form'; import { extendedSchema, ExtendedFormFields } from '../../utils/schemas'; import { useNavigate } from 'react-router-dom'; import { useDispatch } from 'react-redux'; -import { useRegisterUserMutation } from '../../services/authAPI'; -import { setUserRegistered } from '../../state/register/registerSlice'; +import { setUserRegistered } from '../../redux/reducers/registerSlice'; import { useEffect } from 'react'; import { QueryErrorData } from '../../utils/schemas'; +import { useRegisterUserMutation } from '../../services/authenticationAPI'; const RegisterSection = () => { const navigate = useNavigate(); diff --git a/src/index.js b/src/index.js index b9c4b42..7cfcd0b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,3 @@ -import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; diff --git a/src/main.tsx b/src/main.tsx index 9f1839c..5df6c8b 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -2,7 +2,7 @@ import React from 'react'; import ReactDOM from 'react-dom/client'; import App from './App.tsx'; import './index.css'; -import { store } from './state/store.ts'; +import { store } from './redux/store.ts'; import { Provider } from 'react-redux'; ReactDOM.createRoot(document.getElementById('root')!).render( diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index 2135579..338bea1 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -1,20 +1,15 @@ -import { useNavigate } from "react-router-dom"; +import LoginComponent from '../components/LoginComponent'; +import Navbar from '../components/navbar/Navbar'; +import Footer from '../components/footer/Footer'; function Login() { - const navigate = useNavigate(); - - const handleNavigate = () => { - navigate("/"); - }; - - return ( -
-
-

Login page

- -
-
- ); + return ( + <> + + +