From 8cbb8ee9637d08315b0cab59617f87e640404938 Mon Sep 17 00:00:00 2001 From: jveebs <83558932+jveebs@users.noreply.github.com> Date: Thu, 13 Apr 2023 17:01:18 -0400 Subject: [PATCH] navbar finished --- src/components/NavBar.css | 9 ++++++--- src/components/NavBar.js | 40 +++++++++++++++++++-------------------- src/components/SideNav.js | 15 ++++++++++++++- src/pages/Dashboard.jsx | 2 +- src/utils/utils.js | 34 +++++++++++++++++++-------------- 5 files changed, 60 insertions(+), 40 deletions(-) diff --git a/src/components/NavBar.css b/src/components/NavBar.css index 276bea0..5fdcc29 100644 --- a/src/components/NavBar.css +++ b/src/components/NavBar.css @@ -11,12 +11,15 @@ .nav-brand { display: flex; align-items: center; +} + +.nav-header { text-decoration: none; - color: inherit; + color: black; } -.nav-brand:hover { - color: inherit; +.nav-header:hover { + color: black; } .nav-login-control { diff --git a/src/components/NavBar.js b/src/components/NavBar.js index 693c0d0..9f69fa2 100644 --- a/src/components/NavBar.js +++ b/src/components/NavBar.js @@ -4,31 +4,29 @@ import { Toolbar, Typography, Button, IconButton, Link } from "@material-ui/core import AccountCircle from '@mui/icons-material/AccountCircle'; import logo from "../assets/BudgetBuddyLogo.png" import "./NavBar.css" -import {checkLogin, getFirstName} from "../utils/utils"; +import {checkLogin, getName, handleLogout} from "../utils/utils"; import NavDropdown from 'react-bootstrap/NavDropdown'; +import { useEffect, useState } from "react"; +import { async } from "q"; export default function NavBar() { - const handleLogin = () => { - window.location.href='/login'; - } - - const handleSignup = () => { - window.location.href='/signup'; - } - - const handleLogout= () => { - document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); }); - window.location.href='/welcome'; - } + const [name, setName] = useState(null); + useEffect(() => { + async function fetchName() { + const name = await getName(); + setName(name); + } + fetchName(); + }, []); return ( - + + variant="h5" + className="nav-header"> BudgetBuddy @@ -36,17 +34,17 @@ export default function NavBar() { {checkLogin() ? // Logged In
- - Profile - My account + + Settings + About Us Sign Out
: // Logged Out - +

or

- +
} diff --git a/src/components/SideNav.js b/src/components/SideNav.js index eb0cf7f..7e9df1f 100644 --- a/src/components/SideNav.js +++ b/src/components/SideNav.js @@ -9,9 +9,21 @@ import LogoutIcon from '@mui/icons-material/Logout'; import { Avatar, Typography } from '@material-ui/core'; import { Link, Divider } from '@material-ui/core'; import './SideNav.css' +import { useState, useEffect } from 'react' +import {getName, handleLogout} from "../utils/utils"; export default function SideNav() { + const [firstName, setFirstName] = useState(null); + + useEffect(() => { + async function fetchFirstName() { + const name = await getName(); + setFirstName(name.split(" ")[0]); + } + fetchFirstName(); + }, []); + return(
@@ -19,7 +31,7 @@ export default function SideNav() {
- Welcome, Jacob + Welcome, {firstName} Your Budget Overiew
@@ -61,6 +73,7 @@ export default function SideNav() { */} }> Sign Out diff --git a/src/pages/Dashboard.jsx b/src/pages/Dashboard.jsx index e4ae9fe..c2c456c 100644 --- a/src/pages/Dashboard.jsx +++ b/src/pages/Dashboard.jsx @@ -12,7 +12,7 @@ import AddIncome from '../components/AddIncome' export default function Dashboard() { return ( -
+

My Budget Planner

diff --git a/src/utils/utils.js b/src/utils/utils.js index c358688..7a004c7 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -16,24 +16,30 @@ function getSessionKey() { for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i].trim(); // Remove any leading or trailing whitespace if (cookie.startsWith('session=')) { - console.log(cookie.substring('session='.length, cookie.length)); return cookie.substring('session='.length, cookie.length); // Extract the value of the cookie } } return null; } -export function getFirstName() { - fetch('http://127.0.0.1:3030/userinfo', { - method: 'GET', - headers: { - 'x-session-key' : "b36efa01-7824-4f61-a274-63131b58d8fe", - } - }) - .then(response => response.json()) - .then(data => { - const name = data.name; - return name; // Return firstname - }) - .catch(error => console.error(error)); +export async function getName() { + try { + const response = await fetch('http://127.0.0.1:3030/userinfo', { + method: 'GET', + headers: { + 'x-session-key': getSessionKey(), + }, + }); + const data = await response.json(); + const firstName = data.name; + console.log(firstName); // Logs the first name correctly + return firstName; + } catch (error) { + console.error(error); + } +} + +export function handleLogout() { + document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); }); + window.location.href='/welcome'; } \ No newline at end of file