UNPKG

@zohodesk/platform-middleware

Version:

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

111 lines (101 loc) 4.07 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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 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; } }; var WidgetsReducer = exports.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' // } // } // })