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

fix: Cart item availability #1160

Merged
merged 2 commits into from
Feb 25, 2022
Merged

fix: Cart item availability #1160

merged 2 commits into from
Feb 25, 2022

Conversation

tlgimenes
Copy link
Contributor

@tlgimenes tlgimenes commented Feb 24, 2022

What's the purpose of this pull request?

This PR makes the cart module to respect the inventory limits of a certain product.

Currently, when adding an unavailable product to card, the UI let's the counter to increase indefinitely without warning the user the product has limited inventory. This PR addresses this issue by limiting the amount of products the user can add to cart. Also, if the user adds more itens, a toast message is raised warning the user

How it works?

Optimistic UI is a design pattern that improves greatly the UI responsiveness by not blocking the user/UI interaction while the server process requests. Our SDK's cart module is optimistic. This means that the user does not need to wait for the ecommerce platform to process anything before adding/removing items from cart.
To achieve an optimistic cart, the cart state is copied from the server into the client. Any change to cart state is performed locally and the UI reacts to these changes. At some point, the local cart state needs to be synced with the server one. This sync between client/server state is done by validateCart mutation. This mutation's return is the server's state of the cart and is, thus, the source of truth for the cart's state. If validateCart returns null, this means that both client and server state matches perfectly, and thus no change needs to be performed on the client.

There was a bug in validateCart mutation when the a product had a limited inventory. When the user tried to add more items than possible, the mutation returned null, indicating to the UI the operation was valid. When the user got to the checkout phase, he saw that the number of items on /checkout differs from the one present on the minicart, thus creating a mistrust with the store.
To solve this bug, when the user tries to add more itens than possible, the quantity is lowered to the available products and a message is returned saying the operation was impossible. More specifically, the bug happened because the equals function was not applied between server and client states, but between sever and server states. The fix was change equals to compare server and client states

Below, you can see the before/after comparison of the fix. The /tasty-cotton-chicken-30559418/p product has 5 items on stock as of the make of this PR and it should be impossible to add more than the quantity available.

before after

How to test it?

Test using the product: /tasty-cotton-chicken-30559418/p

base.store Deploy Preview

vtex-sites/base.store#366

References

@tlgimenes tlgimenes requested a review from a team as a code owner February 24, 2022 17:32
@netlify
Copy link

netlify bot commented Feb 24, 2022

✔️ Deploy Preview for faststoreui ready!

🔨 Explore the source changes: adfd461

🔍 Inspect the deploy log: https://app.netlify.com/sites/faststoreui/deploys/6217f4fc128e13000700f03f

😎 Browse the preview: https://deploy-preview-1160--faststoreui.netlify.app

@netlify
Copy link

netlify bot commented Feb 24, 2022

✔️ Deploy Preview for faststore-docs ready!

🔨 Explore the source changes: adfd461

🔍 Inspect the deploy log: https://app.netlify.com/sites/faststore-docs/deploys/6217f4fc9dec6b000896780d

😎 Browse the preview: https://deploy-preview-1160--faststore-docs.netlify.app

@tlgimenes tlgimenes changed the title fix: cart item availability fix: Cart item availability Feb 24, 2022
@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 24, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit adfd461:

Sandbox Source
Store UI Typescript Configuration

@@ -7,6 +7,7 @@ export const fetchAPI = async (info: RequestInfo, init?: RequestInit) => {
return response.json()
}

console.error(info, init, response)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you intend to publish this console error in production!?

Copy link
Contributor

@igorbrasileiro igorbrasileiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job, take care with the console.error

Copy link
Member

@eduardoformiga eduardoformiga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing explanation. Thank you! 🎉

LGTM! just left a little suggestion.

packages/api/src/platforms/vtex/resolvers/validateCart.ts Outdated Show resolved Hide resolved
@tlgimenes
Copy link
Contributor Author

Great job, take care with the console.error

Yeah, I'm thinking on leaving it to have better logs when running the function

Co-authored-by: Eduardo Formiga <eduardo.formiga@gmail.com>
@tlgimenes tlgimenes merged commit fd1be18 into master Feb 25, 2022
@tlgimenes tlgimenes deleted the fix/cart-availability branch February 25, 2022 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants