-
Notifications
You must be signed in to change notification settings - Fork 1
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 #34 from Robert-M-Lucas/dev-SCRUM-46/51
Store transactions in database + fix header
- Loading branch information
Showing
4 changed files
with
48 additions
and
49 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 |
---|---|---|
@@ -1,52 +1,52 @@ | ||
import useWindowDimensions from "../hooks/WindowDimensionsHook.tsx"; | ||
import { Link, useNavigate } from "react-router-dom"; | ||
import { auth } from "../utils/firebase.ts"; | ||
import { useEffect, useState } from "react"; | ||
import { User } from "firebase/auth"; | ||
import "../assets/css/Header.css" | ||
import {Link, useNavigate} from "react-router-dom"; | ||
import {auth} from "../utils/firebase.ts"; | ||
import {useState} from "react"; | ||
import {User} from "firebase/auth"; | ||
|
||
export function Header() { | ||
const { width, height } = useWindowDimensions(); | ||
const aspect_ratio = (width == 0? 1 : width) / (height == 0? 1 : height); | ||
const aspect_ratio = (width == 0 ? 1 : width) / (height == 0 ? 1 : height); | ||
const use_narrow = aspect_ratio < 0.7; | ||
|
||
const [userName, setUserName] = useState<string | null>(null); | ||
auth.onAuthStateChanged((new_user: User | null) => { | ||
if (new_user === null) { setUserName(null); } | ||
else { setUserName(new_user?.displayName) } | ||
}); | ||
|
||
const navigate = useNavigate(); | ||
|
||
const handleLogout = async () => { | ||
|
||
const [isLoggedIn, setIsLoggedIn] = useState<boolean>(!!auth.currentUser?.uid); | ||
// displayName is optional, an account may not have a displayName but is logged in | ||
const [displayName, setDisplayName] = useState<string | null | undefined>(auth.currentUser?.displayName); | ||
|
||
// only attach the event listener once, otherwise we get an infinite loop | ||
useEffect(() => { | ||
auth.onAuthStateChanged((user: User | null) => { | ||
setIsLoggedIn(!!user?.uid); | ||
setDisplayName(user?.displayName); | ||
}); | ||
}, []); | ||
|
||
async function handleLogout() { | ||
await auth.signOut(); | ||
|
||
navigate("/", { replace: true }); | ||
}; | ||
|
||
return ( | ||
<header className="header"> | ||
{/* App Name reloads home page */} | ||
<a href="/" className="site-title"> | ||
<h3>{use_narrow? "B-19" : "Budget-19"}</h3> | ||
</a> | ||
|
||
{/* Pages */} | ||
<ul className="header-nav"> | ||
{/* Links to dashboard with tiles */} | ||
<li><Link to="/dash" className={`header-item ${userName ? "header-item" : "text-muted bg-transparent"}`}>Dashboard</Link></li> | ||
|
||
{/* Links to transactions page with table of expenses */} | ||
<li><Link to="/transactions" className={`header-item ${userName ? "header-item" : "text-muted bg-transparent"}`}>Transactions</Link></li> | ||
|
||
<li><Link to="/test" className="header-item">Firestore Test</Link></li> | ||
|
||
{userName && ( | ||
<li> | ||
<span className="username">{userName}</span> | ||
<button type="button" className="logout-btn" onClick={handleLogout}>Logout</button> | ||
</li> | ||
)} | ||
</ul> | ||
</header> | ||
); | ||
} | ||
|
||
return <header className="header"> | ||
{/* App Name reloads home page */} | ||
<h3><Link to="/">{use_narrow ? "B-19" : "Budget-19"}</Link></h3> | ||
|
||
{/* Pages */} | ||
<ul className="header-nav"> | ||
{/* Links to dashboard with tiles */} | ||
<li><Link to="/dash" className={`header-item ${isLoggedIn ? "header-item" : "text-muted bg-transparent"}`}>Dashboard</Link></li> | ||
|
||
{/* Links to transactions page with table of expenses */} | ||
<li><Link to="/transactions" className={`header-item ${isLoggedIn ? "header-item" : "text-muted bg-transparent"}`}>Transactions</Link></li> | ||
|
||
<li><Link to="/test" className="header-item">Firestore Test</Link></li> | ||
|
||
{displayName && <li><span className="username">{displayName}</span></li>} | ||
|
||
{isLoggedIn && <li><button type="button" className="logout-btn" onClick={handleLogout}>Logout</button></li>} | ||
</ul> | ||
</header> | ||
} |
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