UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

31 lines • 7.8 kB
function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}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 _toPropertyKey(arg){var key=_toPrimitive(arg,"string");return _typeof(key)==="symbol"?key:String(key);}function _toPrimitive(input,hint){if(_typeof(input)!=="object"||input===null)return input;var prim=input[Symbol.toPrimitive];if(prim!==undefined){var res=prim.call(input,hint||"default");if(_typeof(res)!=="object")return res;throw new TypeError("@@toPrimitive must return a primitive value.");}return(hint==="string"?String:Number)(input);}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 _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}function _callSuper(_this,derived,args){function isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));}catch(e){return false;}}derived=_getPrototypeOf(derived);return _possibleConstructorReturn(_this,isNativeReflectConstruct()?Reflect.construct(derived,args||[],_getPrototypeOf(_this).constructor):derived.apply(_this,args));}function _possibleConstructorReturn(self,call){if(call&&(_typeof(call)==="object"||typeof call==="function")){return call;}return _assertThisInitialized(self);}function _assertThisInitialized(self){if(self===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return self;}function _getPrototypeOf(o){_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(o){return o.__proto__||Object.getPrototypeOf(o);};return _getPrototypeOf(o);}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function");}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:true,configurable:true}});if(superClass)_setPrototypeOf(subClass,superClass);}function _setPrototypeOf(o,p){_setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(o,p){o.__proto__=p;return o;};return _setPrototypeOf(o,p);}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;}import React,{Component}from'react';import PropTypes from'prop-types';import{UIEvents}from'@shopgate/pwa-core';import LoadingContext from"./context";/** * The LoadingProvider component. */var LoadingProvider=/*#__PURE__*/function(_Component){/** * @param {Object} props The component props. */function LoadingProvider(props){var _this2;_classCallCheck(this,LoadingProvider);_this2=_callSuper(this,LoadingProvider,[props]);/** * Adds or increases the loading counter for a path. * @param {string} path The path which loads. */_defineProperty(_this2,"setLoading",function(path){var _this3=_this2,loading=_this3.loading;var newLoading=_extends({},loading,_defineProperty({},path,loading[path]?loading[path]+1:1));// Immediately updates state due to multiple sets before actual rerender. _this2.loading=newLoading;_this2.setState({loading:newLoading});});/** * Resets the loading counter for a path. * @param {string} path The path which loads. */_defineProperty(_this2,"resetLoading",function(path){var _this2$loading=_this2.loading,removedPath=_this2$loading[path],remaining=_objectWithoutProperties(_this2$loading,[path].map(_toPropertyKey));// Immediately updates state due to multiple sets before actual rerender. _this2.loading=remaining;_this2.setState({loading:remaining});});/** * Decreases the loading counter for a path. * @param {string} path The path which loads. */_defineProperty(_this2,"unsetLoading",function(path){var _this4=_this2,loading=_this4.loading;if(typeof loading[path]==='undefined'){return;}if(loading[path]<=1){_this2.resetLoading(path);return;}var newLoading=_extends({},loading,_defineProperty({},path,loading[path]-1));// Immediately updates state due to multiple sets before actual rerender. _this2.loading=newLoading;_this2.setState({loading:newLoading});});/** * Checks if a path is loading. * @param {string} path The path it inspect. * @return {boolean} */_defineProperty(_this2,"isLoading",function(path){var loading=_this2.state.loading;return!!loading[path];});_this2.loading={};_this2.state={loading:{}};UIEvents.addListener(_this2.constructor.SET,_this2.setLoading);UIEvents.addListener(_this2.constructor.RESET,_this2.resetLoading);UIEvents.addListener(_this2.constructor.UNSET,_this2.unsetLoading);return _this2;}/** * Removes the event listeners when the component unmounts. */_inherits(LoadingProvider,_Component);return _createClass(LoadingProvider,[{key:"componentWillUnmount",value:function componentWillUnmount(){UIEvents.removeListener(this.constructor.SET,this.setLoading);UIEvents.removeListener(this.constructor.RESET,this.resetLoading);UIEvents.removeListener(this.constructor.UNSET,this.unsetLoading);}},{key:"render",value:/** * @return {JSX} */function render(){var value={loading:this.state.loading,setLoading:this.setLoading,unsetLoading:this.unsetLoading,isLoading:this.isLoading};return React.createElement(LoadingContext.Provider,{value:value},this.props.children);}}],[{key:"setLoading",value:function setLoading(path){UIEvents.emit(LoadingProvider.SET,path);}/** * Resets the loading counter for a path. * @param {string} path The path which loads. */},{key:"resetLoading",value:function resetLoading(path){UIEvents.emit(LoadingProvider.RESET,path);}/** * Decreases the loading counter for a path. * @param {string} path The path which loads. */},{key:"unsetLoading",value:function unsetLoading(path){UIEvents.emit(LoadingProvider.UNSET,path);}}]);}(Component);_defineProperty(LoadingProvider,"SET",'loading_set');_defineProperty(LoadingProvider,"RESET",'loading_reset');_defineProperty(LoadingProvider,"UNSET",'loading_unset');export default LoadingProvider;