ExpComponentDataDispatcher
We have learned about creating and using both default and custom components. Both types of components utilize a method called ExpComponentDataDispatcher. TheExpComponentDataDispatcher is an integrated feature that facilitates the management and proper display of API data. It relies on the foundational use of useReducer in React. To understand this functionality, it is important for the user to have a basic knowledge of useReducer
Here, we have three props:
idmodelInternalNamemodelKeyForSSR
useReducer in ExpComponentDataDispatcher, dataOfComponentDispatcher stores data, while componentDataDispatcher is used to set data.
The dispatcher includes three cases along with a default case.
- initializingFreeForm: If the user is creating a component with free form support, in this scenario, the componentData will be cleared, the isLoading flag will be set to false, and the component will return to its initial state.
-
fetchingData: If the user is creating a Default Component or a Content-Library-based component, the execution of the
dispatcherwill occur before the API call. In this scenario, theisLoadingproperty is set to true, and thecomponentDatais cleared or emptied. This ensures that the component reverts to its initial state with a loader. -
dataFetched: This case is also applicable to the Default Component. The purpose of this scenario is to store the fetched API data in the
componentDatavariable and then pass it to the Default Component. It will likely be invoked after the second case i.efetchingData. Additionally, theisLoadingproperty is set to false as we don’t require a loader after obtaining the data. - default: This case is specifically designed to handle errors that may occur if the user has added any unidentified values.
getContentLibraryData
The function below utilizes theContentService from the experro-storefront to send an API request that retrieves data from the admin panel. In order to achieve this, we must provide the following values as parameters:
1. parsedContentModel: It contains the ID of the content-model and the current_version_id associated with that specific content-model.
2. modelInternalName: It contains the model internal name of that component. For example, the modelInternalName for the Title-Section is ‘title_section’. All the modelInternalNames are passed through a file named src/utils/constants-model-internal-name.ts.
3. modelKeyForSSR: The modelKeyForSSR is used to ensure that each components are identical. String is assigend as value in modelKeyForSSR
4. id: Every component has a unique ID (CSS selector) assigned to ensure their identicity.
Note: Both ‘modelKeyForSSR’ and ‘id’ are used to ensure that each components are identical.
linkParser
In theutils/ directory you can find a file link-parser.tsx.
The ExpLinkParser is a component which can be used instead of <a/> tag or the react’s <link/> tag.
<a/> tag or react’s <link/> tag accordingly.
Props
| prop | type | description |
|---|---|---|
| to | String | URL to be re-directed when user clicks on linkParser |
| target | String | Give _blank as target prop if you want to open the url in new tab, by default the target will be _self |
| className | String | To define custom class using className prop |
| dangerouslySetInnerHTML | html | With dangerouslySetInnerHTML you can insert html inside the linkParser tag |
| title | String | Title prop to provide a title to the linkParser tag |
| onClick | Function | You can also use onClick method with the this prop |
| style | linkParserStyle() | You can give style by calling linkParserStyle() function from src/utils/link-parser-style.tsx component. |
modelInternalName
Create a constant object that contains the content model name passed in the API, ensuring that it returns the corresponding data from the content library. Whenever a user creates a new model in the admin panel for a new widget, make sure to append the model name insrc/utils/constants-model-internal-name.ts.
For example, if we have created a new model in the admin panel named ‘Blog Details’, add a new key-value pair as blog_details: "blog_details" to the modelInternalName object in src/utils/constants-model-internal-name.ts
getColorDefaultValueObject
This functionality is utilized to pass the default color value to the color-picker widget.The componentgetColorDefaultValueObject can be found in src/utils/color-default-object.ts file. It requires two parameters.
- value: This parameter is used to specify the default color value to be set in the color-picker.
- defaultValue: This parameter is used to reset the modified color value back to its default in the color-picker.
Note: This function is only utilized in the widget file when the user integrates a color picker within that widget.
expColorObjectParser
TheexpColorObjectParser is a useful component in src/utils/color-object-parser.ts when user needs a color picker in their trait. It is utilized in the component file located at src/components/cms-library/component-folder (e.g., title-section) in the component.tsx (e.g. title-section.tsx) file. Within this file, user receive a prop that contains the color base data, and the expColorObjectParser function returns the specific color name that the user has selected from the color picker.
Let me provide an example to illustrate its usage:
expColorObjectParser and it will return the color that the user can utilize as demonstrated above.
convertCurrency
This component converts the amount to the specified currency. It can be imported fromsrc/utils/currency-converter and can take two arguments:
- The price of the product.
- The currency rate, which can be obtained through an API.