From c1f192fb1e5eedd87255af8d4e71a42bf2d93814 Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Tue, 13 Jul 2021 12:56:39 -0400 Subject: [PATCH] make carpools list respond to whether you are already in a carpool or not --- src/components/Event/EventCarpools.tsx | 51 +++++++++++++++++--------- src/components/types.ts | 2 +- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/components/Event/EventCarpools.tsx b/src/components/Event/EventCarpools.tsx index 345f622..ec7aabf 100644 --- a/src/components/Event/EventCarpools.tsx +++ b/src/components/Event/EventCarpools.tsx @@ -1,5 +1,6 @@ // import CallMergeIcon from '@material-ui/icons/CallMerge'; import EmojiPeopleIcon from '@material-ui/icons/EmojiPeople'; +import { useMemo } from 'react'; import { useCallback, useState } from 'react'; import { createCarpool } from '../api'; import { lightgrey } from '../colors'; @@ -44,10 +45,19 @@ function CarpoolRow({ carpool }: { carpool: IEvent['carpools'][0] }) { ); } +type CreationStatus = null | 'pending' | 'completed' | 'errored'; + export default function Carpools({ event }: { event: IEvent }) { - const [creationStatus, setCreationStatus] = - useState(null); + const [creationStatus, setCreationStatus] = useState(null); const me = useMe()!; + const myCarpool = useMemo( + () => + event.carpools.find((carpool) => + carpool.members.some((member) => member.id === me.id) + ), + [event.carpools, me.id] + ); + const alreadyInCarpool = myCarpool !== undefined; const createEmptyCarpool = useCallback(() => { setCreationStatus('pending'); @@ -65,21 +75,28 @@ export default function Carpools({ event }: { event: IEvent }) { return (
-

Carpools

-
- <>Available to drive? - - {creationStatus === null - ? 'Create Empty Carpool' - : creationStatus === 'pending' - ? 'Creating...' - : creationStatus === 'completed' - ? 'Created!' - : 'Errored'} - +

Carpools

+ {alreadyInCarpool ? ( + You are already in a carpool for this event + ) : ( + <> + Available to drive? + {creationStatus !== 'completed' ? ( + + {creationStatus === null + ? 'Create Empty Carpool' + : creationStatus === 'pending' + ? 'Creating...' + : 'Errored'} + + ) : ( + 'Created!' + )} + + )} {event.carpools.map((carpool) => ( ))} diff --git a/src/components/types.ts b/src/components/types.ts index a6139d2..fc80a5f 100644 --- a/src/components/types.ts +++ b/src/components/types.ts @@ -63,7 +63,7 @@ export type IEvent = { members: { id: number; name: string; - }; + }[]; }[]; startTime: string; // Datestring duration: number;