@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
53 lines (49 loc) • 1.95 kB
JavaScript
import PipelineRequest from '@shopgate/pwa-core/classes/PipelineRequest';
import { shouldFetchData, mutable } from '@shopgate/pwa-common/helpers/redux';
import { errorBehavior } from '@shopgate/engage/core';
import { SHOPGATE_CATALOG_GET_CATEGORY } from "../constants/Pipelines";
import fetchCategoryChildren from "./fetchCategoryChildren";
import requestCategory from "../action-creators/requestCategory";
import receiveCategory from "../action-creators/receiveCategory";
import errorCategory from "../action-creators/errorCategory";
import { getCategory } from "../selectors";
/**
* Fetches the data for a given category ID (including child categories).
* @param {string} categoryId The category ID.
* @return {Function} The dispatched action.
*/
function fetchCategory(categoryId) {
return (dispatch, getState) => {
const category = getCategory(getState(), {
categoryId
});
// Check if we need to fetch data
if (!shouldFetchData(category)) {
/**
* Child categories are maybe missing.
* So we need to check it (check happens inside fetchCategoryChildren).
* This is the case if we got categories from getRootCategory
*/
if (category.childrenCount) {
dispatch(fetchCategoryChildren(categoryId));
}
return Promise.resolve(category);
}
// No data at all. So we have to fetch the category with children included
dispatch(requestCategory(categoryId));
const request = new PipelineRequest(SHOPGATE_CATALOG_GET_CATEGORY).setInput({
categoryId,
includeChildren: true
}).setResponseBehavior({
error: errorBehavior.modal()
}).dispatch();
request.then(result => {
dispatch(receiveCategory(categoryId, result, result.children || []));
}).catch(error => {
dispatch(errorCategory(categoryId, error.code));
});
return request;
};
}
/** @mixes {MutableFunction} */
export default mutable(fetchCategory);