@zohodesk/platform-middleware
Version:
Generalized Contract for the events from Marketplace App, In-App widgets, CRM-Plus and EFC
106 lines (100 loc) • 3.89 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';
var constructWidgetsState = function constructWidgetsState(widgets, uID, state, location) {
var uniqueKey = '';
if (location) {
uniqueKey = uID ? location + '_' + uID : location;
var widgetIds = _Object$keys(widgets);
var prevWidgetIds = state[uniqueKey] || [];
var newWidgets = [].concat(_toConsumableArray(new _Set([].concat(_toConsumableArray(prevWidgetIds), _toConsumableArray(widgetIds)))));
return state = _Object$assign({}, state, _defineProperty({}, uniqueKey, newWidgets));
} else {
_Object$keys(widgets).map(function (widgetId) {
uniqueKey = uID ? widgets[widgetId].locationName + '_' + uID : widgets[widgetId].locationName;
if (state[uniqueKey]) {
state[uniqueKey].push(widgetId);
} else {
state[uniqueKey] = [];
state[uniqueKey].push(widgetId);
}
});
return state;
}
};
export var WidgetsReducer = function WidgetsReducer() {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { widgets: {}, runtimeWidgets: {} };
var action = arguments[1];
switch (action.type) {
case 'FETCH_MY_WIDGETS_SUCCESS':
action.data = action.data.data;
state = _Object$assign({}, state, { widgets: _Object$assign({}, state.widgets, action.data.widgets) });
return constructWidgetsState(action.data.widgets, action.data.uID, _Object$assign({}, state), action.data.location);
case 'FETCH_MY_NEXT_WIDGETS_SUCCESS':
action.data = action.data.data;
_Object$assign({}, state, _defineProperty({}, action.data.location, _Object$assign({}, state[action.data.location[action.data.uID]], _defineProperty({}, action.data.uID, action.data[action.data.uID]))));
return state;
default:
return state;
}
};
// const normalizeWidgetResponse = (widgets, uID) => {
// let uniqueKey = {};
// return Object.keys(widgets).reduce((obj, widgetId) => {
// uniqueKey = `${widgets[widgetId].locationName}_${uID}`;
// if (Array.isArray(obj[uniqueKey])) {
// obj[uniqueKey].push(widgetId)
// } else {
// obj[uniqueKey] = [];
// obj[uniqueKey].push(widgetId)
// }
// return obj;
// }, {})
// }
// const normalizeResponse = (widgets, uID) => {
// let uniqueKey = null;
// return widgets.reduce((obj, widget) => {
// uniqueKey = `${widget.locationName}_${uID}`;
// if (Array.isArray(obj[uniqueKey])) {
// obj[uniqueKey].push(widget)
// } else {
// obj[uniqueKey] = [];
// obj[uniqueKey].push(widget)
// }
// if (obj[widget.locationName] && obj[widget.locationName].constructor === {}.constructor) {
// if (obj[widget.locationName][uID] && obj[widget.locationName][uID].constructor === {}.constructor) {
// obj[widget.locationName][uID][widget.id] = widget;
// } else {
// obj[widget.locationName][uID] = {}
// obj[widget.locationName][uID][widget.id] = widget;
// }
// } else {
// obj[widget.locationName] = {};
// obj[widget.locationName][uID] = {}
// obj[widget.locationName][uID][widget.id] = widget;
// }
// return obj;
// }, {})
// }
// const normalizeWidgets = (widgets) => {
// let widgetID = null;
// return widgets.reduce((obj, widget) => {
// widgetID = widget.id;
// obj[widgetID] = widget;
// return obj;
// }, {})
// }
// $r.state.store.dispatch({
// type: 'FETCH_MY_WIDGETS_SUCCESS',
// data: {
// data: {
// widgets: {
// 'qwerty': { id: 'qwerty', locationName: 'desk.ticket.detail.rightpanel' }
// },
// location: 'desk.ticket.detail.lefttab',
// uID: '12334'
// }
// }
// })