source: frontend/src/pages/Nav.tsx@ 3238007

main
Last change on this file since 3238007 was b70fbeb, checked in by Filip Gavrilovski <filipgavrilovski28@…>, 6 months ago

add toasts on frontend

  • Property mode set to 100644
File size: 2.8 KB
Line 
1import { Link } from "react-router-dom";
2import { toast } from "react-toastify";
3import axiosInstance, { baseURL } from "../api/axiosInstance";
4import Logo from "../assets/logo-finkwave.png";
5import { useAuth } from "../context/authContext";
6
7const Nav = () => {
8 const { user, setUser, isAuthLoading } = useAuth();
9
10 const handleLogout = async (e: React.MouseEvent<HTMLButtonElement>) => {
11 e.preventDefault();
12 try {
13 await axiosInstance.post("/auth/logout");
14 setUser(undefined);
15 toast.success("Logout successful!");
16 } catch (error) {
17 console.error("Logout failed:", error);
18 toast.error("Logout failed!");
19 }
20 };
21
22 return (
23 <div className="bg-gray-800 p-4 flex justify-between items-center">
24 <Link to="/" className="text-white text-lg font-semibold">
25 <img src={Logo} alt="Finkwave Logo" className="h-12 w-auto" />
26 </Link>
27
28 <div className="flex items-center space-x-4">
29 {!isAuthLoading && (
30 <div className="flex items-center space-x-3">
31 {user ? (
32 <div className="flex items-center space-x-3">
33 <div className="flex items-center space-x-2 bg-slate-700 rounded-lg px-3 py-2">
34 {user.profilePhoto ? (
35 <img
36 src={`${baseURL}/${user.profilePhoto}`}
37 alt={`${user.username}'s profile`}
38 className="w-8 h-8 rounded-full object-cover"
39 />
40 ) : (
41 <div className="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center">
42 <span className="text-white text-sm font-semibold">
43 {user.username.charAt(0).toUpperCase()}
44 </span>
45 </div>
46 )}
47
48 <div className="text-white">
49 <p className="text-sm font-medium">{user.username}</p>
50 <p className="text-xs text-gray-300 capitalize">
51 {user.role}
52 </p>
53 </div>
54 </div>
55 <button
56 onClick={handleLogout}
57 className="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg text-sm
58 font-medium transition-colors duration-200 flex items-center space-x-1 cursor-pointer"
59 >
60 Logout
61 </button>
62 </div>
63 ) : (
64 <div className="flex items-center space-x-4">
65 <Link
66 to="/login"
67 className="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg text-sm
68 font-medium transition-colors duration-200 cursor-pointer"
69 >
70 Login
71 </Link>
72 <Link
73 to="/register"
74 className="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-lg text-sm
75 font-medium transition-colors duration-200 cursor-pointer"
76 >
77 Register
78 </Link>
79 </div>
80 )}
81 </div>
82 )}
83 </div>
84 </div>
85 );
86};
87
88export default Nav;
Note: See TracBrowser for help on using the repository browser.