From 2ef179048b375991b764efae4e1f08fe6caf967d Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Sun, 11 Apr 2021 00:56:38 -0400 Subject: [PATCH] feat: added unregister funcitonality --- src/components/Pool.tsx | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/components/Pool.tsx b/src/components/Pool.tsx index c2db0a1..b988b46 100644 --- a/src/components/Pool.tsx +++ b/src/components/Pool.tsx @@ -89,13 +89,28 @@ export default function Pool() { } }, [user, id, pool]); + const onUnregister = useCallback(() => { + if (user) { + makeAPIPostCall(`/pools/${id}/leave`).then(() => { + if (pool) { + let participants: string[] = []; + pool.participant_ids.forEach((e) => participants.push(e)); + setPool({ + ...pool, + participant_ids: [...participants], + }); + } + }); + } + }, [user, id, pool]); + useEffect(() => { makeAPIGetCall(`/pools/${id}`).then((response) => { if (response.data.data) { setPool(response.data.data); } }); - }, [id]); + }, [id, pool]); return ( @@ -119,7 +134,11 @@ export default function Pool() { variant="contained" color="primary" style={{ marginTop: '0.5rem' }} - onClick={onRegister} + onClick={ + pool.participant_ids?.includes(user._id) + ? onUnregister + : onRegister + } > {pool.participant_ids?.includes(user._id) ? 'Unregister'