@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
7 lines • 1.76 kB
JavaScript
import PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import{shouldFetchData,mutable}from'@shopgate/pwa-common/helpers/redux';import{SORT_RELEVANCE}from'@shopgate/pwa-common/constants/DisplayOptions';import{SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS}from"../constants/Pipelines";import{REVIEW_PREVIEW_COUNT}from"../constants";import requestProductReviews from"../action-creators/requestProductReviews";import receiveProductReviews from"../action-creators/receiveProductReviews";import errorProductReviews from"../action-creators/errorProductReviews";/**
* Request product reviews for a product from server.
* @param {string} productId The product ID
* @param {number} [limit=REVIEW_PREVIEW_COUNT] The maximum number of reviews to fetch
* @param {('relevance'|'dateDesc'|'dateAsc'|'rateDesc'|'rateAsc')} [sort=SORT_RELEVANCE] Sorting.
* @returns {Promise} The dispatched action.
*/function fetchProductReviews(productId){var limit=arguments.length>1&&arguments[1]!==undefined?arguments[1]:REVIEW_PREVIEW_COUNT;var sort=arguments.length>2&&arguments[2]!==undefined?arguments[2]:SORT_RELEVANCE;return function(dispatch,getState){var data=getState().reviews.reviewsByProductId[productId];if(!shouldFetchData(data)){return Promise.resolve(null);}dispatch(requestProductReviews(productId,limit));var request=new PipelineRequest(SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS).setInput({productId:productId,limit:limit,sort:sort}).dispatch();request.then(function(_ref){var reviews=_ref.reviews,totalReviewCount=_ref.totalReviewCount;dispatch(receiveProductReviews(productId,reviews,totalReviewCount));})["catch"](function(){dispatch(errorProductReviews(productId));});return request;};}/** @mixes {MutableFunction} */export default mutable(fetchProductReviews);