-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from leozhang14/createDropdown
Create dropdown
- Loading branch information
Showing
5 changed files
with
139 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useState } from "react"; | ||
import { CiMenuBurger } from "react-icons/ci"; | ||
import { logout } from "./Auth"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { toast } from "react-toastify"; | ||
|
||
type DropdownProps = { | ||
email?: string | null; | ||
}; | ||
|
||
function getUsernameFromEmail(email: string): string { | ||
const atIndex = email.indexOf("@"); | ||
|
||
if (atIndex !== -1) { | ||
return email.substring(0, atIndex); | ||
} | ||
|
||
return email; | ||
} | ||
|
||
const Dropdown = ({ email }: DropdownProps) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
const handleClick = () => { | ||
setIsOpen(!isOpen); | ||
}; | ||
|
||
const navigate = useNavigate(); | ||
|
||
const handleLogout = async () => { | ||
try { | ||
await logout(navigate); | ||
toast.success("Logged out successfully", { | ||
autoClose: 1000, | ||
hideProgressBar: true, | ||
position: "bottom-left", | ||
}); | ||
|
||
console.log("Logged out successfully"); | ||
} catch (error) { | ||
toast.error("Logged out successfully", { | ||
autoClose: 1000, | ||
hideProgressBar: true, | ||
position: "bottom-left", | ||
}); | ||
console.error("Logout failed", error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="flex items-center relative"> | ||
<CiMenuBurger | ||
className="text-3xl cursor-pointer hover:scale-110" | ||
onClick={handleClick} | ||
></CiMenuBurger> | ||
|
||
{isOpen && ( | ||
<div className="absolute right-0 top-14 w-48 bg-white border rounded-lg shadow-lg z-10 text-center"> | ||
<div className="flex flex-col"> | ||
<div className="p-2 border-b text-gray-400"> | ||
<span> | ||
{email ? getUsernameFromEmail(email) : "not logged in"} | ||
</span> | ||
</div> | ||
|
||
{/* Logout option */} | ||
<div | ||
className="p-3 px-3 text-gray-700 cursor-pointer hover:bg-gray-100" | ||
onClick={handleLogout} | ||
> | ||
Log Out | ||
</div> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Dropdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters