@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
8 lines • 3.71 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";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 asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}import{getFulfillmentSchedulingEnabled}from'@shopgate/engage/core/selectors';import{getPreferredLocation}from'@shopgate/engage/locations';import{forceOpenFulfillmentSlotDialog}from'@shopgate/engage/locations/components/FulfillmentSlotSwitcher/FulfillmentSlotProvider';import{ROPIS,BOPIS}from'@shopgate/engage/locations/constants';import{getActiveFulfillmentSlot,getActiveFulfillmentSlotLocationCode}from'@shopgate/engage/cart/cart.selectors';/**
*
* @param {Object} state The current state
* @param {Array} products An array of products
* @returns {Array}
*/export var handleFulfillmentSlots=/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(state,products){var _getPreferredLocation;var needsScheduling,fulfillmentScheduling,activeFulfillmentSlot,activeSlotLocationCode,preferredLocationCode,result;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:needsScheduling=products.some(function(product){var _product$fulfillment;return[ROPIS,BOPIS].includes(product===null||product===void 0?void 0:(_product$fulfillment=product.fulfillment)===null||_product$fulfillment===void 0?void 0:_product$fulfillment.method);});fulfillmentScheduling=getFulfillmentSchedulingEnabled(state);if(!(!needsScheduling||!fulfillmentScheduling)){_context.next=4;break;}return _context.abrupt("return",products);case 4:activeFulfillmentSlot=getActiveFulfillmentSlot(state);activeSlotLocationCode=getActiveFulfillmentSlotLocationCode(state);preferredLocationCode=(_getPreferredLocation=getPreferredLocation(state))===null||_getPreferredLocation===void 0?void 0:_getPreferredLocation.code;// Make sure that a fulfillment slot has been chosen first!
if(!(!activeFulfillmentSlot||activeSlotLocationCode!==preferredLocationCode)){_context.next=11;break;}_context.next=10;return forceOpenFulfillmentSlotDialog('test');case 10:activeFulfillmentSlot=_context.sent;case 11:// Enrich slot id to fulfillment settings of all line items.
result=products.map(function(product){var _product$fulfillment2,_product$fulfillment3;var isRope=[ROPIS,BOPIS].includes(product===null||product===void 0?void 0:(_product$fulfillment2=product.fulfillment)===null||_product$fulfillment2===void 0?void 0:_product$fulfillment2.method);return _extends({},product,{fulfillment:product.fulfillment?_extends({},product.fulfillment,{slotId:isRope?activeFulfillmentSlot.id:undefined,location:isRope?(_product$fulfillment3=product.fulfillment)===null||_product$fulfillment3===void 0?void 0:_product$fulfillment3.location:undefined}):undefined});});return _context.abrupt("return",result);case 13:case"end":return _context.stop();}},_callee);}));return function handleFulfillmentSlots(_x,_x2){return _ref.apply(this,arguments);};}();