From 7a897a142bcb5dcc3acd1e765d89bc70bebe164e Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Sun, 11 Apr 2021 10:58:04 -0400 Subject: [PATCH] improve groups UI --- src/components/Group.tsx | 35 +++++++++---------------- src/components/Nav.tsx | 7 ++++- src/components/Pool.tsx | 52 ++++++++++--------------------------- src/components/PoolPage.tsx | 21 +++++++++++++++ 4 files changed, 52 insertions(+), 63 deletions(-) create mode 100644 src/components/PoolPage.tsx diff --git a/src/components/Group.tsx b/src/components/Group.tsx index 7afdd3c..f1ed356 100644 --- a/src/components/Group.tsx +++ b/src/components/Group.tsx @@ -1,13 +1,10 @@ import Button from '@material-ui/core/Button'; -import Card from '@material-ui/core/Card'; import Typography from '@material-ui/core/Typography'; -import { useEffect, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { useParams } from 'react-router-dom'; import { makeAPIGetCall } from '../api/utils'; import CreatePool from './CreatePool'; - -const maybePluralize = (count: number, noun: string, suffix = 's') => - `${count} ${noun}${count !== 1 ? suffix : ''}`; +import Pool from './Pool'; const SAMPLE_POOLS: Carpool.Pool[] = [ { @@ -41,9 +38,17 @@ export default function Group() { const { id } = useParams<{ id: string }>(); const [error, setError] = useState(false); const [group, setGroup] = useState(); - const [pools, setPools] = useState(SAMPLE_POOLS); + const [pools, setPools] = useState([]); const [createPoolVisible, setCreatePoolVisible] = useState(false); + const fetchPools = useCallback(() => { + makeAPIGetCall(`/groups/${id}/pools`).then((res) => { + setPools(res.data.data); + }); + }, [id]); + + useEffect(() => fetchPools(), [fetchPools]); + useEffect(() => { makeAPIGetCall(`/groups/${id}`).then((res) => { if ('error' in res.data) { @@ -52,10 +57,6 @@ export default function Group() { setGroup(res.data.data); } }); - - makeAPIGetCall(`/groups/${id}/pools`).then((res) => { - setPools(res.data.data); - }); }, [id]); if (error) { @@ -93,19 +94,7 @@ export default function Group() { {createPoolVisible && } {pools.map((pool, index) => ( - - - {pool.title} - -

- Capacity: {pool.participant_ids.length} / {pool.capacity} -

-

Start Time: {pool.start_time}

-

End Time: {pool.end_time}

-

- {maybePluralize(pool.comments.length, 'comment')} -

-
+ ))} diff --git a/src/components/Nav.tsx b/src/components/Nav.tsx index 0081a4a..eef593d 100644 --- a/src/components/Nav.tsx +++ b/src/components/Nav.tsx @@ -19,6 +19,11 @@ const useStyles = makeStyles({ textTransform: `uppercase`, color: `white`, }, + 'linkText:hover': { + textDecoration: `none`, + textTransform: `uppercase`, + color: `white`, + }, }); const navLinks = [ { title: `Profile`, path: `/profile` }, @@ -28,7 +33,7 @@ const navLinks = [ const Nav = () => { const classes = useStyles(); return ( - + diff --git a/src/components/Pool.tsx b/src/components/Pool.tsx index b988b46..2e6330d 100644 --- a/src/components/Pool.tsx +++ b/src/components/Pool.tsx @@ -1,11 +1,10 @@ -import { useState, useEffect, useCallback, useRef, useContext } from 'react'; -import { useParams } from 'react-router'; +import { useState, useCallback, useRef, useContext } from 'react'; import Button from '@material-ui/core/Button'; import Card from '@material-ui/core/Card'; import Textarea from '@material-ui/core/TextareaAutosize'; import Typography from '@material-ui/core/Typography'; import Comment from './Comment'; -import { makeAPIGetCall, makeAPIPostCall } from '../api/utils'; +import { makeAPIPostCall } from '../api/utils'; import AuthenticationContext from './AuthenticationContext'; import PoolMap from './PoolMap'; @@ -35,9 +34,13 @@ const SAMPLE_POOL = { type: 'offer', }; -export default function Pool() { - const id = useParams<{ id: string }>().id; - const [pool, setPool] = useState(); +export default function Pool({ + pool, + triggerUpdate, +}: { + pool: Carpool.Pool; + triggerUpdate: Function; +}) { const { user } = useContext(AuthenticationContext); const commentTextareaRef = useRef(null); @@ -76,41 +79,12 @@ export default function Pool() { ); const onRegister = useCallback(() => { - if (user) { - let userID = user._id; - makeAPIPostCall(`/pools/${id}/join`).then(() => { - if (pool) { - setPool({ - ...pool, - participant_ids: [...pool.participant_ids, userID], - }); - } - }); - } - }, [user, id, pool]); + makeAPIPostCall(`/pools/${pool._id}/join`).then(() => triggerUpdate()); + }, [pool._id, triggerUpdate]); 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, pool]); + makeAPIPostCall(`/pools/${pool._id}/leave`).then(() => triggerUpdate()); + }, [pool._id, triggerUpdate]); return ( diff --git a/src/components/PoolPage.tsx b/src/components/PoolPage.tsx new file mode 100644 index 0000000..c9c4929 --- /dev/null +++ b/src/components/PoolPage.tsx @@ -0,0 +1,21 @@ +import { useCallback, useEffect, useState } from 'react'; +import { useParams } from 'react-router-dom'; +import { makeAPIGetCall } from '../api/utils'; +import Pool from './Pool'; + +export default function PoolPage() { + const id = useParams<{ id: string }>().id; + const [pool, setPool] = useState(); + + const fetchData = useCallback(() => { + makeAPIGetCall(`/pools/${id}`).then((response) => { + if (response.data.data) { + setPool(response.data.data); + } + }); + }, [id]); + + useEffect(() => fetchData(), [fetchData]); + + return pool ? : null; +}