fix clientside race to update cache of item upvote
This commit is contained in:
parent
91a2061342
commit
d204f4b6b8
@ -15,6 +15,16 @@ export default function UpVote ({ itemId, meSats, className }) {
|
|||||||
vote(id: $id, sats: $sats)
|
vote(id: $id, sats: $sats)
|
||||||
}`, {
|
}`, {
|
||||||
update (cache, { data: { vote } }) {
|
update (cache, { data: { vote } }) {
|
||||||
|
// read in the cached object so we don't use meSats prop
|
||||||
|
// which can be stale
|
||||||
|
const item = cache.readFragment({
|
||||||
|
id: `Item:${itemId}`,
|
||||||
|
fragment: gql`
|
||||||
|
fragment votedItem on Item {
|
||||||
|
meSats
|
||||||
|
}
|
||||||
|
`
|
||||||
|
})
|
||||||
cache.modify({
|
cache.modify({
|
||||||
id: `Item:${itemId}`,
|
id: `Item:${itemId}`,
|
||||||
fields: {
|
fields: {
|
||||||
@ -22,10 +32,10 @@ export default function UpVote ({ itemId, meSats, className }) {
|
|||||||
return existingMeSats + vote
|
return existingMeSats + vote
|
||||||
},
|
},
|
||||||
sats (existingSats = 0) {
|
sats (existingSats = 0) {
|
||||||
return meSats === 0 ? existingSats + vote : existingSats
|
return item.meSats === 0 ? existingSats + vote : existingSats
|
||||||
},
|
},
|
||||||
boost (existingBoost = 0) {
|
boost (existingBoost = 0) {
|
||||||
return meSats >= 1 ? existingBoost + vote : existingBoost
|
return item.meSats >= 1 ? existingBoost + vote : existingBoost
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user