-
Notifications
You must be signed in to change notification settings - Fork 147
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
[V3][Hooks Integration 🪝] Manually update cache for ShopperCustomer #1113
Conversation
…SalesforceCommerceCloud/pwa-kit into shopper-customers-cache-updates
* @param update | ||
* @returns | ||
*/ | ||
const createUpdateFunction = |
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.
Open to feedback on these types and this function generator @wjhsf
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.
There's a lot of repeated patterns, so most of my comments apply to multiple places. But I've only left them once.
{queryKey: getCustomerProductLists.queryKey(parameters)} | ||
] | ||
// We can only update cache for this product list item if we have the ID | ||
// QUESTION: Why would we get a response that doesn't have an ID? |
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.
The RAML spec does not guarantee that it will be present. You'll have to ask the API team about that.
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com>
Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com>
Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com>
Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com>
…SalesforceCommerceCloud/pwa-kit into shopper-customers-cache-updates
@@ -12,7 +12,7 @@ | |||
|
|||
/* Language and Environment */ | |||
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, | |||
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ | |||
"lib": ["dom", "es2019"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ |
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.
If we have target: es2016
, should we also use the es2016
lib, so that we only use available features? Or are we fine with the compiler inserting things that we need?
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.
From what I read there is a kind of chaining that would be happening es2019 uses es2018 and it goes down the line. Anyway. I needed to add this to get the deep clone implementation working using the entries
method. In general I'm not 100% sure if the solution is 100% correct. But it works as is, so maybe revisit it as needed.
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.
I'm fairly confident that this is fine from a "code that compiles and runs" standpoint. The thing I'm not sure about is how the es2019 code gets changed to es2016 code, particularly the impact on bundle size. Might be worth investigating as a separate ticket. (Outcome would probably either be "this is fine" or "don't use modern code so we don't add polyfill bloat".)
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com>
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
packages/commerce-sdk-react/src/hooks/ShopperCustomers/cache.ts
Outdated
Show resolved
Hide resolved
const itemId = response.id | ||
if (!itemId) return {invalidate} | ||
|
||
return { | ||
invalidate, | ||
update: [{queryKey: getCustomerProductListItem.queryKey({...parameters, itemId})}] | ||
update: [ | ||
{ | ||
queryKey: getCustomerProductListItem.queryKey({...parameters, itemId}) |
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.
According to the RAML spec, we could get a response
with no id
, meaning this update could potentially be storing data in the cache that we never actually retrieve (itemId: undefined
).
In practice, though, I don't know when we'd ever not receive an id
.
* v3: [Spike]Replace watch with nodemon (#1146) Fix Page Designer ImageWithText Link component (#1092) (#1148) Upgrade deprecated dependencies (#1124) Restart login flow if refresh_token is invalid (#1135) Move some util to site-utils to avoid circular imports (#1133) Restore old file name. (#1140) Update eslint configuration (#1129) CI: clarify the environment variables (#1127) Remove react-query-devtools from production build (#1121) Update lerna.json (#1118) Parallelize lighthouse ci (#1126) Increase test timeouts only on CI env (#1123) Remove unused `request` deprecated dependency. (#1125) Upgrade msw to latest (#1100) Add mergeBasket hook (#1114) [V3][Hooks Integration 🪝] Manually update cache for ShopperCustomer (#1113) dont use callback on mutateAsync (#1119) 2-spaces not 4-spaces (#1117) # Conflicts: # packages/internal-lib-build/package-lock.json # packages/pwa-kit-create-app/package-lock.json # packages/pwa-kit-dev/package-lock.json # packages/pwa-kit-dev/package.json # packages/pwa-kit-dev/src/configs/webpack/config.js # packages/pwa-kit-dev/src/ssr/server/build-dev-server.js # packages/pwa-kit-react-sdk/package-lock.json # packages/pwa-kit-runtime/package-lock.json # packages/template-express-minimal/package-lock.json # packages/template-mrt-reference-app/package-lock.json # packages/template-retail-react-app/package-lock.json # packages/template-typescript-minimal/package-lock.json # packages/test-commerce-sdk-react/package-lock.json
…-rehaul * feature/template-extensibility: [Spike]Replace watch with nodemon (#1146) Fix Page Designer ImageWithText Link component (#1092) (#1148) Upgrade deprecated dependencies (#1124) Restart login flow if refresh_token is invalid (#1135) Move some util to site-utils to avoid circular imports (#1133) Restore old file name. (#1140) Update eslint configuration (#1129) CI: clarify the environment variables (#1127) Remove react-query-devtools from production build (#1121) Update lerna.json (#1118) Parallelize lighthouse ci (#1126) Increase test timeouts only on CI env (#1123) Remove unused `request` deprecated dependency. (#1125) Upgrade msw to latest (#1100) Add mergeBasket hook (#1114) [V3][Hooks Integration 🪝] Manually update cache for ShopperCustomer (#1113) dont use callback on mutateAsync (#1119) 2-spaces not 4-spaces (#1117) # Conflicts: # packages/internal-lib-build/package-lock.json # packages/pwa-kit-dev/package-lock.json # packages/pwa-kit-react-sdk/package-lock.json # packages/pwa-kit-runtime/package-lock.json # packages/pwa-kit-runtime/package.json
Description
In this PR we leverage the ability to proactively update cache without having to invalidate and refetch what we already have. For example, when we create and post a new customer address, we are returned the customer object with the new address assigned to it. Instead of invalidating the current cache for the customer, which would in turn cause a fetch to get new data, we can take the updated customer and update the cache client side.
We do this throughout the ShopperCustomer endpoints, below is a list of when we manually update cache:
NOTES
clone
as to not mutate the values passed to updater methods. They might have observers attached to them.Right now I am bailing when there is no cache item to be updated, for example if you add an address to a known customer and you have no previously loaded said customer the "oldData" value for the customer updater would be undefined. Since the response has the full customer, we can proactively update the cache with the its first value. I'm working on this.I'm working on DRY-ing up the code, right there is a lot of duplication.Clone 3PP ✅
Types of Changes
Changes
How to Test-Drive This PR
commerce-sdk-react
Checklists
General