UNPKG

@shopgate/engage

Version:
9 lines 2.84 kB
function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}import React,{useRef,useState,useCallback,useEffect}from'react';import PropTypes from'prop-types';import{UIEvents}from'@shopgate/pwa-core';import Dialog from"./FulfillmentSlotDialog";import connect from"./FulfillmentSlotProvider.connector";var EVENT_FORCE_OPEN='FULFILLMENT_SLOT_FORCE_OPEN';/** * Force opens the fulfillment slot dialog and returns a promise. * @returns {Promise} */export var forceOpenFulfillmentSlotDialog=function forceOpenFulfillmentSlotDialog(){return new Promise(function(resolve){UIEvents.emit(EVENT_FORCE_OPEN,{resolve:resolve});});};/** * @param {Object} props Props. * @returns {JSX} */var FulfillmentSlotProvider=function FulfillmentSlotProvider(_ref){var setFulfillmentSlot=_ref.setFulfillmentSlot,isFulfillmentScheduling=_ref.isFulfillmentScheduling;var promiseRef=useRef(null);// Handle sheet. var _useState=useState(false),_useState2=_slicedToArray(_useState,2),isOpen=_useState2[0],setOpen=_useState2[1];var _useState3=useState(true),_useState4=_slicedToArray(_useState3,2),allowClose=_useState4[0],setAllowClose=_useState4[1];var handleClose=useCallback(function(){setOpen(false);setAllowClose(true);},[]);useEffect(function(){/** */var callback=function callback(_ref2){var resolve=_ref2.resolve;promiseRef.current={resolve:resolve};setAllowClose(true);setOpen(true);};UIEvents.addListener(EVENT_FORCE_OPEN,callback);return function(){return UIEvents.removeListener(EVENT_FORCE_OPEN,callback);};});var handleUpdate=useCallback(function(slot){var _promiseRef$current;setFulfillmentSlot(slot);if((_promiseRef$current=promiseRef.current)===null||_promiseRef$current===void 0?void 0:_promiseRef$current.resolve){setTimeout(function(){var _promiseRef$current2;// eslint-disable-next-line no-unused-expressions (_promiseRef$current2=promiseRef.current)===null||_promiseRef$current2===void 0?void 0:_promiseRef$current2.resolve(slot);promiseRef.current=null;},10);}handleClose();},[handleClose,setFulfillmentSlot]);if(!isFulfillmentScheduling){return null;}return React.createElement(Dialog,{allowClose:allowClose,isOpen:isOpen,onClose:handleClose,onChange:handleUpdate});};FulfillmentSlotProvider.defaultProps={isFulfillmentScheduling:false};export default connect(FulfillmentSlotProvider);