make carpools list respond to whether you are already in a carpool or not

This commit is contained in:
Michael Fatemi 2021-07-13 12:56:39 -04:00
parent 43a16a57cc
commit c1f192fb1e
2 changed files with 35 additions and 18 deletions

View File

@ -1,5 +1,6 @@
// import CallMergeIcon from '@material-ui/icons/CallMerge'; // import CallMergeIcon from '@material-ui/icons/CallMerge';
import EmojiPeopleIcon from '@material-ui/icons/EmojiPeople'; import EmojiPeopleIcon from '@material-ui/icons/EmojiPeople';
import { useMemo } from 'react';
import { useCallback, useState } from 'react'; import { useCallback, useState } from 'react';
import { createCarpool } from '../api'; import { createCarpool } from '../api';
import { lightgrey } from '../colors'; 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 }) { export default function Carpools({ event }: { event: IEvent }) {
const [creationStatus, setCreationStatus] = const [creationStatus, setCreationStatus] = useState<CreationStatus>(null);
useState<null | 'pending' | 'completed' | 'errored'>(null);
const me = useMe()!; 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(() => { const createEmptyCarpool = useCallback(() => {
setCreationStatus('pending'); setCreationStatus('pending');
@ -65,21 +75,28 @@ export default function Carpools({ event }: { event: IEvent }) {
return ( return (
<div style={{ display: 'flex', flexDirection: 'column' }}> <div style={{ display: 'flex', flexDirection: 'column' }}>
<h3 style={{ marginBlockEnd: '0' }}>Carpools</h3> <h3 style={{ marginBottom: '0' }}>Carpools</h3>
<br /> {alreadyInCarpool ? (
<>Available to drive?</> <span>You are already in a carpool for this event</span>
<UIButton ) : (
onClick={createEmptyCarpool} <>
style={{ backgroundColor: lightgrey }} <span>Available to drive?</span>
> {creationStatus !== 'completed' ? (
{creationStatus === null <UIButton
? 'Create Empty Carpool' onClick={createEmptyCarpool}
: creationStatus === 'pending' style={{ backgroundColor: lightgrey }}
? 'Creating...' >
: creationStatus === 'completed' {creationStatus === null
? 'Created!' ? 'Create Empty Carpool'
: 'Errored'} : creationStatus === 'pending'
</UIButton> ? 'Creating...'
: 'Errored'}
</UIButton>
) : (
'Created!'
)}
</>
)}
{event.carpools.map((carpool) => ( {event.carpools.map((carpool) => (
<CarpoolRow carpool={carpool} key={carpool.id} /> <CarpoolRow carpool={carpool} key={carpool.id} />
))} ))}

View File

@ -63,7 +63,7 @@ export type IEvent = {
members: { members: {
id: number; id: number;
name: string; name: string;
}; }[];
}[]; }[];
startTime: string; // Datestring startTime: string; // Datestring
duration: number; duration: number;