From d204f4b6b8dda269755a6352a3fdd6459ed999fd Mon Sep 17 00:00:00 2001 From: keyan Date: Tue, 7 Sep 2021 07:59:27 -0500 Subject: [PATCH] fix clientside race to update cache of item upvote --- components/upvote.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/components/upvote.js b/components/upvote.js index 96756b06..50c13923 100644 --- a/components/upvote.js +++ b/components/upvote.js @@ -15,6 +15,16 @@ export default function UpVote ({ itemId, meSats, className }) { vote(id: $id, sats: $sats) }`, { 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({ id: `Item:${itemId}`, fields: { @@ -22,10 +32,10 @@ export default function UpVote ({ itemId, meSats, className }) { return existingMeSats + vote }, sats (existingSats = 0) { - return meSats === 0 ? existingSats + vote : existingSats + return item.meSats === 0 ? existingSats + vote : existingSats }, boost (existingBoost = 0) { - return meSats >= 1 ? existingBoost + vote : existingBoost + return item.meSats >= 1 ? existingBoost + vote : existingBoost } } })