From 5a2088118fe2067755f80f6869209b7317aa1711 Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Tue, 6 Jul 2021 23:11:03 -0400 Subject: [PATCH] hook up invitations api with notifications bar --- src/components/App.tsx | 16 +++++++++------- src/components/Notification.tsx | 16 +++++----------- src/components/Notifications.tsx | 16 ++-------------- src/components/api.ts | 15 +++++++++++++++ src/components/types.ts | 24 +++++++++++++----------- 5 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index fa7c4e5..e926e39 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -3,13 +3,14 @@ import { BrowserRouter, Route, Switch } from 'react-router-dom'; import AuthenticationContext from './Authentication/AuthenticationContext'; import WheelShare from './WheelShare'; import WheelShareLoggedOut from './WheelShareLoggedOut'; -import { INotification } from './Notifications'; import Notifications from './Notifications'; +import { getReceivedInvitationsAndRequests } from './api'; +import { IInvitation } from './types'; const Authenticator = lazy(() => import('./Authentication/Authenticator')); const Group = lazy(() => import('./Group')); -const dummyNotificationData: INotification[] = ([] = [ +const dummyNotificationData: IInvitation[] = [ { user: { id: 0, @@ -20,7 +21,7 @@ const dummyNotificationData: INotification[] = ([] = [ name: 'Cross Country', }, isRequest: true, - sentTime: new Date(), + sentTime: new Date().toISOString(), }, { user: { @@ -32,22 +33,23 @@ const dummyNotificationData: INotification[] = ([] = [ name: 'TJ Lax', }, isRequest: false, - sentTime: new Date(), + sentTime: new Date().toISOString(), }, -]); +]; export default function App() { const { user } = useContext(AuthenticationContext); + // eslint-disable-next-line const [notifications, setNotifications] = useState(dummyNotificationData); useEffect(() => { - //getNotifications().then(setNotifications); + getReceivedInvitationsAndRequests().then(setNotifications); }, []); return (
{notifications.length > 0 ? ( ) : ( - No notifications + No notifications )} diff --git a/src/components/Notification.tsx b/src/components/Notification.tsx index 357b09d..4d4c878 100644 --- a/src/components/Notification.tsx +++ b/src/components/Notification.tsx @@ -1,12 +1,12 @@ -import { INotification } from './Notifications'; import { useCallback } from 'react'; import { acceptInvite, acceptRequest, denyInvite, denyRequest } from './api'; +import { IInvitation } from './types'; import UIButton from './UIButton'; export default function Notification({ notification, }: { - notification: INotification; + notification: IInvitation; }) { const carpoolId = notification.carpool.id; @@ -26,6 +26,8 @@ export default function Notification({ denyInvite(carpoolId, notification.user.id); }, [carpoolId, notification.user.id]); + const sentTime = new Date(notification.sentTime); + return (
{notification.user.name} @@ -34,15 +36,7 @@ export default function Notification({ ) : ( invited you to join )} - {notification.carpool.name + - ' ' + - (notification.sentTime.getMonth() + 1) + - '/' + - notification.sentTime.getDate() + - '/' + - notification.sentTime.getFullYear() + - ' ' + - notification.sentTime.toLocaleTimeString()} + {notification.carpool.name + sentTime.toLocaleString()} {notification.isRequest ? (
Accept diff --git a/src/components/Notifications.tsx b/src/components/Notifications.tsx index 78a863f..09d3ac1 100644 --- a/src/components/Notifications.tsx +++ b/src/components/Notifications.tsx @@ -1,22 +1,10 @@ import Notification from './Notification'; - -export type INotification = { - user: { - id: number; - name: string; - }; - carpool: { - id: number; - name: string; - }; - isRequest: boolean; - sentTime: Date; -}; +import { IInvitation } from './types'; export default function Notifications({ notifications, }: { - notifications: INotification[]; + notifications: IInvitation[]; }) { return (
diff --git a/src/components/api.ts b/src/components/api.ts index e25e757..ba09bd2 100644 --- a/src/components/api.ts +++ b/src/components/api.ts @@ -1,5 +1,6 @@ import { IEventSignup } from './Event'; import { GroupPreview } from './GroupJoinerLink'; +import { IInvitation } from './types'; async function post(path: string, data: any) { const res = await fetch('http://localhost:5000/api' + path, { @@ -163,3 +164,17 @@ export async function joinGroup(id: number, code: string) { status: result.status, }; } + +export async function generateCode(groupId: number) { + return await post('/groups/' + groupId + '/generate_code', {}); +} + +export async function resetCode(groupId: number) { + return await post('/groups/' + groupId + '/reset_code', {}); +} + +export async function getReceivedInvitationsAndRequests() { + return (await get( + '/users/@me/received_requests_and_invites' + )) as IInvitation[]; +} diff --git a/src/components/types.ts b/src/components/types.ts index 635c5ff..208ebc2 100644 --- a/src/components/types.ts +++ b/src/components/types.ts @@ -8,17 +8,6 @@ export type IUser = { name: string | null; }; -/** - * Model Invitation - */ - -export type IInvitation = { - userId: number; - carpoolId: number; - isRequest: boolean; - sentTime: Date; -}; - /** * Model Carpool */ @@ -75,3 +64,16 @@ export type IEventSignup = { latitude: number | null; longitude: number | null; }; + +export type IInvitation = { + user: { + id: number; + name: string; + }; + carpool: { + id: number; + name: string; + }; + sentTime: string; + isRequest: boolean; +};