@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
8 lines • 1.86 kB
JavaScript
import PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import{SORT_DATE_DESC}from'@shopgate/pwa-common/constants/DisplayOptions';import{generateResultHash,mutable}from'@shopgate/pwa-common/helpers/redux';import{REVIEW_PREVIEW_COUNT}from"../constants";import{SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS}from"../constants/Pipelines";import requestProductReviewsList from"../action-creators/requestReviews";import receiveProductReviewsList from"../action-creators/receiveReviews";import errorProductReviewsList from"../action-creators/errorReviews";/**
* Request product reviews for a product by the given id.
* @param {string} productId The product ID.
* @param {number} [limit=REVIEW_PREVIEW_COUNT] The maximum number of reviews to fetch.
* @param {number} [offset=0] The list offset (defaults to 0).
* @param {('relevance'|'dateDesc'|'dateAsc'|'rateDesc'|'rateAsc')} sort Sorting.
* @returns {Function} The dispatched action.
*/function fetchReviews(productId){var limit=arguments.length>1&&arguments[1]!==undefined?arguments[1]:REVIEW_PREVIEW_COUNT;var offset=arguments.length>2&&arguments[2]!==undefined?arguments[2]:0;var sort=arguments.length>3&&arguments[3]!==undefined?arguments[3]:SORT_DATE_DESC;return function(dispatch){var hash=generateResultHash({pipeline:SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS,productId:productId},false);dispatch(requestProductReviewsList(hash));var request=new PipelineRequest(SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS).setInput({productId:productId,limit:limit,offset:offset,sort:sort}).dispatch();request.then(function(_ref){var reviews=_ref.reviews,totalReviewCount=_ref.totalReviewCount;dispatch(receiveProductReviewsList(hash,productId,reviews,totalReviewCount));})["catch"](function(){dispatch(errorProductReviewsList(hash));});return request;};}/** @mixes {MutableFunction} */export default mutable(fetchReviews);