From 0999b0fc27ae9f2e18c044c027fe0820a0536bbc Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Tue, 13 Jul 2021 14:22:31 -0400 Subject: [PATCH] create Header component, fix Notifications --- src/components/App.tsx | 48 +------------------ src/components/Carpool/Carpool.tsx | 5 +- src/components/Header/Header.tsx | 36 ++++++++++++++ src/components/Notifications/Notification.tsx | 2 +- .../Notifications/Notifications.tsx | 18 ++++--- src/components/WheelShare.tsx | 12 +---- src/components/hooks.ts | 14 +++++- 7 files changed, 64 insertions(+), 71 deletions(-) create mode 100644 src/components/Header/Header.tsx diff --git a/src/components/App.tsx b/src/components/App.tsx index 4f96b62..6d9e16e 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -1,9 +1,6 @@ -import { CSSProperties, lazy, Suspense, useEffect, useState } from 'react'; +import { CSSProperties, lazy, Suspense } from 'react'; import { BrowserRouter, Route, Switch } from 'react-router-dom'; -import { getReceivedInvitationsAndRequests } from './api'; import { useMe } from './hooks'; -import Notifications from './Notifications/Notifications'; -import { IInvitation } from './types'; import WheelShare from './WheelShare'; import WheelShareLoggedOut from './WheelShareLoggedOut'; @@ -11,33 +8,6 @@ const Authenticator = lazy(() => import('./Authentication/Authenticator')); const Carpool = lazy(() => import('./Carpool/Carpool')); const Group = lazy(() => import('./Group')); -const dummyNotificationData: IInvitation[] = [ - { - user: { - id: 0, - name: 'Michael Fatemi', - }, - carpool: { - id: 0, - name: 'Cross Country', - }, - isRequest: true, - sentTime: new Date().toISOString(), - }, - { - user: { - id: 1, - name: 'Joshua Hsueh', - }, - carpool: { - id: 0, - name: 'TJ Lax', - }, - isRequest: false, - sentTime: new Date().toISOString(), - }, -]; - const style: CSSProperties = { display: 'flex', flexDirection: 'column', @@ -48,27 +18,11 @@ const style: CSSProperties = { marginRight: 'auto', }; -function useNotifications() { - const [notifications, setNotifications] = useState(dummyNotificationData); - - useEffect(() => { - getReceivedInvitationsAndRequests().then(setNotifications); - }, []); - - return notifications; -} - export default function App() { const user = useMe(); - const notifications = useNotifications(); return (
- {notifications.length > 0 ? ( - - ) : ( - No notifications - )} +
{carpool ? ( <> diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx new file mode 100644 index 0000000..a41db6a --- /dev/null +++ b/src/components/Header/Header.tsx @@ -0,0 +1,36 @@ +import logout from '../Authentication/logout'; +import { useMe, useNotifications } from '../hooks'; +import Notifications from '../Notifications/Notifications'; +import UIPressable from '../UI/UIPressable'; +import UIPrimaryTitle from '../UI/UIPrimaryTitle'; + +export default function Header() { + const me = useMe(); + const notifications = useNotifications(); + return ( +
+ WheelShare + {me ? ( + <> + {me.name} + Log out +
+ {notifications.length > 0 ? ( + + ) : ( + No notifications + )} + + ) : ( + Log In + )} +
+ ); +} diff --git a/src/components/Notifications/Notification.tsx b/src/components/Notifications/Notification.tsx index 3893ab1..a10d538 100644 --- a/src/components/Notifications/Notification.tsx +++ b/src/components/Notifications/Notification.tsx @@ -36,7 +36,7 @@ export default function Notification({ ) : ( invited you to join )} - {notification.carpool.name + sentTime.toLocaleString()} + {notification.carpool.name + ' at ' + sentTime.toLocaleString()} {notification.isRequest ? (
Accept diff --git a/src/components/Notifications/Notifications.tsx b/src/components/Notifications/Notifications.tsx index 773b58c..9e8ab9a 100644 --- a/src/components/Notifications/Notifications.tsx +++ b/src/components/Notifications/Notifications.tsx @@ -7,16 +7,14 @@ export default function Notifications({ notifications: IInvitation[]; }) { return ( -
-
-

Notifications

- {notifications.map((notification) => ( - - ))} -
+
+

Notifications

+ {notifications.map((notification) => ( + + ))}
); } diff --git a/src/components/WheelShare.tsx b/src/components/WheelShare.tsx index d96e999..3f49195 100644 --- a/src/components/WheelShare.tsx +++ b/src/components/WheelShare.tsx @@ -1,19 +1,11 @@ -import logout from './Authentication/logout'; import Events from './Events'; import Groups from './Groups/Groups'; -import { useMe } from './hooks'; -import UIPressable from './UI/UIPressable'; -import UIPrimaryTitle from './UI/UIPrimaryTitle'; +import Header from './Header/Header'; export default function WheelShare() { - const { name } = useMe()!; - return ( <> - WheelShare - - {name} - Log out +
diff --git a/src/components/hooks.ts b/src/components/hooks.ts index 7372eda..69a37a0 100644 --- a/src/components/hooks.ts +++ b/src/components/hooks.ts @@ -1,5 +1,17 @@ -import { useContext } from 'react'; +import { useContext, useEffect, useState } from 'react'; +import { getReceivedInvitationsAndRequests } from './api'; import AuthenticationContext from './Authentication/AuthenticationContext'; +import { IInvitation } from './types'; export const useAuth = () => useContext(AuthenticationContext); export const useMe = () => useAuth().user; + +export function useNotifications() { + const [notifications, setNotifications] = useState([]); + + useEffect(() => { + getReceivedInvitationsAndRequests().then(setNotifications); + }, []); + + return notifications; +}