diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/PollCard/PollCard.tsx | 18 | ||||
-rw-r--r-- | src/components/PollsList/PollsList.tsx (renamed from src/components/Feed/Feed.tsx) | 20 |
2 files changed, 26 insertions, 12 deletions
diff --git a/src/components/PollCard/PollCard.tsx b/src/components/PollCard/PollCard.tsx index 2378945..689e872 100644 --- a/src/components/PollCard/PollCard.tsx +++ b/src/components/PollCard/PollCard.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { Card, @@ -14,7 +14,8 @@ import { post } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; interface PropTypes { - initialPoll: Poll; + poll: Poll; + setPoll: (poll: Poll) => void; } const DATE_FORMAT = { @@ -49,8 +50,7 @@ const useStyles = makeStyles(theme => ({ } })); -const PollCard: React.FC<PropTypes> = ({ initialPoll }) => { - const [poll, setPoll] = useState<Poll>(initialPoll); +const PollCard: React.FC<PropTypes> = ({ poll, setPoll }) => { const classes = useStyles(); const { author, contents: { left, right }, vote } = poll; const { enqueueSnackbar } = useSnackbar(); @@ -68,15 +68,17 @@ const PollCard: React.FC<PropTypes> = ({ initialPoll }) => { }); } else { const newVote = ({ which, pollId: poll._id }); - post('votes/', newVote); - poll.contents[which].votes += 1; - poll.vote = { + const newPoll = { ...poll }; + newPoll.contents[which].votes += 1; + newPoll.vote = { _id: '', authorId: '', createdAt: new Date(), ...newVote }; - setPoll({ ...poll }); + setPoll(newPoll); + + post('votes/', newVote); } }; diff --git a/src/components/Feed/Feed.tsx b/src/components/PollsList/PollsList.tsx index bf3c5b7..0fd8fa4 100644 --- a/src/components/Feed/Feed.tsx +++ b/src/components/PollsList/PollsList.tsx @@ -6,6 +6,7 @@ import PollCard from '../PollCard/PollCard'; interface PropTypes { polls: Poll[]; + mutate: (polls: Poll[], refetch: boolean) => void; } interface RenderPropTypes { @@ -15,13 +16,24 @@ interface RenderPropTypes { } -const Feed: React.FC<PropTypes> = ({ polls }) => { +const PollsList: React.FC<PropTypes> = ({ polls, mutate }) => { + const RenderItem: React.FC<RenderPropTypes> = ({ index, style, key }) => { const poll = polls[index]; + + const setPoll = (poll: Poll) => { + const newPolls = [...polls]; + newPolls[index] = poll; + + // Force-update list-size so everything re-renders + mutate([], false); + mutate(newPolls, false); + }; + return ( // To re-render on list resize, add this info to key - <div key={key + polls.length} style={style}> - <PollCard initialPoll={poll} /> + <div key={`${key}-${poll._id}-${polls.length}`} style={style}> + <PollCard poll={poll} setPoll={setPoll} /> </div> ); }; @@ -59,5 +71,5 @@ const Feed: React.FC<PropTypes> = ({ polls }) => { ); }; -export default Feed; +export default PollsList; |