@shopgate/engage
Version:
Shopgate's ENGAGE library.
10 lines • 4.17 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}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{useEffect}from'react';import{isProductAvailable}from"../../helpers";import{MULTI_LINE_RESERVE,QUICK_RESERVE,DIRECT_SHIP,ROPIS,BOPIS,STAGE_RESERVE_FORM}from"../../constants";import{FulfillmentSheet}from"../FulfillmentSheet";import{FulfillmentPathSelector}from"../FulfillmentPathSelector";import{useFulfillmentSelectorState}from"./FulfillmentSelector.hooks";/**
* Opens the fulfillment path selector and returns a promise that resolves after selection.
* @returns {Promise}
*/function promisifiedFulfillmentPathSelector(){return new Promise(function(resolve){FulfillmentPathSelector.open(function(selectedPath){resolve(selectedPath);});});}/**
* Interject add to cart flow.
* @returns {JSX}
*/export function FulfillmentSelectorAddToCart(){var _useFulfillmentSelect=useFulfillmentSelectorState(),preferredLocation=_useFulfillmentSelect.preferredLocation,inventory=_useFulfillmentSelect.inventory,selectedLocation=_useFulfillmentSelect.selectedLocation,conditioner=_useFulfillmentSelect.conditioner,fulfillmentPaths=_useFulfillmentSelect.fulfillmentPaths,selection=_useFulfillmentSelect.selection,isDirectShipEnabled=_useFulfillmentSelect.isDirectShipEnabled,isROPISEnabled=_useFulfillmentSelect.isROPISEnabled,isBOPISEnabled=_useFulfillmentSelect.isBOPISEnabled;var usedLocation=selectedLocation||preferredLocation;// Add to cart effect to validate inventory
useEffect(function(){// Add most late conditioner
conditioner.addConditioner('fulfillment-inventory',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(){var mapping,selectedPath;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:if(!(isDirectShipEnabled&&selection===DIRECT_SHIP)){_context.next=2;break;}return _context.abrupt("return",true);case 2:mapping=_defineProperty(_defineProperty(_defineProperty({},DIRECT_SHIP,isDirectShipEnabled),ROPIS,isROPISEnabled),BOPIS,isBOPISEnabled);if(!(!selection||!mapping[selection]||!usedLocation)){_context.next=5;break;}return _context.abrupt("return",false);case 5:if(isProductAvailable(usedLocation,inventory)){_context.next=7;break;}return _context.abrupt("return",false);case 7:if(!(fulfillmentPaths.length>1)){_context.next=19;break;}_context.next=10;return promisifiedFulfillmentPathSelector();case 10:selectedPath=_context.sent;if(!(selectedPath==='')){_context.next=13;break;}return _context.abrupt("return",false);case 13:if(!(selectedPath===MULTI_LINE_RESERVE)){_context.next=15;break;}return _context.abrupt("return",true);case 15:if(!(selectedPath===QUICK_RESERVE)){_context.next=18;break;}FulfillmentSheet.open({stage:STAGE_RESERVE_FORM,fulfillmentPath:selectedPath});return _context.abrupt("return",false);case 18:return _context.abrupt("return",false);case 19:if(fulfillmentPaths.includes(MULTI_LINE_RESERVE)){_context.next=22;break;}// Open reservation form. Stop adding to a cart
FulfillmentSheet.open({stage:STAGE_RESERVE_FORM});return _context.abrupt("return",false);case 22:return _context.abrupt("return",isProductAvailable(usedLocation,inventory));case 23:case"end":return _context.stop();}},_callee);})),100);return function(){return conditioner.removeConditioner('fulfillment-inventory');};},[conditioner,fulfillmentPaths,selection,usedLocation,isDirectShipEnabled,isROPISEnabled,isBOPISEnabled,inventory]);return null;}