Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logout functionality #18

Merged
merged 5 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/profile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 29 additions & 1 deletion components/TopBar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
"use client";

import { useState } from "react";
import { usePathname } from "next/navigation";
import CurrentTime from "./CurrentTime";
import { useAuth } from "@/services/AuthContext";
import ProfileIcon from '../assets/profile.png';
import Image from "next/image";
import { deleteCookie } from "cookies-next";

const TopBar = () => {
const { userName } = useAuth();
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const routeNames: { [key: string]: string } = {
'/reports': 'Report',
'/library': 'Library',
Expand All @@ -15,11 +20,34 @@ const TopBar = () => {
const pathname = usePathname();
const routeName = routeNames[pathname] || <p>Welcome <br /> {userName} </p>;

const toggleDropdown = () => {
setIsDropdownOpen(!isDropdownOpen);
};

const handleLogout = () => {
deleteCookie("access_token");
deleteCookie("refresh_token");
window.location.reload();
};

return (
<div className="max-w-xl mx-auto text-white p-4 h-32 justify-between items-center bg-primary">
<CurrentTime className="text-sm mb-4" />
<div className="mt-6 text-lg font-semibold">
<div className="mt-6 text-lg font-semibold justify-between flex mr-4 items-center">
{routeName}
<div className="relative">
<Image
src={ProfileIcon}
alt="Profile"
className="w-6 h-6 cursor-pointer"
onClick={toggleDropdown}
/>
{isDropdownOpen && (
<div className="absolute top-full right-1 bg-white p-2 shadow-md text-black rounded-lg text-base w-32 grid grid-cols-1 gap-2">
<button onClick={handleLogout}>Logout</button>
</div>
)}
</div>
</div>
<div className="flex items-center space-x-4"></div>
</div>
Expand Down
Loading