UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

5 lines 1.25 kB
import PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import{shouldFetchData,mutable}from'@shopgate/pwa-common/helpers/redux';import requestProductOptions from"../action-creators/requestProductOptions";import{SHOPGATE_CATALOG_GET_PRODUCT_OPTIONS}from"../constants/Pipelines";import receiveProductOptions from"../action-creators/receiveProductOptions";import errorProductOptions from"../action-creators/errorProductOptions";/** * Retrieves product options from store. * @param {string} productId The product ID for which the product options are requested. * @return {Function} A Redux Thunk */function fetchProductOptions(productId){return function(dispatch,getState){var state=getState();var cachedData=state.product.optionsByProductId[productId];if(!shouldFetchData(cachedData)){return Promise.resolve(null);}dispatch(requestProductOptions(productId));var request=new PipelineRequest(SHOPGATE_CATALOG_GET_PRODUCT_OPTIONS).setInput({productId:productId}).dispatch();request.then(function(result){dispatch(receiveProductOptions(productId,result.options));})["catch"](function(error){dispatch(errorProductOptions(productId,error.code));});return request;};}/** @mixes {MutableFunction} */export default mutable(fetchProductOptions);