import React, { useEffect, useState } from "react"; import { FaUserCircle } from "react-icons/fa"; import { NavLink, useNavigate } from "react-router-dom"; // Import NavLink for navigation import "./Navbar.css"; // Navbar-specific styles import axios from "axios"; import { toast, ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; const Navbar = () => { const navigate = useNavigate(); const [user, setUser] = useState(null); useEffect(() => { const loggedInUser = localStorage.getItem("user"); if (loggedInUser) { setUser(JSON.parse(loggedInUser)); } else { axios.get("http://localhost:8080/api/user/profile", { withCredentials: true }) .then((response) => { setUser(response.data.user); }) .catch((error) => { console.error("Error fetching user data:", error); }); } }, []); // Handle logout functionality const handleLogout = async () => { try { // Call the logout API await axios.get("http://localhost:8080/auth/logout", { withCredentials: true }); // Redirect to the login page after successful logout navigate("/"); } catch (error) { console.error("Error during logout:", error); toast.error("Failed to log out. Please try again."); } }; return (
Logo
{/* Consolidated buttons in the center */} Faculty Consolidated Course Consolidated Department Consolidated
{/* User icon at the right */} {user && user.profilePicture ? ( Profile ) : ( )}
); }; export default Navbar;