@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
9 lines • 2.09 kB
JavaScript
import PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import{logger}from'@shopgate/pwa-core/helpers';import{shouldFetchData,mutable}from'@shopgate/pwa-common/helpers/redux';import receiveProductRelations from"../action-creators/receiveProductRelations";import requestProductRelations from"../action-creators/requestProductRelations";import errorProductRelations from"../action-creators/errorProductRelations";import{SHOPGATE_CATALOG_GET_PRODUCT_RELATIONS}from"../constants/Pipelines";import{generateProductRelationsHash}from"../helpers";import{getProductRelationsState}from"../selectors/relations";import{PRODUCT_RELATIONS_DEFAULT_LIMIT}from"../constants";/**
* Action starts product relation fetching process.
* Returns early if product relation cache is still valid.
* @param {Object} params Params.
* @param {string} params.productId Product Id.
* @param {string} params.type Type (see constants).
* @param {number} params.limit Query limit.
* @returns {Function}
*/function fetchProductRelations(_ref){var productId=_ref.productId,type=_ref.type,_ref$limit=_ref.limit,limit=_ref$limit===void 0?PRODUCT_RELATIONS_DEFAULT_LIMIT:_ref$limit;return function(dispatch,getState){var pipeline=SHOPGATE_CATALOG_GET_PRODUCT_RELATIONS;var hash=generateProductRelationsHash({productId:productId,type:type,limit:limit});var currentState=getProductRelationsState(getState())[hash];if(!shouldFetchData(currentState,'productIds')){return Promise.resolve(null);}dispatch(requestProductRelations({hash:hash}));var request=new PipelineRequest(pipeline).setInput({productId:productId,type:type,limit:limit}).dispatch();request.then(function(payload){var relations=payload.relations;if(!Array.isArray(relations)){logger.error(new Error("Invalid ".concat(pipeline," pipeline response")),payload);dispatch(errorProductRelations({hash:hash}));return;}dispatch(receiveProductRelations({hash:hash,payload:payload}));})["catch"](function(){dispatch(errorProductRelations({hash:hash}));});return request;};}/** @mixes {MutableFunction} */export default mutable(fetchProductRelations);