diff --git a/src/components/Carpool/Carpool.tsx b/src/components/Carpool/Carpool.tsx index e0dbaa5..f696f31 100644 --- a/src/components/Carpool/Carpool.tsx +++ b/src/components/Carpool/Carpool.tsx @@ -160,7 +160,9 @@ export default function Carpool({ id }: { id: number }) { leave, }} > - +

{carpool.name}

{carpool.event.name} diff --git a/src/components/Carpool/CarpoolMap.tsx b/src/components/Carpool/CarpoolMap.tsx index 34dfeab..0b97536 100644 --- a/src/components/Carpool/CarpoolMap.tsx +++ b/src/components/Carpool/CarpoolMap.tsx @@ -39,7 +39,7 @@ const CarpoolMap = () => { google={google} style={{ width: '100%', height: '100%' }} containerStyle={{ - width: '30rem', + width: 'min(30rem, 100%)', height: '25rem', position: 'relative', borderRadius: '0.5rem', diff --git a/src/components/Carpool/MemberList.tsx b/src/components/Carpool/MemberList.tsx index 431ba3b..9f22fdd 100644 --- a/src/components/Carpool/MemberList.tsx +++ b/src/components/Carpool/MemberList.tsx @@ -40,7 +40,7 @@ export default function MemberList() { () => carpool.members.map((member) => member.id), [carpool] ); - const members = useSignups(carpool.id, memberIDs); + const members = useSignups(carpool.event.id, memberIDs); console.log(members); const membersToShow = useMemo( diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx index 9fcc058..e7d5293 100644 --- a/src/components/Header/Header.tsx +++ b/src/components/Header/Header.tsx @@ -8,7 +8,7 @@ import UIPrimaryTitle from '../UI/UIPrimaryTitle'; export default function Header() { const me = useMe(); - const notifications = useNotifications(); + const [notifications, refreshNotifications] = useNotifications(); return (
Log out
{notifications.length > 0 ? ( - + ) : ( No notifications )} diff --git a/src/components/Notifications/Notification.tsx b/src/components/Notifications/Notification.tsx index 76500f8..623be32 100644 --- a/src/components/Notifications/Notification.tsx +++ b/src/components/Notifications/Notification.tsx @@ -1,45 +1,44 @@ -import { useCallback } from 'react'; -import { - acceptInvite, - acceptCarpoolRequest, - denyInvite, - denyCarpoolRequest, -} from '../api'; +import { useCallback, useContext } from 'react'; +import { NotificationsContext } from '../../state/Notifications/NotificationsProvider'; +import { acceptCarpoolRequest, denyCarpoolRequest } from '../api'; import { IInvitation } from '../types'; import UIButton from '../UI/UIButton'; export default function Notification({ notification, + refresh, }: { notification: IInvitation; + refresh: () => void; }) { const carpoolId = notification.carpool.id; + const notifs = useContext(NotificationsContext); + const acceptReq = useCallback(() => { - acceptCarpoolRequest(carpoolId, notification.user.id); - }, [carpoolId, notification.user.id]); + acceptCarpoolRequest(carpoolId, notification.user.id).finally(refresh); + }, [carpoolId, notification.user.id, refresh]); const rejectReq = useCallback(() => { - denyCarpoolRequest(carpoolId, notification.user.id); - }, [carpoolId, notification.user.id]); + denyCarpoolRequest(carpoolId, notification.user.id).finally(refresh); + }, [carpoolId, notification.user.id, refresh]); const acceptInv = useCallback(() => { - acceptInvite(carpoolId); - }, [carpoolId]); + notifs.acceptCarpoolInvite(carpoolId); + }, [carpoolId, notifs]); const rejectInv = useCallback(() => { - denyInvite(carpoolId); - }, [carpoolId]); + notifs.denyCarpoolInvite(carpoolId); + }, [carpoolId, notifs]); const sentTime = new Date(notification.sentTime); return (
- {notification.user.name}{' '} {notification.isRequest ? ( - requested to join + {notification.user.name} requested to join ) : ( - invited you to join + You're invited to join )}{' '} {notification.carpool.name + ' at ' + sentTime.toLocaleString()} {notification.isRequest ? ( diff --git a/src/components/Notifications/Notifications.tsx b/src/components/Notifications/Notifications.tsx index 9e8ab9a..8846a21 100644 --- a/src/components/Notifications/Notifications.tsx +++ b/src/components/Notifications/Notifications.tsx @@ -3,8 +3,10 @@ import { IInvitation } from '../types'; export default function Notifications({ notifications, + refresh, }: { notifications: IInvitation[]; + refresh: () => void; }) { return (
@@ -12,6 +14,7 @@ export default function Notifications({ {notifications.map((notification) => ( ))} diff --git a/src/components/api.ts b/src/components/api.ts index 53faf81..a22c94e 100644 --- a/src/components/api.ts +++ b/src/components/api.ts @@ -245,7 +245,7 @@ export async function leaveCarpool(carpoolId: number) { } export async function sendCarpoolRequest(carpoolId: number) { - await post('/carpools/' + carpoolId + '/request', {}); + await post(`/carpools/${carpoolId}/request`, {}); } export async function cancelCarpoolRequest(carpoolId: number) { diff --git a/src/components/hooks.ts b/src/components/hooks.ts index 69a37a0..d4ba820 100644 --- a/src/components/hooks.ts +++ b/src/components/hooks.ts @@ -1,4 +1,4 @@ -import { useContext, useEffect, useState } from 'react'; +import { useCallback, useContext, useEffect, useState } from 'react'; import { getReceivedInvitationsAndRequests } from './api'; import AuthenticationContext from './Authentication/AuthenticationContext'; import { IInvitation } from './types'; @@ -9,9 +9,11 @@ export const useMe = () => useAuth().user; export function useNotifications() { const [notifications, setNotifications] = useState([]); - useEffect(() => { + const refresh = useCallback(() => { getReceivedInvitationsAndRequests().then(setNotifications); }, []); - return notifications; + useEffect(refresh, [refresh]); + + return [notifications, refresh] as const; }