@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
64 lines • 6.78 kB
JavaScript
function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import{createSelector}from'reselect';import{generateResultHash}from'@shopgate/pwa-common/helpers/redux';import{isUserLoggedIn}from'@shopgate/pwa-common/selectors/user';import{REVIEW_PREVIEW_COUNT}from"../constants";import*as pipelines from"../constants/Pipelines";import{getBaseProductId}from"../../product/selectors/product";/**
* @param {Object} state The global state.
* @return {Object}
*/var getReviewsState=function getReviewsState(state){return state.reviews;};/**
* @param {Object} state The global state.
* @return {Object}
*/var getProductReviewsExcerptState=function getProductReviewsExcerptState(state){return state.reviews.reviewsByProductId;};/**
* Select the product reviews state.
* @param {Object} state The current application state.
* @return {Object} The product reviews state.
*/var getReviewsByHash=createSelector(getReviewsState,function(state){return state.reviewsByHash;});/**
* Retrieves the fetching state for the current product's reviews.
* @param {Object} state The current application state.
* @return {Object|null} The reviews for a product.
*/var getCollectionForCurrentBaseProduct=createSelector(getBaseProductId,getReviewsByHash,function(productId,reviews){var hash=generateResultHash({pipeline:pipelines.SHOPGATE_CATALOG_GET_PRODUCT_REVIEWS,productId:productId},false);if(reviews.hasOwnProperty(hash)){return reviews[hash];}return null;});/**
* Select the product reviews state
* @param {Object} state The current application state.
* @return {Object} The product reviews state.
*/var getReviewsByProductId=createSelector(getReviewsState,function(state){return state.reviewsByProductId;});/**
* Retrieves the reviews collection which contains all reviews data.
* @param {Object} state The current application state.
* @return {Object} The reviews collection stored as reviewId => review pairs.
*/export var getReviews=createSelector(getReviewsState,function(state){return state.reviewsById||{};});/**
* Retrieves the number of reviews for a product
* @param {Object} state The current application state.
* @return {number} The total review count for a product
*/export var getProductReviewCount=createSelector(getBaseProductId,getReviewsByProductId,function(productId,reviewsState){var collection=reviewsState[productId];if(!collection||!collection.totalReviewCount){return null;}return collection.totalReviewCount;});/**
* Retrieves the total number of reviews for a current product.
* @param {Object} state The current application state.
* @return {number|null} The total number of reviews.
*/export var getReviewsTotalCount=createSelector(getCollectionForCurrentBaseProduct,function(collection){if(!collection||!collection.totalReviewCount){return null;}return collection.totalReviewCount;});/**
* Retrieves the total number of currently fetched reviews for a current product.
* @param {Object} state The current application state.
* @return {number|null} The current number of fetched reviews.
*/export var getCurrentReviewCount=createSelector(getCollectionForCurrentBaseProduct,function(collection){if(!collection||!collection.reviews){return null;}return collection.reviews.length;});/**
* Retrieves the information if reviews are currently fetched.
* @param {Object} state The current application state.
* @return {bool} The boolean information if reviews are currently being fetched.
*/export var getReviewsFetchingState=createSelector(getCollectionForCurrentBaseProduct,function(collection){return collection&&collection.isFetching;});/**
* Select the user reviews state.
* @param {Object} state The current application state.
* @return {Object} The user reviews collection stored as productId => review.
*/var getUserReviewsByProductId=createSelector(getReviewsState,function(state){return state.userReviewsByProductId;});/**
* Retrieves a user review for a product.
*/export var getUserReviewForProduct=createSelector(getUserReviewsByProductId,getReviews,function(state){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.productId;},function(userReviews,allReviews,productId){if(!userReviews||!userReviews[productId]||!allReviews[userReviews[productId].review]){return{};}return _extends({},allReviews[userReviews[productId].review]);});/**
* Gets user reviews fetching state. Only the first fetch is considered.
* @return {bool} True if user review for current product is being fetched.
*/export var getUserReviewFirstFetchState=createSelector(getBaseProductId,getUserReviewsByProductId,function(productId,userReviews){return!!(userReviews&&productId&&userReviews[productId]&&!userReviews[productId].review&&userReviews[productId].isFetching);});/**
* Get a user name for the review form.
* @param {Object} state The state.
* @returns {string} A user name.
*/export var getDefaultAuthorName=function getDefaultAuthorName(state){return isUserLoggedIn&&state.user.data&&state.user.data.firstName?"".concat(state.user.data.firstName," ").concat(state.user.data.lastName):'';};/**
* Retrieves the current product reviews.
* When the user review is available, it will always be the first entry.
* @param {Object} state The current application state.
* @return {Array|null} The reviews for a product.
*/export var getProductReviews=createSelector(getCollectionForCurrentBaseProduct,getReviews,getUserReviewForProduct,function(collection,allReviews,userReview){if(!collection||!collection.reviews){return[];}var reviews=collection.reviews.map(function(id){return allReviews[id];});// There is no user review. Returning only from reviews collection.
if(!userReview.id){return reviews;}// User review always on top. Avoid duplicates.
return[userReview].concat(reviews.filter(function(r){return r.id!==userReview.id;}));});/**
* Retrieves the current product reviews excerpt.
* When user review is available, it will always be the first entry.
* @param {Object} state The current application state.
* @return {Array|null} The reviews for a product
*/export var getProductReviewsExcerpt=createSelector(getBaseProductId,getProductReviewsExcerptState,getReviews,getUserReviewForProduct,function(productId,productReviewsState,reviewsState,userReview){var collection=productReviewsState[productId];if(!collection||!collection.reviews){return null;}var reviews=collection.reviews.map(function(id){return reviewsState[id];});if(!userReview.id){return reviews;}return[userReview].concat(reviews.filter(function(r){return r.id!==userReview.id;})).slice(0,REVIEW_PREVIEW_COUNT);});