-
Notifications
You must be signed in to change notification settings - Fork 145
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
Update customer baskets cache when there is a basket mutation #945
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cc2ffa1
update customer baskets cache when there is a basket mutation
alexvuong 3e7a8ac
minor fix
alexvuong 25a085a
fixing tests
alexvuong f778ce9
fixing tests
alexvuong 3322b3d
typo
alexvuong 90777ee
adjust updater type on update
alexvuong 6457139
adjust updater type on update
alexvuong 6497546
rename basket id for test
alexvuong 2389613
Merge branch 'develop' into fix/update-basket-cache-for-mutations
alexvuong a5d1516
uncomment tests
alexvuong ada964c
fix type
alexvuong f82b7c8
linting
alexvuong acd23db
remove unwanted code
alexvuong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import {CacheUpdateMatrixElement, NotImplementedError, updateCache} from '../uti | |
import useCustomerId from '../useCustomerId' | ||
|
||
type Client = ApiClients['shopperBaskets'] | ||
type CustomerClient = ApiClients['shopperCustomers'] | ||
|
||
export const ShopperBasketsMutations = { | ||
/** | ||
|
@@ -189,15 +190,46 @@ export type ShopperBasketsMutationType = | |
* @private | ||
*/ | ||
export const getCacheUpdateMatrix = (customerId: string | null) => { | ||
const updateBasketQuery = (basketId?: string) => { | ||
const updateBasketQuery = ( | ||
basketId?: string, | ||
response?: DataType<Client[ShopperBasketsMutationType]> | ||
) => { | ||
// TODO: we're missing headers, rawResponse -> not only {basketId} | ||
const arg = {basketId} | ||
|
||
return basketId | ||
? { | ||
update: [ | ||
{ | ||
name: 'basket', | ||
key: ['/baskets', basketId, arg] | ||
key: ['/baskets', basketId, arg], | ||
updater: () => response | ||
}, | ||
{ | ||
// Since we use baskets from customer basket query, we need to update it for any basket mutation | ||
name: 'customerBaskets', | ||
key: ['/customers', customerId, '/baskets', {customerId}], | ||
updater: ( | ||
oldData: NonNullable<DataType<CustomerClient['getCustomerBaskets']>> | ||
) => { | ||
// do not update if responded basket does not exist inside existing customer baskets | ||
if ( | ||
!oldData?.baskets?.some( | ||
(basket) => basket.basketId === response?.basketId | ||
) | ||
) { | ||
return undefined | ||
} | ||
const updatedBaskets = oldData.baskets?.map( | ||
(basket: DataType<Client[ShopperBasketsMutationType]>) => { | ||
return basket?.basketId === basketId ? response : basket | ||
} | ||
) | ||
return { | ||
...oldData, | ||
baskets: updatedBaskets | ||
} | ||
} | ||
} | ||
] | ||
} | ||
|
@@ -241,8 +273,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
addItemToBasket: ( | ||
|
@@ -252,8 +283,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
removeItemFromBasket: ( | ||
|
@@ -263,8 +293,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params?.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
addPaymentInstrumentToBasket: ( | ||
|
@@ -274,18 +303,15 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
createBasket: ( | ||
params: Argument<Client['createBasket']>, | ||
response: DataType<Client['createBasket']> | ||
): CacheUpdateMatrixElement => { | ||
const basketId = response.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
// we want to re-fetch basket data in this case to get the basket total and other baskets data | ||
...invalidateCustomerBasketsQuery(customerId) | ||
} | ||
}, | ||
|
@@ -304,10 +330,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
params: Argument<Client['mergeBasket']>, | ||
response: DataType<Client['mergeBasket']> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we not get lint errors for unused method arguments in TS? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We currently do not set TS to prompt error for it. |
||
): CacheUpdateMatrixElement => { | ||
const basketId = response.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
} | ||
}, | ||
|
@@ -318,8 +341,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params?.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
removePaymentInstrumentFromBasket: ( | ||
|
@@ -329,8 +351,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params?.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
updateBasket: ( | ||
|
@@ -340,8 +361,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
updateBillingAddressForBasket: ( | ||
|
@@ -351,8 +371,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
updateCustomerForBasket: ( | ||
|
@@ -362,8 +381,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
updateItemInBasket: ( | ||
|
@@ -373,8 +391,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
updatePaymentInstrumentInBasket: ( | ||
|
@@ -384,8 +401,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
updateShippingAddressForShipment: ( | ||
|
@@ -395,8 +411,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
}, | ||
updateShippingMethodForShipment: ( | ||
|
@@ -406,8 +421,7 @@ export const getCacheUpdateMatrix = (customerId: string | null) => { | |
const basketId = params.parameters.basketId | ||
|
||
return { | ||
...updateBasketQuery(basketId), | ||
...invalidateCustomerBasketsQuery(customerId) | ||
...updateBasketQuery(basketId, response) | ||
} | ||
} | ||
} | ||
|
@@ -446,6 +460,7 @@ export function useShopperBasketsMutation<Action extends ShopperBasketsMutationT | |
} | ||
const queryClient = useQueryClient() | ||
const customerId = useCustomerId() | ||
|
||
const cacheUpdateMatrix = getCacheUpdateMatrix(customerId) | ||
|
||
return useMutation<Data, Error, Params>( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't the Create Basket endpoint return a basket? Could we not just use that data as we do with the other endpoints to
updateBasketQuery(basketId, response)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does return a basket, but we want to fetch the basket to get the correct value of
total
in customer basketsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok... I can get on board with that. I thing there will be room for improvement/optimization here tho. But we can address that in the next feature update as originally planned. 👍