@shopgate/engage
Version:
Shopgate's ENGAGE library.
24 lines • 5.58 kB
JavaScript
var _excluded=["productInventory"],_excluded2=["locationCode"],_excluded3=["locationCode","productCode"];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);}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;}import{produce}from'immer';import{generateSortedHash}from'@shopgate/pwa-common/helpers/redux/generateSortedHash';import{ERROR_LOCATIONS,REQUEST_LOCATIONS,RECEIVE_LOCATIONS,REQUEST_PRODUCT_LOCATIONS,RECEIVE_PRODUCT_LOCATIONS,RECEIVE_INVENTORIES,ERROR_PRODUCT_LOCATIONS,STORE_FULFILLMENT_METHOD,SELECT_LOCATION,FETCH_FULFILLMENT_SLOTS_SUCCESS,RECEIVE_PRODUCT_INVENTORIES}from"../constants";var initialState={// If general usage of state has been finished.
// Currently used to identify if any change is currently pending
// before the store switcher is being displayed.
pending:true,// If any data is being fetched right now.
isFetching:false,// The users preferred fulfillment location.
preferredLocation:{code:null},// The users preferred fulfillment method.
preferredFulfillmentMethod:{type:null},// All location information stored by code.
locationsByCode:{},// Inventory for a specific location and product.
inventoriesByCodePair:{},// List of locations that based on a given filter.
locationsByFilter:{},// Available FO slots mapped by location.
fulfillmentSlotsByLocation:{}};/* eslint-disable no-param-reassign */ /**
* Stores a list of locations and updates the store.
* @param {Object} draft Draft
* @param {Array} locations Locations
*/var storeLocationData=function storeLocationData(draft,locations){locations.forEach(function(location){// Remove some situational data to avoid confusion.
// The location storage should only contain the raw location info.
var productInventory=location.productInventory,filteredData=_objectWithoutProperties(location,_excluded);draft.locationsByCode[location.code]=filteredData;// Enhance with primary address for easier lookup.
draft.locationsByCode[location.code].address=location.addresses.find(function(a){return a.isPrimary;})||location.addresses[0];});};export default(function(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:initialState;var action=arguments.length>1?arguments[1]:undefined;var producer=produce(function(draft){var _action$location;switch(action.type){case REQUEST_LOCATIONS:case REQUEST_PRODUCT_LOCATIONS:draft.isFetching=true;break;case ERROR_LOCATIONS:case ERROR_PRODUCT_LOCATIONS:draft.isFetching=false;break;case FETCH_FULFILLMENT_SLOTS_SUCCESS:{draft.fulfillmentSlotsByLocation=draft.fulfillmentSlotsByLocation||{};draft.fulfillmentSlotsByLocation[action.locationCode]=action.fulfillmentSlots;break;}case RECEIVE_LOCATIONS:{// Store all missing locations.
storeLocationData(draft,action.locations);// Store filtered result set.
var locationCodes=action.locations.map(function(l){return l.code;});var filter=generateSortedHash(_extends({},action.filters));draft.locationsByFilter[filter]=locationCodes;draft.initialized=true;draft.isFetching=false;draft.pending=false;break;}case RECEIVE_PRODUCT_INVENTORIES:{action.inventories.forEach(function(inventory){var locationCode=inventory.locationCode,rest=_objectWithoutProperties(inventory,_excluded2);var key=generateSortedHash({productCode:action.productCode,locationCode:locationCode});draft.inventoriesByCodePair[key]=rest;});break;}case RECEIVE_INVENTORIES:{action.productInventories.forEach(function(inventory){var locationCode=inventory.locationCode,productCode=inventory.productCode,rest=_objectWithoutProperties(inventory,_excluded3);var key=generateSortedHash({productCode:productCode,locationCode:locationCode});draft.inventoriesByCodePair[key]=rest;});break;}case RECEIVE_PRODUCT_LOCATIONS:{// Store all missing locations.
storeLocationData(draft,action.locations);// For each location we store a new inventory entry.
action.locations.forEach(function(location){var key=generateSortedHash({productCode:action.productCode,locationCode:location.code});// Keep bin/binLocation for location if present
draft.inventoriesByCodePair[key]=_extends({},draft.inventoriesByCodePair[key],{},location.productInventory);});// Store filtered result set.
var _filter=generateSortedHash(_extends({productCode:action.productCode},action.filters));var _locationCodes=action.locations.map(function(l){return l.code;});draft.locationsByFilter[_filter]=_locationCodes;draft.pending=false;draft.isFetching=false;break;}case STORE_FULFILLMENT_METHOD:draft.preferredFulfillmentMethod.type=action.method;break;case SELECT_LOCATION:draft.preferredLocation.code=((_action$location=action.location)===null||_action$location===void 0?void 0:_action$location.code)||null;break;default:break;}});return producer(state);});/* eslint-enable no-param-reassign */