UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

21 lines 2.23 kB
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);}import CryptoJs from'crypto-js';import{logger}from'@shopgate/pwa-core';import{createModal}from"../../action-creators/modal";import{mutable}from"../../helpers/redux";import{getModalById}from"../../selectors/modal";import promiseMap from"./promiseMap";/** * Creates a runtime unique modal id. * @param {Object} options The modal options. * @returns {number} */export var getModalId=function getModalId(options){return CryptoJs.MD5(JSON.stringify(options)).toString();};/** * The modal defaults. * @type {Object} */var defaultModalOptions={id:null,type:null,// In this case the template should display a fallback dialog type. title:'',confirm:'modal.confirm',dismiss:'modal.dismiss',params:{}// Any parameters for special modal types go here. };/** * Dispatches the createModal action creator and returns * a promise that will be resolved when the modal closes. * @param {Object} options The modal options. * @return {Function} A Redux thunk. */function showModal(options){return function(dispatch,getState){var id=getModalId(options);// Check if there is already a modal with the same id on the modal stack. var modal=getModalById(getState(),id);if(modal){/** * To prevent bugging the user with duplicate modal, those are not added to the modal stack. * Usually the promise which is returned by showModal resolves with a boolean value * when the user interacts with its buttons. Since promise handling is optional for the * action, the promise can't be rejected, but resolves with null. */logger.warn('Modal creation aborted since an identical one was already added to the modal stack',options);return Promise.resolve(null);}var enrichedOptions=_extends({},defaultModalOptions,{},options,{id:id});dispatch(createModal(enrichedOptions));return new Promise(function(resolve,reject){promiseMap.set(id,{resolve:resolve,reject:reject});});};}/** @mixes {MutableFunction} */export default mutable(showModal);