grafana/public/app/features/dashboard/state/reducers.ts

97 lines
2.5 KiB
TypeScript
Raw Normal View History

import { Action } from 'redux';
import { DashboardInitPhase, DashboardState } from 'app/types';
import {
cleanUpDashboard,
dashboardInitCompleted,
dashboardInitFailed,
dashboardInitFetching,
dashboardInitServices,
dashboardInitSlow,
loadDashboardPermissions,
} from './actions';
2018-09-13 09:00:02 -05:00
import { processAclItems } from 'app/core/utils/acl';
import { panelEditorReducer } from '../panel_editor/state/reducers';
import { DashboardModel } from './DashboardModel';
2018-09-13 09:00:02 -05:00
2018-10-25 09:56:49 -05:00
export const initialState: DashboardState = {
initPhase: DashboardInitPhase.NotStarted,
isInitSlow: false,
model: null,
2018-09-13 09:00:02 -05:00
permissions: [],
};
// Redux Toolkit uses ImmerJs as part of their solution to ensure that state objects are not mutated.
// ImmerJs has an autoFreeze option that freezes objects from change which means this reducer can't be migrated to createSlice
// because the state would become frozen and during run time we would get errors because Angular would try to mutate
// the frozen state.
// https://github.com/reduxjs/redux-toolkit/issues/242
export const dashboardReducer = (state: DashboardState = initialState, action: Action<unknown>): DashboardState => {
if (loadDashboardPermissions.match(action)) {
return {
2019-02-02 15:43:19 -06:00
...state,
permissions: processAclItems(action.payload),
};
}
if (dashboardInitFetching.match(action)) {
return {
...state,
initPhase: DashboardInitPhase.Fetching,
};
}
if (dashboardInitServices.match(action)) {
return {
...state,
initPhase: DashboardInitPhase.Services,
};
}
if (dashboardInitSlow.match(action)) {
return {
2019-02-02 15:43:19 -06:00
...state,
isInitSlow: true,
};
}
if (dashboardInitFailed.match(action)) {
return {
...state,
initPhase: DashboardInitPhase.Failed,
isInitSlow: false,
initError: action.payload,
model: new DashboardModel({ title: 'Dashboard init failed' }, { canSave: false, canEdit: false }),
};
}
if (dashboardInitCompleted.match(action)) {
return {
...state,
initPhase: DashboardInitPhase.Completed,
model: action.payload,
isInitSlow: false,
};
}
if (cleanUpDashboard.match(action)) {
// Destroy current DashboardModel
// Very important as this removes all dashboard event listeners
state.model.destroy();
return {
...state,
initPhase: DashboardInitPhase.NotStarted,
model: null,
isInitSlow: false,
initError: null,
};
}
return state;
};
2018-09-13 09:00:02 -05:00
export default {
dashboard: dashboardReducer,
panelEditor: panelEditorReducer,
2018-09-13 09:00:02 -05:00
};