mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* Refactor: Adds Redux Toolkit package * Refactor: Uses configureStore from Redux Toolkit * Refactor: Migrates applicationReducer * Refactor: Migrates appNotificationsReducer * Refactor: Migrates locationReducer * Refactor: Migrates navModelReducer * Refactor: Migrates teamsReducer and teamReducer * Refactor: Migrates cleanUpAction * Refactor: Migrates alertRulesReducer * Refactor: Cleans up recursiveCleanState * Refactor: Switched to Angular compatible reducers * Refactor: Migrates folderReducer * Refactor: Migrates dashboardReducer * Migrates panelEditorReducer * Refactor: Migrates dataSourcesReducer * Refactor: Migrates usersReducer * Refactor: Migrates organizationReducer * Refactor: Migrates pluginsReducer * Refactor: Migrates ldapReducer and ldapUserReducer * Refactor: Migrates apiKeysReducer * Refactor: Migrates exploreReducer and itemReducer * Refactor: Removes actionCreatorFactory and reducerFactory * Refactor: Moves mocks to test section * Docs: Removes sections about home grown framework * Update contribute/style-guides/redux.md Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Refactor: Cleans up some code * Refactor: Adds state typings * Refactor: Cleans up typings * Refactor: Adds comment about ImmerJs autoFreeze Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
97 lines
2.5 KiB
TypeScript
97 lines
2.5 KiB
TypeScript
import { Action } from 'redux';
|
|
import { DashboardInitPhase, DashboardState } from 'app/types';
|
|
import {
|
|
cleanUpDashboard,
|
|
dashboardInitCompleted,
|
|
dashboardInitFailed,
|
|
dashboardInitFetching,
|
|
dashboardInitServices,
|
|
dashboardInitSlow,
|
|
loadDashboardPermissions,
|
|
} from './actions';
|
|
import { processAclItems } from 'app/core/utils/acl';
|
|
import { panelEditorReducer } from '../panel_editor/state/reducers';
|
|
import { DashboardModel } from './DashboardModel';
|
|
|
|
export const initialState: DashboardState = {
|
|
initPhase: DashboardInitPhase.NotStarted,
|
|
isInitSlow: false,
|
|
model: null,
|
|
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 {
|
|
...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 {
|
|
...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;
|
|
};
|
|
|
|
export default {
|
|
dashboard: dashboardReducer,
|
|
panelEditor: panelEditorReducer,
|
|
};
|