@zohodesk/platform-middleware
Version:
Generalized Contract for the events from Marketplace App, In-App widgets, CRM-Plus and EFC
70 lines (59 loc) • 2.29 kB
JavaScript
import _defineProperty from "babel-runtime/helpers/defineProperty";
import _Object$assign from "babel-runtime/core-js/object/assign";
import _Set from "babel-runtime/core-js/set";
import _toConsumableArray from "babel-runtime/helpers/toConsumableArray";
import _Object$keys from "babel-runtime/core-js/object/keys";
import { constants } from "../constants";
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);
}
export var 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.ADD_WIDGETS_DATA:
var _data = constructWidgetIdsByLocation(_data);
console.log(_data);
return state;
default:
return state;
}
};