Poll is re-rendered

This commit is contained in:
Zhiyuan Zheng 2020-12-03 10:39:37 +01:00
parent 7d7c907fa3
commit d59fabd47f
No known key found for this signature in database
GPG Key ID: 078A93AB607D85E0
2 changed files with 32 additions and 26 deletions

View File

@ -43,7 +43,7 @@ const TimelineDefault: React.FC<Props> = ({ item, queryKey }) => {
<TimelineContent status={actualStatus} /> <TimelineContent status={actualStatus} />
)} )}
{actualStatus.poll && ( {actualStatus.poll && (
<TimelinePoll queryKey={queryKey} poll={actualStatus.poll} /> <TimelinePoll queryKey={queryKey} status={actualStatus} />
)} )}
{actualStatus.media_attachments.length > 0 && ( {actualStatus.media_attachments.length > 0 && (
<TimelineAttachment status={actualStatus} width={contentWidth} /> <TimelineAttachment status={actualStatus} width={contentWidth} />
@ -90,10 +90,14 @@ const styles = StyleSheet.create({
export default React.memo(TimelineDefault, (prev, next) => { export default React.memo(TimelineDefault, (prev, next) => {
let skipUpdate = true let skipUpdate = true
skipUpdate = prev.item.id === next.item.id skipUpdate =
skipUpdate = prev.item.replies_count === next.item.replies_count prev.item.id === next.item.id &&
skipUpdate = prev.item.favourited === next.item.favourited prev.item.replies_count === next.item.replies_count &&
skipUpdate = prev.item.reblogged === next.item.reblogged prev.item.favourited === next.item.favourited &&
skipUpdate = prev.item.bookmarked === next.item.bookmarked 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 return skipUpdate
}) })

View File

@ -25,7 +25,6 @@ const fireMutation = async ({
formData.append('choices[]', option) formData.append('choices[]', option)
} }
}) })
console.log(formData)
const res = await client({ const res = await client({
method: 'post', method: 'post',
@ -49,12 +48,10 @@ const fireMutation = async ({
export interface Props { export interface Props {
queryKey: App.QueryKey queryKey: App.QueryKey
poll: Mastodon.Poll status: Mastodon.Status
} }
const TimelinePoll: React.FC<Props> = ({ queryKey, poll }) => { const TimelinePoll: React.FC<Props> = ({ queryKey, status: { poll } }) => {
console.log('render poll ' + Math.random())
console.log(poll)
const { theme } = useTheme() const { theme } = useTheme()
const queryCache = useQueryCache() const queryCache = useQueryCache()
@ -68,25 +65,28 @@ const TimelinePoll: React.FC<Props> = ({ queryKey, poll }) => {
toots: paging.toots.map((toot: any) => { toots: paging.toots.map((toot: any) => {
if (toot.poll?.id === id) { if (toot.poll?.id === id) {
const poll = toot.poll const poll = toot.poll
console.log('update votes') const myVotes = Object.keys(options).filter(
console.log( // @ts-ignore
Object.keys(options) option => options[option]
.filter(option => options[option])
.map(option => parseInt(option))
) )
console.log(toot.poll) const myVotesInt = myVotes.map(option => parseInt(option))
toot.poll = { toot.poll = {
...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, voters_count: poll.voters_count ? poll.voters_count + 1 : 1,
voted: true, voted: true,
own_votes: [ own_votes: myVotesInt,
Object.keys(options) // @ts-ignore
// @ts-ignore options: poll.options.map((o, i) => {
.filter(option => options[option]) if (myVotesInt.includes(i)) {
.map(option => parseInt(option)) o.votes_count = o.votes_count + 1
] }
return o
})
} }
console.log(toot.poll)
} }
return toot return toot
}), }),
@ -217,7 +217,6 @@ const TimelinePoll: React.FC<Props> = ({ queryKey, poll }) => {
<Button <Button
onPress={() => { onPress={() => {
if (poll.multiple) { if (poll.multiple) {
console.log(multipleOptions)
mutateAction({ id: poll.id, options: multipleOptions }) mutateAction({ id: poll.id, options: multipleOptions })
} else { } else {
mutateAction({ id: poll.id, options: singleOptions }) mutateAction({ id: poll.id, options: singleOptions })
@ -299,4 +298,7 @@ const styles = StyleSheet.create({
} }
}) })
export default TimelinePoll export default React.memo(
TimelinePoll,
(prev, next) => prev.status.poll.voted === next.status.poll.voted
)