smarter cache lookups for "do we own/want this?"
This commit is contained in:
parent
2cfafce768
commit
3ecdd265c2
2 changed files with 51 additions and 3 deletions
|
@ -198,8 +198,8 @@ function ItemPageOwnWantButtons({ itemId }) {
|
|||
{
|
||||
variables: { itemId },
|
||||
onCompleted: (data) => {
|
||||
setCurrentUserOwnsThis(data.item.currentUserOwnsThis);
|
||||
setCurrentUserWantsThis(data.item.currentUserWantsThis);
|
||||
setCurrentUserOwnsThis(data?.item?.currentUserOwnsThis || false);
|
||||
setCurrentUserWantsThis(data?.item?.currentUserWantsThis || false);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
@ -208,6 +208,7 @@ function ItemPageOwnWantButtons({ itemId }) {
|
|||
return <Box color="red.400">{error.message}</Box>;
|
||||
}
|
||||
|
||||
// TODO: Focus state!
|
||||
return (
|
||||
<Box display="flex">
|
||||
<Skeleton isLoaded={!loading} marginRight="4">
|
||||
|
@ -215,7 +216,7 @@ function ItemPageOwnWantButtons({ itemId }) {
|
|||
<VisuallyHidden
|
||||
as="input"
|
||||
type="checkbox"
|
||||
isChecked={currentUserOwnsThis}
|
||||
checked={currentUserOwnsThis}
|
||||
onChange={(e) => {
|
||||
setCurrentUserOwnsThis(e.target.checked);
|
||||
toast({
|
||||
|
|
|
@ -78,6 +78,53 @@ const typePolicies = {
|
|||
return undefined;
|
||||
}
|
||||
},
|
||||
|
||||
currentUserOwnsThis: (cachedValue, { readField }) => {
|
||||
if (cachedValue != null) {
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
// Do we know what items this user owns? If so, scan for this item.
|
||||
const currentUserRef = readField("currentUser", {
|
||||
__ref: "ROOT_QUERY",
|
||||
});
|
||||
if (!currentUserRef) {
|
||||
return undefined;
|
||||
}
|
||||
const thisItemId = readField("id");
|
||||
const itemsTheyOwn = readField("itemsTheyOwn", currentUserRef);
|
||||
if (!itemsTheyOwn) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const theyOwnThisItem = itemsTheyOwn.some(
|
||||
(itemRef) => readField("id", itemRef) === thisItemId
|
||||
);
|
||||
return theyOwnThisItem;
|
||||
},
|
||||
currentUserWantsThis: (cachedValue, { readField }) => {
|
||||
if (cachedValue != null) {
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
// Do we know what items this user owns? If so, scan for this item.
|
||||
const currentUserRef = readField("currentUser", {
|
||||
__ref: "ROOT_QUERY",
|
||||
});
|
||||
if (!currentUserRef) {
|
||||
return undefined;
|
||||
}
|
||||
const thisItemId = readField("id");
|
||||
const itemsTheyWant = readField("itemsTheyWant", currentUserRef);
|
||||
if (!itemsTheyWant) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const theyWantThisItem = itemsTheyWant.some(
|
||||
(itemRef) => readField("id", itemRef) === thisItemId
|
||||
);
|
||||
return theyWantThisItem;
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue