UNPKG

@zohodesk/platform-middleware

Version:

Generalized Contract for the events from Marketplace App, In-App widgets, CRM-Plus and EFC

76 lines (61 loc) 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.marketplaceReducer = undefined; var _constants = require("../constants"); 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 _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } var initialState = { widgetIds: {}, widgets: {}, runTimeWidgets: {} }; /** * Widget Contract * { * id : '', * location : '' * } */ function constructWidgetIdsByLocation(data, state) { var isInitialRequest = data.isInitialRequest, widgets = data.widgets, uniqueKey = data.uniqueKey, location = data.location; var widgetIds = Object.keys(widgets) || []; uniqueKey = uniqueKey && location ? location + "_" + uniqueKey : location; if (location) { var newWidgetIds = []; if (isInitialRequest) { newWidgetIds = [].concat(_toConsumableArray(widgetIds)); } else { var prevWidgetIds = state[location] || []; newWidgetIds = [].concat(_toConsumableArray(new Set([].concat(_toConsumableArray(prevWidgetIds), _toConsumableArray(widgetIds))))); } return Object.assign({}, state, _defineProperty({}, uniqueKey, newWidgetIds)); } return widgetIds.reduce(function (response, widgetId) { var widgetInfo = widgets[widgetId]; if (widgetInfo) { uniqueKey = uniqueKey ? widgetInfo.location + "_" + uniqueKey : widgetInfo.location; var _prevWidgetIds = state[widgetInfo.location] || []; response[uniqueKey] = [].concat(_toConsumableArray(new Set([].concat(_toConsumableArray(_prevWidgetIds), [widgetId])))); } return response; }, state); } var marketplaceReducer = exports.marketplaceReducer = function marketplaceReducer() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; var action = arguments[1]; var type = action.type, data = action.data; switch (type) { case _constants.constants.ADD_WIDGETS_DATA: var _data = constructWidgetIdsByLocation(_data); console.log(_data); return state; default: return state; } };