2020-02-09 02:45:50 -06:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
2019-02-06 12:42:04 -06:00
|
|
|
import {
|
2020-02-09 02:45:50 -06:00
|
|
|
DashboardInitPhase,
|
|
|
|
DashboardState,
|
|
|
|
DashboardAclDTO,
|
|
|
|
DashboardInitError,
|
|
|
|
QueriesToUpdateOnDashboardLoad,
|
|
|
|
} from 'app/types';
|
2018-09-13 09:00:02 -05:00
|
|
|
import { processAclItems } from 'app/core/utils/acl';
|
2019-09-23 07:17:00 -05:00
|
|
|
import { panelEditorReducer } from '../panel_editor/state/reducers';
|
2020-01-13 01:03:22 -06:00
|
|
|
import { DashboardModel } from './DashboardModel';
|
2020-02-10 07:23:54 -06:00
|
|
|
import { PanelModel } from './PanelModel';
|
2018-09-13 09:00:02 -05:00
|
|
|
|
2018-10-25 09:56:49 -05:00
|
|
|
export const initialState: DashboardState = {
|
2019-02-06 12:42:04 -06:00
|
|
|
initPhase: DashboardInitPhase.NotStarted,
|
|
|
|
isInitSlow: false,
|
2020-02-09 02:45:50 -06:00
|
|
|
getModel: () => null,
|
2018-09-13 09:00:02 -05:00
|
|
|
permissions: [],
|
2020-02-09 02:45:50 -06:00
|
|
|
modifiedQueries: null,
|
2020-02-10 07:23:54 -06:00
|
|
|
panels: {},
|
|
|
|
initError: null,
|
2018-09-13 09:00:02 -05:00
|
|
|
};
|
|
|
|
|
2020-02-09 02:45:50 -06:00
|
|
|
const dashbardSlice = createSlice({
|
|
|
|
name: 'dashboard',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
loadDashboardPermissions: (state, action: PayloadAction<DashboardAclDTO[]>) => {
|
|
|
|
state.permissions = processAclItems(action.payload);
|
|
|
|
},
|
2020-02-09 03:53:34 -06:00
|
|
|
dashboardInitFetching: (state, action: PayloadAction) => {
|
2020-02-09 02:45:50 -06:00
|
|
|
state.initPhase = DashboardInitPhase.Fetching;
|
|
|
|
},
|
2020-02-09 03:53:34 -06:00
|
|
|
dashboardInitServices: (state, action: PayloadAction) => {
|
2020-02-09 02:45:50 -06:00
|
|
|
state.initPhase = DashboardInitPhase.Services;
|
|
|
|
},
|
2020-02-09 03:53:34 -06:00
|
|
|
dashboardInitSlow: (state, action: PayloadAction) => {
|
2020-02-09 02:45:50 -06:00
|
|
|
state.isInitSlow = true;
|
|
|
|
},
|
2020-02-10 07:23:54 -06:00
|
|
|
dashboardInitCompleted: (state, action: PayloadAction<DashboardModel>) => {
|
2020-02-09 02:45:50 -06:00
|
|
|
state.getModel = () => action.payload;
|
|
|
|
state.initPhase = DashboardInitPhase.Completed;
|
|
|
|
state.isInitSlow = false;
|
2020-02-10 07:23:54 -06:00
|
|
|
|
|
|
|
for (const panel of action.payload.panels) {
|
|
|
|
state.panels[panel.id] = {
|
|
|
|
pluginId: panel.type,
|
|
|
|
};
|
|
|
|
}
|
2020-02-09 02:45:50 -06:00
|
|
|
},
|
|
|
|
dashboardInitFailed: (state, action: PayloadAction<DashboardInitError>) => {
|
|
|
|
state.initPhase = DashboardInitPhase.Failed;
|
|
|
|
state.initError = action.payload;
|
2020-02-09 03:53:34 -06:00
|
|
|
state.getModel = () => {
|
|
|
|
return new DashboardModel({ title: 'Dashboard init failed' }, { canSave: false, canEdit: false });
|
|
|
|
};
|
2020-02-09 02:45:50 -06:00
|
|
|
},
|
2020-02-09 03:53:34 -06:00
|
|
|
cleanUpDashboard: (state, action: PayloadAction) => {
|
2020-02-09 02:45:50 -06:00
|
|
|
if (state.getModel()) {
|
2020-02-10 07:23:54 -06:00
|
|
|
state.getModel()!.destroy();
|
2020-02-09 02:45:50 -06:00
|
|
|
state.getModel = () => null;
|
|
|
|
}
|
2019-02-06 12:42:04 -06:00
|
|
|
|
2020-02-09 02:45:50 -06:00
|
|
|
state.initPhase = DashboardInitPhase.NotStarted;
|
|
|
|
state.isInitSlow = false;
|
|
|
|
state.initError = null;
|
|
|
|
},
|
|
|
|
setDashboardQueriesToUpdateOnLoad: (state, action: PayloadAction<QueriesToUpdateOnDashboardLoad>) => {
|
|
|
|
state.modifiedQueries = action.payload;
|
|
|
|
},
|
2020-02-09 03:53:34 -06:00
|
|
|
clearDashboardQueriesToUpdateOnLoad: (state, action: PayloadAction) => {
|
2020-02-09 02:45:50 -06:00
|
|
|
state.modifiedQueries = null;
|
|
|
|
},
|
2020-02-10 07:23:54 -06:00
|
|
|
dashboardPanelTypeChanged: (state, action: PayloadAction<DashboardPanelTypeChangedPayload>) => {
|
|
|
|
state.panels[action.payload.panelId] = { pluginId: action.payload.pluginId };
|
|
|
|
},
|
|
|
|
addPanelToDashboard: (state, action: PayloadAction<AddPanelPayload>) => {},
|
2020-02-09 02:45:50 -06:00
|
|
|
},
|
|
|
|
});
|
2020-01-16 05:44:05 -06:00
|
|
|
|
2020-02-10 07:23:54 -06:00
|
|
|
export interface DashboardPanelTypeChangedPayload {
|
|
|
|
panelId: number;
|
|
|
|
pluginId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AddPanelPayload {
|
|
|
|
panel: PanelModel;
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:45:50 -06:00
|
|
|
export const {
|
|
|
|
loadDashboardPermissions,
|
|
|
|
dashboardInitFetching,
|
|
|
|
dashboardInitFailed,
|
|
|
|
dashboardInitSlow,
|
|
|
|
dashboardInitCompleted,
|
|
|
|
dashboardInitServices,
|
|
|
|
cleanUpDashboard,
|
|
|
|
setDashboardQueriesToUpdateOnLoad,
|
|
|
|
clearDashboardQueriesToUpdateOnLoad,
|
2020-02-10 07:23:54 -06:00
|
|
|
dashboardPanelTypeChanged,
|
|
|
|
addPanelToDashboard,
|
2020-02-09 02:45:50 -06:00
|
|
|
} = dashbardSlice.actions;
|
2020-01-16 05:44:05 -06:00
|
|
|
|
2020-02-09 02:45:50 -06:00
|
|
|
export const dashboardReducer = dashbardSlice.reducer;
|
2018-09-13 09:00:02 -05:00
|
|
|
|
|
|
|
export default {
|
|
|
|
dashboard: dashboardReducer,
|
2019-09-23 07:17:00 -05:00
|
|
|
panelEditor: panelEditorReducer,
|
2018-09-13 09:00:02 -05:00
|
|
|
};
|