@shopgate/pwa-common
Version:
Common library for the Shopgate Connect PWA.
12 lines • 3.74 kB
JavaScript
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 _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 _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{ENOTFOUND}from'@shopgate/pwa-core/constants/Pipeline';import{REQUEST_PAGE_CONFIG,RECEIVE_PAGE_CONFIG,ERROR_PAGE_CONFIG,APP_WILL_START}from"../../constants/ActionTypes";/**
* Enrich the widget data.
* @param {Object} action The action that was received.
* @return {Object} An enriched set of widgets.
*/var enrichWidgets=function enrichWidgets(action){return action.config.widgets.map(function(widget,index){return _extends({},widget,{id:"".concat(action.pageId,"-").concat(index,"-").concat(widget.type)});});};/**
* The page config reducer.
* @param {Object} [state={}] The current application state.
* @param {Object} action The action object.
* @return {Object} The store data.
*/export default function pageReducer(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var action=arguments.length>1?arguments[1]:undefined;switch(action.type){case REQUEST_PAGE_CONFIG:return _extends({},state,_defineProperty({},action.pageId,_extends({},state[action.pageId],{isFetching:true,expires:0})));case RECEIVE_PAGE_CONFIG:{return _extends({},state,_defineProperty({},action.pageId,{title:action.config.title,widgets:enrichWidgets(action),isFetching:false,expires:Date.now()+3600000// One hour
}));}case ERROR_PAGE_CONFIG:{if(action.errorCode===ENOTFOUND){// Remove the temporary page entry from the state when noting was found for the pageId.
var _action$pageId2=action.pageId,ignore=state[_action$pageId2],rest=_objectWithoutProperties(state,[_action$pageId2].map(_toPropertyKey));return rest;}return _extends({},state,_defineProperty({},action.pageId,_extends({},state[action.pageId],{isFetching:false,expires:0})));}case APP_WILL_START:{return Object.keys(state).reduce(function(acc,pageId){acc[pageId].expires=0;return acc;},_extends({},state));}default:return state;}}