reducers.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import { PanelPlugin } from '@grafana/data';
  3. import { AngularComponent } from '@grafana/runtime';
  4. import { processAclItems } from 'app/core/utils/acl';
  5. import { DashboardAclDTO, DashboardInitError, DashboardInitPhase, DashboardState } from 'app/types';
  6. import { DashboardModel } from './DashboardModel';
  7. import { PanelModel } from './PanelModel';
  8. export const initialState: DashboardState = {
  9. initPhase: DashboardInitPhase.NotStarted,
  10. getModel: () => null,
  11. permissions: [],
  12. initError: null,
  13. };
  14. const dashboardSlice = createSlice({
  15. name: 'dashboard',
  16. initialState,
  17. reducers: {
  18. loadDashboardPermissions: (state, action: PayloadAction<DashboardAclDTO[]>) => {
  19. state.permissions = processAclItems(action.payload);
  20. },
  21. dashboardInitFetching: (state) => {
  22. state.initPhase = DashboardInitPhase.Fetching;
  23. },
  24. dashboardInitServices: (state) => {
  25. state.initPhase = DashboardInitPhase.Services;
  26. },
  27. dashboardInitCompleted: (state, action: PayloadAction<DashboardModel>) => {
  28. state.getModel = () => action.payload;
  29. state.initPhase = DashboardInitPhase.Completed;
  30. },
  31. dashboardInitFailed: (state, action: PayloadAction<DashboardInitError>) => {
  32. state.initPhase = DashboardInitPhase.Failed;
  33. state.initError = action.payload;
  34. state.getModel = () => {
  35. return new DashboardModel({ title: 'Dashboard init failed' }, { canSave: false, canEdit: false });
  36. };
  37. },
  38. cleanUpDashboard: (state) => {
  39. state.initPhase = DashboardInitPhase.NotStarted;
  40. state.initError = null;
  41. state.getModel = () => null;
  42. },
  43. addPanel: (state, action: PayloadAction<PanelModel>) => {
  44. //state.panels[action.payload.id] = { pluginId: action.payload.type };
  45. },
  46. },
  47. });
  48. export interface PanelModelAndPluginReadyPayload {
  49. panelId: number;
  50. plugin: PanelPlugin;
  51. }
  52. export interface SetPanelAngularComponentPayload {
  53. panelId: number;
  54. angularComponent: AngularComponent | null;
  55. }
  56. export interface SetPanelInstanceStatePayload {
  57. panelId: number;
  58. value: any;
  59. }
  60. export const {
  61. loadDashboardPermissions,
  62. dashboardInitFetching,
  63. dashboardInitFailed,
  64. dashboardInitCompleted,
  65. dashboardInitServices,
  66. cleanUpDashboard,
  67. addPanel,
  68. } = dashboardSlice.actions;
  69. export const dashboardReducer = dashboardSlice.reducer;
  70. export default {
  71. dashboard: dashboardReducer,
  72. };