Skip to content

Commit

Permalink
PR Feedback & Use query key path in deleteBasket cache
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraya committed Mar 21, 2023
1 parent 47bd1d8 commit 5c60483
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
8 changes: 6 additions & 2 deletions packages/commerce-sdk-react/src/hooks/ShopperBaskets/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,12 @@ export const cacheUpdateMatrix: CacheUpdateMatrix<Client> = {
// TODO: Convert invalidate to an update that removes the matching basket
...invalidateCustomerBasketsQuery(customerId, parameters),
remove: [
{queryKey: getBasket.queryKey(parameters)},
{queryKey: getPaymentMethodsForBasket.queryKey(parameters)}
// We want to fuzzy match all query keys that contain starting with the path
// `["/organizations/",${organization},"/baskets/",${basketId}]`
{queryKey: getBasket.path(parameters)},
...(customerId
? [{queryKey: getCustomerBaskets.queryKey({...parameters, customerId})}]
: [])
]
}
},
Expand Down
9 changes: 2 additions & 7 deletions packages/commerce-sdk-react/src/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,10 @@ export type CacheUpdateUpdate<T> = {
}

/** Query predicate for queries to invalidate */
export type CacheUpdateInvalidate =
| InvalidateQueryFilters
// TODO: Change Shopper Baskets cache logic from using predicates to creating query filters
// using the query key helpers, then this 'predicate' type can be removed, as can the one in
// `CacheUpdateRemove`. The predicate helpers in `utils.ts` should also then be removed.
| NonNullable<InvalidateQueryFilters['predicate']>
export type CacheUpdateInvalidate = InvalidateQueryFilters

/** Query predicate for queries to remove */
export type CacheUpdateRemove = QueryFilters | NonNullable<QueryFilters['predicate']>
export type CacheUpdateRemove = QueryFilters

/** Collection of updates to make to the cache when a request completes. */
export type CacheUpdate = {
Expand Down
8 changes: 2 additions & 6 deletions packages/commerce-sdk-react/src/hooks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ import {
/** Applies the set of cache updates to the query client. */
export const updateCache = (queryClient: QueryClient, cacheUpdates: CacheUpdate, data: unknown) => {
cacheUpdates.invalidate?.forEach((invalidate) => {
// TODO: Update Shopper Baskets cache logic to not use predicate functions, and then this
// check will no longer be needed. (Same for the remove block.)
const filters = typeof invalidate === 'function' ? {predicate: invalidate} : invalidate
queryClient.invalidateQueries(filters)
queryClient.invalidateQueries(invalidate)
})
cacheUpdates.remove?.forEach((remove) => {
const filters = typeof remove === 'function' ? {predicate: remove} : remove
queryClient.removeQueries(filters)
queryClient.removeQueries(remove)
})
cacheUpdates.update?.forEach(({queryKey, updater}) =>
// If an updater isn't given, fall back to just setting the data
Expand Down

0 comments on commit 5c60483

Please sign in to comment.