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.
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
andSKUMatrixSidebar
.SKUMatrix
: this component serves as a wrapper (component that will wrapSKUMatrixTrigger
andSKUMatrixSidebar
). It contains theSKUMatrixProvider
. This provider will control all the internal state of SKU Matrix (open and close the slider, control the state of the SKUs: change the quantity of items to be added to the cart, return the list of product variations). For the SKU Matrix feature to work correctly, this wrapper must be present. To preventSKUMatrixSidebar
orSKUMatrixTrigger
from being used in isolation, a treatment was made in theuseSKUMatrix
hook.SKUMatrixTrigger
: This component is responsible for triggering the slider opening/closing trigger. It is an abstraction of theButton
component. Within this component, a context is consumed (SKUMatrixProvider
contained inSKUMatrix
), this hook returns thesetOpen
property, which is responsible for opening or closingSKUMatrixSidebar
.SKUMatrixSidebar
: The feature main component. It is responsible for displaying the table containing all the variations of a product and allows the selection of multiple SKUs. For the component to work correctly, the context created and exposed bySKUMatrix
must be consumed, as mentioned previously, all the logic and state management ofSKUMatrixSidebar
is contained in the context. This component is composed of:SlideOver
,Table
,QuantitySelector
,Price
,Button
,Skeleton
.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 theSKUMatrixProvider
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 theSKUMatrix
wrapper.Continuing the implementation of the SKU Matrix resource, within
@faststore/core
in the ui folder, an abstraction ofSKUMatrixSidebar
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 highlightcallBack
andenabled
.callBack
: is a function that will be executed only when the request is successful and will return all the data. This method will return the data fully formatted following the format pre-established in theSKUMatrixSidebar
component.enabled
: is aboolean
(true/false). It will inform the query whether theSKUMatrixSidebar
is open or not. The request will only be made if the slider is open, otherwise it will not be, avoiding unnecessary requests.Within this
SKUMatrixSidebar
abstraction, theSKUMatrix
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, thecallBack
that is passed inuseAllVariantProducts
will be the method to add the items that will be displayed in theSKUMatrixSidebar
table. Therefore, thesetAllVariantProducts
method present in theSKUMatrix
context must be assigned. This way, the formatted data that will be returned will be informed to the context and later, theSKUMatrixSidebar
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 thebuyButtonProps
(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 theSKUMatrix
context, so whenever there is any type of modification in the context state (for example, a change in the quantity of a SKU within theSKUMatrixSidebar
table), it will be reflected in theuseBuyButton
hook, and the properties will be generated correctly.Therefore, we performed:
Construction of 3 new components:
SKUMatrix
,SKUMatrixTrigger
,SKUMatrixSidebar
Provider to manage the entire SKU Matrix context
Hook for consumption of the context:
useSKUMatrix
Exclusive query to access only the important data for the construction of SKU Matrix:
useAllVariantProducts
Change in the
useBuyButton
hookHow 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 trigger button:
SKU Matrix side bar: