From 72ff0030cdc599ff68ae620bb3b144f407e3848c Mon Sep 17 00:00:00 2001 From: Joshua Hsueh Date: Tue, 6 Jul 2021 22:44:12 -0400 Subject: [PATCH] started notifications --- src/components/Groups.tsx | 39 ++++++++++++++++++++-- src/components/Notification.tsx | 57 ++++++++++++++++++++++++++++++++ src/components/Notifications.tsx | 32 ++++++++++++++++++ src/components/api.ts | 20 +++++++++++ 4 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 src/components/Notification.tsx create mode 100644 src/components/Notifications.tsx diff --git a/src/components/Groups.tsx b/src/components/Groups.tsx index ba7dd1e..80c5218 100644 --- a/src/components/Groups.tsx +++ b/src/components/Groups.tsx @@ -1,19 +1,54 @@ import { useState, useEffect } from 'react'; -import { getGroups } from './api'; +import { getGroups, getNotifications } from './api'; import { IGroup } from './Group'; +import { INotification } from './Notifications'; import GroupCreatorLink from './GroupCreatorLink'; import GroupJoinerLink from './GroupJoinerLink'; import GroupList from './GroupList'; +import Notifications from './Notifications'; + +const dummyNotificationData: INotification[] = ([] = [ + { + user: { + id: 0, + name: 'Michael Fatemi', + }, + carpool: { + id: 0, + name: 'Cross Country', + }, + isRequest: true, + sentTime: new Date(), + }, + { + user: { + id: 1, + name: 'Joshua Hsueh', + }, + carpool: { + id: 0, + name: 'TJ Lax', + }, + isRequest: false, + sentTime: new Date(), + }, +]); export default function Groups() { const [groups, setGroups] = useState([]); - + const [notifications, setNotifications] = useState(dummyNotificationData); useEffect(() => { getGroups().then(setGroups); + //getNotifications().then(setNotifications); }, []); return ( <> + {notifications.length > 0 ? ( + + ) : ( + No notifications + )}

Groups


diff --git a/src/components/Notification.tsx b/src/components/Notification.tsx new file mode 100644 index 0000000..558ca63 --- /dev/null +++ b/src/components/Notification.tsx @@ -0,0 +1,57 @@ +import { INotification } from './Notifications'; +import { useCallback } from 'react'; +import { acceptInvite, acceptRequest, denyInvite, denyRequest } from './api'; +import UIButton from './UIButton'; + +export default function Notification({ + notification, +}: { + notification: INotification; +}) { + const acceptReq = useCallback(() => { + acceptRequest(notification.user.id); + }, [notification.user.id]); + + const rejectReq = useCallback(() => { + denyRequest(notification.user.id); + }, [notification.user.id]); + + const acceptInv = useCallback(() => { + acceptInvite(notification.user.id); + }, [notification.user.id]); + + const rejectInv = useCallback(() => { + denyInvite(notification.user.id); + }, []); + + return ( +
+ {notification.user.name} + {notification.isRequest ? ( + request you to join + ) : ( + invited you to join + )} + {notification.carpool.name + + ' ' + + (notification.sentTime.getMonth() + 1) + + '/' + + notification.sentTime.getDate() + + '/' + + notification.sentTime.getFullYear() + + ' ' + + notification.sentTime.toLocaleTimeString()} + {notification.isRequest ? ( +
+ Accept + Reject +
+ ) : ( +
+ Accept + Reject +
+ )} +
+ ); +} diff --git a/src/components/Notifications.tsx b/src/components/Notifications.tsx new file mode 100644 index 0000000..be98466 --- /dev/null +++ b/src/components/Notifications.tsx @@ -0,0 +1,32 @@ +import { userInfo } from 'node:os'; +import Notification from './Notification'; + +export type INotification = { + user: { + id: number; + name: string; + }; + carpool: { + id: number; + name: string; + }; + isRequest: boolean; + sentTime: Date; +}; + +export default function Notifications({ + notifications, +}: { + notifications: INotification[]; +}) { + return ( +
+
+

Notifications

+ {notifications.map((notification) => { + return ; + })} +
+
+ ); +} diff --git a/src/components/api.ts b/src/components/api.ts index 3ff9f76..05230c3 100644 --- a/src/components/api.ts +++ b/src/components/api.ts @@ -128,6 +128,26 @@ export async function createGroup(name: string) { }; } +export async function getNotifications() { + return await get('/users/@me/received_requests_and_invites'); +} + +export async function acceptRequest(userId: number) { + return await post('/carpools/:id/accept_request', { userId }); +} + +export async function acceptInvite(userId: number) { + return await post('/carpools/:id/accept_invite', { userId }); +} + +export async function denyRequest(userId: number) { + return await post('/carpools/:id/deny_request', { userId }); +} + +export async function denyInvite(userId: number) { + return await post('/carpools/:id/deny_invite', { userId }); +} + export async function getMe() { return await get('/users/@me'); }