vtex / faststore

Digital commerce toolkit for frontend developers
https://faststore.dev
MIT License
189 stars 60 forks source link

feat: new component SKU Matrix #2501

Closed tiago-freire closed 2 days ago

tiago-freire commented 1 week ago

What's the purpose of this pull request?

Implements the SKU Matrix feature and the respective controls on Product Details section of Headless CMS in order to facilitate the selection of product variations simultaneously (such as color and size) through an intuitive interface.

How it works?

Initially, we developed 3 components: SKUMatrix, SKUMatrixTrigger and SKUMatrixSidebar.

As mentioned in the description of the created components, a context was created to manage states of SKU Matrix feature. There is a hook called useSKUMatrix, which is responsible for consuming the SKUMatrixProvider and returning all the properties so that the components in the chain can use it. A validation was implemented so that the components in the chain can only be used through the SKUMatrix wrapper.

Continuing the implementation of the SKU Matrix resource, within @faststore/core in the ui folder, an abstraction of SKUMatrixSidebar was created. All the logic for capturing and formatting the data is done in this abstraction.

To request the data, a new query was created. To use it, simply call the useAllVariantProducts hook. To consume this hook, it is necessary to pass some properties, of which we can highlight callBack and enabled.

Within this SKUMatrixSidebar abstraction, the SKUMatrix context must be consumed. As mentioned previously, the context will control all states, such as informing the context of the list of product variations. Since this abstraction will be responsible for making the request and informing the context of the data, the callBack that is passed in useAllVariantProducts will be the method to add the items that will be displayed in the SKUMatrixSidebar table. Therefore, the setAllVariantProducts method present in the SKUMatrix context must be assigned. This way, the formatted data that will be returned will be informed to the context and later, the SKUMatrixSidebar will be able to consume this data and display it on the screen.

Speaking a little about useBuyButton, this hook is responsible for adding the selected items to the cart. Previously, it did not accept a list of items, so it was necessary to modify it to meet the new functionality. Since the hook must consume some information to then inform the buyButtonProps (responsible for preparing the data to be inserted in the cart), it needs to observe the data of the products to be added to the cart. This data is returned from the SKUMatrix context, so whenever there is any type of modification in the context state (for example, a change in the quantity of a SKU within the SKUMatrixSidebar table), it will be reflected in the useBuyButton hook, and the properties will be generated correctly.

Therefore, we performed:

How to test it?

Check the "Should display SKUMatrix?" checkbox field on Product Details section of Product Details Page at Headless CMS and publish changes. Then access a product details page at store. A "Select multiple" button must be appear below "Add to Cart" button, responsible to trigger the SKU Matrix Sidebar.

References

RFC:

B2B Faststore - SKU Matrix.pdf

Printscreens

Headless CMS

SKU Matrix - Headless CMS

SKU Matrix trigger button:

SKU Matrix - Trigger Button

SKU Matrix side bar:

SKU Matrix - SKU Matrix Sidebar

vercel[bot] commented 1 week ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
faststore-site ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 11, 2024 6:04pm
codesandbox-ci[bot] commented 1 week ago

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.

tiago-freire commented 3 days ago

Splitted in #2503, #2504 and #2505