From 6769992f96de4cc4c7d273f9c526a791ebabfaae Mon Sep 17 00:00:00 2001 From: ucangun <149247682+ucangun@users.noreply.github.com> Date: Tue, 17 Sep 2024 20:55:33 +0200 Subject: [PATCH] "Updated Navbar component to use react-redux and added logout functionality" --- src/components/Navbar.tsx | 100 +++++++++++++++++++++++++------------ src/features/authSlice.tsx | 13 ++++- src/hooks/useAuthCall.tsx | 34 +++++++++++-- 3 files changed, 111 insertions(+), 36 deletions(-) diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 18cd0ef..43d5a84 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -14,11 +14,36 @@ import { } from "@mui/material"; import MenuIcon from "@mui/icons-material/Menu"; import Logo from "./Logo"; +import { useSelector } from "react-redux"; +import { RootState } from "../app/store"; +import useAuthCall from "../hooks/useAuthCall"; const pages = ["Products", "Pricing", "Blog"]; -const settings = ["Profile", "Account", "Logout"]; + +interface SettingsType { + title: string; + onClick: () => void; +} function Navbar() { + const { currentUser } = useSelector((state: RootState) => state.auth); + const { logout } = useAuthCall(); + + const settings: SettingsType[] = [ + { + title: "Profile", + onClick: () => {}, + }, + { + title: "Account", + onClick: () => {}, + }, + { + title: "Logout", + onClick: logout, + }, + ]; + const [anchorElNav, setAnchorElNav] = React.useState( null ); @@ -132,37 +157,50 @@ function Navbar() { {/* Pages */} {/* Avatar */} - - - - - - - {settings.map((setting) => ( - - - {setting} - - - ))} - + {currentUser ? ( + <> + + + + + + + {settings.map((setting) => ( + + + {setting.title} + + + ))} + + + ) : ( + + + + )} - {/* Avatar */} diff --git a/src/features/authSlice.tsx b/src/features/authSlice.tsx index 6ba6808..f63a4f3 100644 --- a/src/features/authSlice.tsx +++ b/src/features/authSlice.tsx @@ -33,6 +33,11 @@ const authSlice = createSlice({ state.currentUser = payload.user.username; state.token = payload.token; }, + logoutSuccess: (state) => { + state.loading = false; + state.token = ""; + state.currentUser = ""; + }, fetchFail: (state) => { state.loading = false; state.error = true; @@ -40,6 +45,12 @@ const authSlice = createSlice({ }, }); -export const { fetchStart, registerSuccess, fetchFail } = authSlice.actions; +export const { + fetchStart, + registerSuccess, + loginSuccess, + logoutSuccess, + fetchFail, +} = authSlice.actions; export default authSlice.reducer; diff --git a/src/hooks/useAuthCall.tsx b/src/hooks/useAuthCall.tsx index b556b0f..a3c42ae 100644 --- a/src/hooks/useAuthCall.tsx +++ b/src/hooks/useAuthCall.tsx @@ -1,15 +1,23 @@ -import { useDispatch } from "react-redux"; +import { useDispatch, useSelector } from "react-redux"; import { RegisterFormValues } from "../pages/Register"; import axios from "axios"; -import { fetchFail, fetchStart, registerSuccess } from "../features/authSlice"; +import { + fetchFail, + fetchStart, + loginSuccess, + logoutSuccess, + registerSuccess, +} from "../features/authSlice"; import { LoginFormValues } from "../pages/Login"; import { useNavigate } from "react-router-dom"; +import { RootState } from "../app/store"; const BASE_URL: string = import.meta.env.VITE_BASE_URL; const useAuthCall = () => { const dispatch = useDispatch(); const navigate = useNavigate(); + const { token } = useSelector((state: RootState) => state.auth); // register const register = async (userInfo: RegisterFormValues): Promise => { @@ -27,10 +35,11 @@ const useAuthCall = () => { // login const login = async (userInfo: LoginFormValues): Promise => { + dispatch(fetchStart()); try { - dispatch(fetchStart()); const { data } = await axios.post(`${BASE_URL}auth/login`, userInfo); console.log(data); + dispatch(loginSuccess(data)); navigate("/"); } catch (error) { dispatch(fetchFail()); @@ -38,7 +47,24 @@ const useAuthCall = () => { } }; - return { register, login }; + // logout + const logout = async (): Promise => { + dispatch(fetchStart()); + try { + await axios(`${BASE_URL}auth/logout`, { + headers: { + Authorization: `Token ${token}`, + }, + }); + dispatch(logoutSuccess()); + navigate("/login"); + } catch (error) { + dispatch(fetchFail()); + console.error(error); + } + }; + + return { register, login, logout }; }; export default useAuthCall;