@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
5 lines • 1.33 kB
JavaScript
import appConfig from'@shopgate/pwa-common/helpers/config';import PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import{shouldFetchData,mutable}from'@shopgate/pwa-common/helpers/redux';import{SHOPGATE_CATALOG_GET_PRODUCT_MEDIA}from"../constants/Pipelines";import requestProductMedia from"../action-creators/requestProductMedia";import receiveProductMedia from"../action-creators/receiveProductMedia";import errorProductMedia from"../action-creators/errorProductMedia";/**
* Retrieves product options from store.
* @param {string} productId The product ID for which the product options are requested.
* @return {Function} A Redux Thunk
*/function fetchProductMedia(productId){return function(dispatch,getState){if(!appConfig.beta){return Promise.resolve(null);}var state=getState();var cachedData=state.product.mediaByProductId[productId];if(!shouldFetchData(cachedData)){return Promise.resolve(null);}dispatch(requestProductMedia(productId));var request=new PipelineRequest(SHOPGATE_CATALOG_GET_PRODUCT_MEDIA).setInput({productId:productId}).dispatch();request.then(function(result){dispatch(receiveProductMedia(productId,result.media));})["catch"](function(error){dispatch(errorProductMedia(productId,error.code));});return request;};}/** @mixes {MutableFunction} */export default mutable(fetchProductMedia);