Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ShopperBaskets] - added logic to update cache for multiple baskets on basket mutations #2148

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions packages/commerce-sdk-react/src/hooks/ShopperBaskets/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,42 @@ const invalidateCustomerBasketsQuery = (
}
}

const updateCustomerBasketsQuery = (
customerId: string,
parameters: BasketParameters,
response: Basket
): CacheUpdateUpdate<unknown> => {
const updateCustomerBasketsQuery = (customerId: string, parameters: BasketParameters, response: Basket): CacheUpdateUpdate<unknown> => {
return {
queryKey: getCustomerBaskets.queryKey({...parameters, customerId}),
updater: (oldData: CustomerBasketsResult | undefined) => {
// custom code to enable multiple baskets query upadte
let isBasketNew = true
let basketsTotal = 1
if (!oldData?.baskets?.length) {
return {
baskets: [response],
total: 1
total: basketsTotal
}
}

basketsTotal = oldData.total
const updatedBaskets = oldData.baskets.map((basket) => {
if (basket.basketId === parameters.basketId) {
isBasketNew = false
return response
} else {
return basket
}
})

if (isBasketNew) {
updatedBaskets[basketsTotal] = response
basketsTotal += 1
}
const updatedBaskets = oldData.baskets.map((basket) =>
basket.basketId === parameters.basketId ? response : basket
)

return {
...oldData,
// Shopper Customers and Shopper Baskets have different definitions for the `Basket`
// type. (99% similar, but that's not good enough for TypeScript.)
// TODO: Remove this type assertion when the RAML specs match.
baskets: updatedBaskets as CustomerBasketsResult['baskets']
baskets: updatedBaskets as CustomerBasketsResult['baskets'],
total: basketsTotal
}
}
}
Expand Down
Loading