From 10ff0baa50dcb75a61046b2c706628f0d689b47f Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Tue, 13 Jul 2021 18:11:17 -0400 Subject: [PATCH] add leave carpool button --- src/components/Carpool/Carpool.tsx | 31 ++++++++++++++-- src/components/Carpool/MemberList.tsx | 51 +++++++++++++++------------ src/components/api.ts | 4 +++ 3 files changed, 62 insertions(+), 24 deletions(-) diff --git a/src/components/Carpool/Carpool.tsx b/src/components/Carpool/Carpool.tsx index 6a94a91..30ec6c8 100644 --- a/src/components/Carpool/Carpool.tsx +++ b/src/components/Carpool/Carpool.tsx @@ -1,7 +1,12 @@ import MailOutlineIcon from '@material-ui/icons/MailOutline'; import PersonAddIcon from '@material-ui/icons/PersonAdd'; import { createContext, useCallback, useEffect, useState } from 'react'; -import { cancelCarpoolInvite, getCarpool, sendCarpoolInvite } from '../api'; +import { + cancelCarpoolInvite, + getCarpool, + leaveCarpool, + sendCarpoolInvite, +} from '../api'; import { lightgrey } from '../colors'; import { ICarpool } from '../types'; import UIButton from '../UI/UIButton'; @@ -19,6 +24,9 @@ export const CarpoolContext = createContext({ cancelInvite: (user: { id: number; name: string }) => { console.error('not implemented: cancelInvite'); }, + leave: () => { + console.error('not implemented: leave'); + }, }); export default function Carpool({ id }: { id: number }) { @@ -75,12 +83,31 @@ export default function Carpool({ id }: { id: number }) { [id] ); + const eventId = carpool?.eventId; + + const leave = useCallback(() => { + if (eventId) { + leaveCarpool(id) + .then(() => { + window.location.href = '/events/' + eventId; + }) + .catch(console.error); + } + }, [eventId, id]); + if (!carpool) { return <>Loading...; } return ( - + {carpool ? ( <> diff --git a/src/components/Carpool/MemberList.tsx b/src/components/Carpool/MemberList.tsx index 839825b..aac57ad 100644 --- a/src/components/Carpool/MemberList.tsx +++ b/src/components/Carpool/MemberList.tsx @@ -1,4 +1,17 @@ import AccountCircleIcon from '@material-ui/icons/AccountCircle'; +import { useContext } from 'react'; +import { lightgrey } from '../colors'; +import UIButton from '../UI/UIButton'; +import { CarpoolContext } from './Carpool'; + +function MemberRow({ member }: { member: { id: number; name: string } }) { + return ( +
+ +
{member.name}
+
+ ); +} export default function MemberList({ members, @@ -8,42 +21,36 @@ export default function MemberList({ name: string; }[]; }) { + const { leave } = useContext(CarpoolContext); + const membersToShow = members.slice(0, 2); + const hiddenMemberCount = members.length - membersToShow.length; + return (

Members

{members.length > 0 ? ( -
- {members.map((member, index) => { - return index < 2 ? ( -
- -
{member.name}
-
- ) : ( - '' - ); - })} - {members.length > 2 - ? members.length - 2 === 1 - ? members.length - 2 + ' other...' - : members.length - 2 + ' others...' - : ''}{' '} +
+ {membersToShow.map((member) => ( + + ))} + {hiddenMemberCount > 0 && + (hiddenMemberCount === 1 + ? hiddenMemberCount + ' other...' + : hiddenMemberCount + ' others...')}
) : ( 'This carpool has no members.' )} + + Leave +
); } diff --git a/src/components/api.ts b/src/components/api.ts index d2adbd3..1a484ef 100644 --- a/src/components/api.ts +++ b/src/components/api.ts @@ -189,3 +189,7 @@ export async function sendCarpoolInvite(carpoolId: number, userId: number) { export async function cancelCarpoolInvite(carpoolId: number, userId: number) { return await delete$('/carpools/' + carpoolId + '/invite', { userId }); } + +export async function leaveCarpool(carpoolId: number) { + return await post(`/carpools/${carpoolId}/leave`, {}); +}