cluedin-widget
Version: 
This is the project for creating and managing widgets in CluedIn.
78 lines (70 loc) • 2.78 kB
JavaScript
const constants = require( '../constants' );
const initialState = {
    layout: {},
    token: '',
    org: '',
    connectedData: []
};
module.exports = function update( state = initialState, action = {} ) {
    switch( action.type ) {
        case constants.core.RESET_LAYOUT:
            return {
                layout: {},
                token: state.token,
                org: state.org,
                connectedData: state.connectedData
            };
        case constants.generic.UNAUTHORIZED_REQUEST:
            return Object.assign( {}, {
                layout: state.layout,
                token: void 0,
                org: state.org,
                connectedData: state.connectedData
            } );
        case constants.core.RECEIVE_LAYOUT:
            var newLayout = Object.assign( {}, state.layout );
            newLayout[ action.data.index ] = Object.assign( {}, action.data.layout );
            return {
                layout: newLayout,
                token: state.token,
                org: state.org,
                connectedData: state.connectedData
            };
        case constants.core.RECEIVE_MOST_CONNECTED:
            return Object.assign( {}, {
                connectedData: action.data,
                layout: state.layout,
                token: state.token,
                org: state.org
            } );
        case constants.core.REMOVE_WIDGET:
            let newLayout         = state.layout;
            let name              = action.data.name;
            let layoutInformation = action.data;
            if ( state.layout[ layoutInformation.layoutIndex ] ) {
                if ( layoutInformation.tabIndex >= 0 ) {
                    state.layout[ layoutInformation.layoutIndex ].tabs[ layoutInformation.tabIndex ].widgets.filter( ( w ) => {
                        return w.name !== name;
                    } );
                } else {
                    if ( state.layout[ layoutInformation.layoutIndex ].sharedWidgets ) {
                        state.layout[ layoutInformation.layoutIndex ].sharedWidgets.filter( ( w ) => {
                            return w.name !== name;
                        } );
                    } else {
                        newLayout[ layoutInformation.layoutIndex ].widgets = state.layout[ layoutInformation.layoutIndex ].widgets.filter( ( w ) => {
                            return w.name !== name;
                        } );
                    }
                }
            }
            return Object.assign( {}, {
                connectedData: action.data,
                layout: Object.assign( {}, newLayout ),
                token: state.token,
                org: state.org
            } );
        default :
            return state;
    }
};