UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

15 lines 5.29 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 _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{themeConfig}from'@shopgate/pwa-common/helpers/config';import ToastContext from"./context";var _themeConfig$variable=themeConfig.variables,_themeConfig$variable2=_themeConfig$variable===void 0?{}:_themeConfig$variable,_themeConfig$variable3=_themeConfig$variable2.toast,_themeConfig$variable4=_themeConfig$variable3===void 0?{}:_themeConfig$variable3,_themeConfig$variable5=_themeConfig$variable4.duration,duration=_themeConfig$variable5===void 0?5000:_themeConfig$variable5;/** * The ToastProvider component */var ToastProvider=/*#__PURE__*/function(_Component){function ToastProvider(props){var _this2;_classCallCheck(this,ToastProvider);_this2=_callSuper(this,ToastProvider,[props]);/** * Adds a new, unique, toast to the list. * @param {Object} toast The toast object to add. */_defineProperty(_this2,"addToast",function(toast){if(!toast.message){return;}var toasts=_this2.state.toasts;// Check if the toast id already is present. var found=toasts.find(function(_ref){var id=_ref.id;return toast.id===id;});// If found, update the toast with the new data. if(found){found.action=toast.action;found.actionLabel=toast.actionLabel;found.message=toast.message;found.messageParams=toast.messageParams;found.duration=toast.duration||duration;}else{toasts.push({id:toast.id,action:toast.action,actionLabel:toast.actionLabel,message:toast.message,messageParams:toast.messageParams,duration:toast.duration||duration});}_this2.setState({toasts:toasts});});/** * Removes the first toast from the list. */_defineProperty(_this2,"removeToast",function(){var toasts=_this2.state.toasts;toasts.shift();_this2.setState({toasts:toasts});});_defineProperty(_this2,"flushToasts",function(){if(_this2.state.toasts.length){_this2.setState({toasts:[]});}});_this2.state={toasts:[]};UIEvents.addListener(_this2.constructor.ADD,_this2.addToast);UIEvents.addListener(_this2.constructor.FLUSH,_this2.flushToasts);return _this2;}/** * Returns the context value to be passed to consumers. * @returns {Object} */_inherits(ToastProvider,_Component);return _createClass(ToastProvider,[{key:"provided",get:function get(){return{addToast:this.addToast,removeToast:this.removeToast,toasts:this.state.toasts};}},{key:"render",value:/** * @returns {JSX} */function render(){return React.createElement(ToastContext.Provider,{value:this.provided},this.props.children);}}]);}(Component);_defineProperty(ToastProvider,"ADD",'toast_add');_defineProperty(ToastProvider,"FLUSH",'toast_flush');export default ToastProvider;