From d59fabd47f04a7e0702638b529d396854513102e Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Thu, 3 Dec 2020 10:39:37 +0100 Subject: [PATCH] Poll is re-rendered --- src/components/Timelines/Timeline/Default.tsx | 16 ++++--- .../Timelines/Timeline/Shared/Poll.tsx | 42 ++++++++++--------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/components/Timelines/Timeline/Default.tsx b/src/components/Timelines/Timeline/Default.tsx index 65b2f1d7..edd04799 100644 --- a/src/components/Timelines/Timeline/Default.tsx +++ b/src/components/Timelines/Timeline/Default.tsx @@ -43,7 +43,7 @@ const TimelineDefault: React.FC = ({ item, queryKey }) => { )} {actualStatus.poll && ( - + )} {actualStatus.media_attachments.length > 0 && ( @@ -90,10 +90,14 @@ const styles = StyleSheet.create({ export default React.memo(TimelineDefault, (prev, next) => { let skipUpdate = true - skipUpdate = prev.item.id === next.item.id - skipUpdate = prev.item.replies_count === next.item.replies_count - skipUpdate = prev.item.favourited === next.item.favourited - skipUpdate = prev.item.reblogged === next.item.reblogged - skipUpdate = prev.item.bookmarked === next.item.bookmarked + skipUpdate = + prev.item.id === next.item.id && + prev.item.replies_count === next.item.replies_count && + prev.item.favourited === next.item.favourited && + prev.item.reblogged === next.item.reblogged && + prev.item.bookmarked === next.item.bookmarked && + prev.item.poll?.voted === next.item.poll?.voted && + prev.item.reblog?.poll?.voted === next.item.reblog?.poll?.voted + return skipUpdate }) diff --git a/src/components/Timelines/Timeline/Shared/Poll.tsx b/src/components/Timelines/Timeline/Shared/Poll.tsx index 40090bbf..418dcc44 100644 --- a/src/components/Timelines/Timeline/Shared/Poll.tsx +++ b/src/components/Timelines/Timeline/Shared/Poll.tsx @@ -25,7 +25,6 @@ const fireMutation = async ({ formData.append('choices[]', option) } }) - console.log(formData) const res = await client({ method: 'post', @@ -49,12 +48,10 @@ const fireMutation = async ({ export interface Props { queryKey: App.QueryKey - poll: Mastodon.Poll + status: Mastodon.Status } -const TimelinePoll: React.FC = ({ queryKey, poll }) => { - console.log('render poll ' + Math.random()) - console.log(poll) +const TimelinePoll: React.FC = ({ queryKey, status: { poll } }) => { const { theme } = useTheme() const queryCache = useQueryCache() @@ -68,25 +65,28 @@ const TimelinePoll: React.FC = ({ queryKey, poll }) => { toots: paging.toots.map((toot: any) => { if (toot.poll?.id === id) { const poll = toot.poll - console.log('update votes') - console.log( - Object.keys(options) - .filter(option => options[option]) - .map(option => parseInt(option)) + const myVotes = Object.keys(options).filter( + // @ts-ignore + option => options[option] ) - console.log(toot.poll) + const myVotesInt = myVotes.map(option => parseInt(option)) + toot.poll = { ...toot.poll, + votes_count: poll.votes_count + ? poll.votes_count + myVotes.length + : myVotes.length, voters_count: poll.voters_count ? poll.voters_count + 1 : 1, voted: true, - own_votes: [ - Object.keys(options) - // @ts-ignore - .filter(option => options[option]) - .map(option => parseInt(option)) - ] + own_votes: myVotesInt, + // @ts-ignore + options: poll.options.map((o, i) => { + if (myVotesInt.includes(i)) { + o.votes_count = o.votes_count + 1 + } + return o + }) } - console.log(toot.poll) } return toot }), @@ -217,7 +217,6 @@ const TimelinePoll: React.FC = ({ queryKey, poll }) => {