UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

11 lines 3.76 kB
var _excluded=["cartItems"];function _objectWithoutProperties(source,excluded){if(source==null)return{};var target=_objectWithoutPropertiesLoose(source,excluded);var key,i;if(Object.getOwnPropertySymbols){var sourceSymbolKeys=Object.getOwnPropertySymbols(source);for(i=0;i<sourceSymbolKeys.length;i++){key=sourceSymbolKeys[i];if(excluded.indexOf(key)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(source,key))continue;target[key]=source[key];}}return target;}function _objectWithoutPropertiesLoose(source,excluded){if(source==null)return{};var target={};var sourceKeys=Object.keys(source);var key,i;for(i=0;i<sourceKeys.length;i++){key=sourceKeys[i];if(excluded.indexOf(key)>=0)continue;target[key]=source[key];}return target;}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 remove from'lodash/remove';import{ADD_PRODUCTS_TO_CART,DELETE_PRODUCTS_FROM_CART,UPDATE_PRODUCTS_IN_CART,ERROR_ADD_PRODUCTS_TO_CART,REQUEST_CART,RECEIVE_CART,ERROR_CART,SET_CART_PENDING_PRODUCT_COUNT,SET_FULFILLMENT_SLOT}from"../constants";var defaultState={items:[],totals:[],productPendingCount:0,flags:{},activeFulfillmentSlot:null};/** * Stores all the cart items. * The cart will be cached in localStorage as defined in './library/store/configureStore.js'. * @param {Object} [state] The current state. * @param {Object} action The action object. * @return {Object} The new state. */export default(function(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:defaultState;var action=arguments.length>1?arguments[1]:undefined;switch(action.type){case ADD_PRODUCTS_TO_CART:case ERROR_ADD_PRODUCTS_TO_CART:{return state;}case DELETE_PRODUCTS_FROM_CART:{return _extends({},state,{items:remove(state.items,function(item){return!action.cartItemIds.includes(item.id);})});}case UPDATE_PRODUCTS_IN_CART:{return _extends({},state,{items:state.items.map(function(item){var updateData=action.updateData;// Search the stored cart item, which shall be updated var match=updateData.find(function(update){return item.id===update.cartItemId;});// Apply the updated quantity to the cart item in the store. return _extends({},item,{},typeof match!=='undefined'&&typeof match.quantity!=='undefined'&&{quantity:match.quantity},{},typeof match!=='undefined'&&typeof match.substitutionAllowed!=='undefined'&&{substitutionAllowed:match.substitutionAllowed});})});}case REQUEST_CART:{return _extends({},state,{isFetching:true,expires:0});}case RECEIVE_CART:{var _ref,_cart$flags,_ref2,_cart$flags2;var _action$cart2=action.cart,cartItems=_action$cart2.cartItems,cart=_objectWithoutProperties(_action$cart2,_excluded);return _extends({},state,{},cart,{// Map cart flags to properties on root level to establish compatibility with getCart // pipelines that don't return "isOrderable" and "isTaxIncluded" flags isOrderable:(_ref=cart===null||cart===void 0?void 0:(_cart$flags=cart.flags)===null||_cart$flags===void 0?void 0:_cart$flags.orderable)!==null&&_ref!==void 0?_ref:true,isTaxIncluded:(_ref2=cart===null||cart===void 0?void 0:(_cart$flags2=cart.flags)===null||_cart$flags2===void 0?void 0:_cart$flags2.taxIncluded)!==null&&_ref2!==void 0?_ref2:true,expires:0,items:cartItems,isFetching:false,productPendingCount:0});}case ERROR_CART:return _extends({},state,{isFetching:false,expires:0});case SET_CART_PENDING_PRODUCT_COUNT:return _extends({},state,{productPendingCount:Math.max(0,action.count)});case SET_FULFILLMENT_SLOT:return _extends({},state,{activeFulfillmentSlot:action.fulfillmentSlot});default:return state;}});