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

feat(store-api): Add cart to store-api #963

Merged
merged 3 commits into from
Oct 4, 2021
Merged

feat(store-api): Add cart to store-api #963

merged 3 commits into from
Oct 4, 2021

Conversation

tlgimenes
Copy link
Contributor

@tlgimenes tlgimenes commented Sep 20, 2021

What's the purpose of this pull request?

This PR adds an optimistic cart implementation to store-api.

How it works?

The optimistic cart we have works via state validation. This means that, we build a state of the cart on the browser, and then, the backend validates this state. If this state is valid, the backend signalizes this state is ok and nothing changes on the frontend. If, however, this state is not valid, a.k.a, this product is unavailable, a promotion was applied, etc, the backend sends the new cart state to the frontend. An example of this process can be seen below:

image

To implement the cart, I used the schema.org's type Order. Also, another thing this PR brings is input fields. We didn't really use them before, but now I needed a standard for writing them. I added an I prefix to them, so writing an input for a type can be achieved by:
type StoreProduct -> type IStoreProduct.

Also, another thing this PR brings is the usage of dataloaders. This was necessary for loading skus and simulations without overwhelming the search/checkout API.

How to test it?

To test it, open our PR on base.store and check that all promotions are applied correctly. Also, if you want you can change the base account name to carrefourbrfood and check all edge cases in there.

vtex-sites/base.store#36

@netlify
Copy link

netlify bot commented Sep 20, 2021

✔️ Deploy Preview for storeui ready!

🔨 Explore the source changes: 7511598

🔍 Inspect the deploy log: https://app.netlify.com/sites/storeui/deploys/61577e8adc28e500077b995e

😎 Browse the preview: https://deploy-preview-963--storeui.netlify.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 20, 2021

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 7511598:

Sandbox Source
Store UI Typescript Configuration

@tlgimenes tlgimenes marked this pull request as ready for review September 20, 2021 21:18
@tlgimenes tlgimenes requested a review from a team as a code owner September 20, 2021 21:18
@tlgimenes tlgimenes linked an issue Sep 21, 2021 that may be closed by this pull request
Comment on lines +16 to +21
listPrice: item.listPrice / 100,
price: item.sellingPrice / 100,
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer to transfer this data over the net with a natural value, not a float.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why so? doing this will make us do this computation on the frontend

@tlgimenes tlgimenes added this to the Store API milestone Sep 22, 2021
packages/store-api/src/platforms/vtex/loaders/sku.ts Outdated Show resolved Hide resolved
/**
* This resolver implements the optimistic cart behavior. The main idea in here
* is that we receive a cart from the UI (as query params) and we validate it with
* the commerce platform. If the cart is valid, we return null, if the cart is
Copy link
Member

Choose a reason for hiding this comment

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

Not so sure about returning null to signify that "no changes were made", or "the cart is valid".

Why not add a field to the returned orderForm like updated, which would be a boolean value? Then we always return back the orderForm, but with an easy way for the frontend to check if the cart should be updated or not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to save data. These carts can be big and it would be nice to save a little bit of bandwidth when the cart is valid

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think?

Copy link
Member

@victorhmp victorhmp Oct 4, 2021

Choose a reason for hiding this comment

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

Yeah, makes sense!

I don't know how users will react to this hahahaha they might not find it weird. But regardless, from a performance standpoint, I agree. This is a great way to save data when dealing with big carts 😄 Especially taking into account poor mobile connections.

packages/store-api/src/typeDefs/index.ts Outdated Show resolved Hide resolved
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.

Good job overall. This PR is a little bit huge, so I might not see problems by mistake.
I left some comments

@tlgimenes tlgimenes requested a review from victorhmp October 4, 2021 14:04
@tlgimenes tlgimenes merged commit 0607d82 into master Oct 4, 2021
@tlgimenes tlgimenes deleted the feat/api-cart branch October 4, 2021 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cart
4 participants