reducers.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { createSlice, Draft, PayloadAction } from '@reduxjs/toolkit';
  2. import { DataSourceInstanceSettings, LoadingState } from '@grafana/data';
  3. import { LibraryElementDTO } from '../../library-panels/types';
  4. export enum DashboardSource {
  5. Gcom = 0,
  6. Json = 1,
  7. }
  8. export interface ImportDashboardDTO {
  9. title: string;
  10. uid: string;
  11. gnetId: string;
  12. constants: string[];
  13. dataSources: DataSourceInstanceSettings[];
  14. elements: LibraryElementDTO[];
  15. folder: { id: number; title?: string };
  16. }
  17. export enum InputType {
  18. DataSource = 'datasource',
  19. Constant = 'constant',
  20. LibraryPanel = 'libraryPanel',
  21. }
  22. export enum LibraryPanelInputState {
  23. New = 'new',
  24. Exists = 'exists',
  25. Different = 'different',
  26. }
  27. export interface DashboardInput {
  28. name: string;
  29. label: string;
  30. info: string;
  31. value: string;
  32. type: InputType;
  33. }
  34. export interface DataSourceInput extends DashboardInput {
  35. pluginId: string;
  36. }
  37. export interface LibraryPanelInput {
  38. model: LibraryElementDTO;
  39. state: LibraryPanelInputState;
  40. }
  41. export interface DashboardInputs {
  42. dataSources: DataSourceInput[];
  43. constants: DashboardInput[];
  44. libraryPanels: LibraryPanelInput[];
  45. }
  46. export interface ImportDashboardState {
  47. meta: { updatedAt: string; orgName: string };
  48. dashboard: any;
  49. source: DashboardSource;
  50. inputs: DashboardInputs;
  51. state: LoadingState;
  52. }
  53. export const initialImportDashboardState: ImportDashboardState = {
  54. meta: { updatedAt: '', orgName: '' },
  55. dashboard: {},
  56. source: DashboardSource.Json,
  57. inputs: {} as DashboardInputs,
  58. state: LoadingState.NotStarted,
  59. };
  60. const importDashboardSlice = createSlice({
  61. name: 'manageDashboards',
  62. initialState: initialImportDashboardState,
  63. reducers: {
  64. setGcomDashboard: (state: Draft<ImportDashboardState>, action: PayloadAction<any>) => {
  65. state.dashboard = {
  66. ...action.payload.json,
  67. id: null,
  68. };
  69. state.meta = { updatedAt: action.payload.updatedAt, orgName: action.payload.orgName };
  70. state.source = DashboardSource.Gcom;
  71. state.state = LoadingState.Done;
  72. },
  73. setJsonDashboard: (state: Draft<ImportDashboardState>, action: PayloadAction<any>) => {
  74. state.dashboard = {
  75. ...action.payload,
  76. id: null,
  77. };
  78. state.meta = initialImportDashboardState.meta;
  79. state.source = DashboardSource.Json;
  80. state.state = LoadingState.Done;
  81. },
  82. clearDashboard: (state: Draft<ImportDashboardState>) => {
  83. state.dashboard = {};
  84. state.state = LoadingState.NotStarted;
  85. },
  86. setInputs: (state: Draft<ImportDashboardState>, action: PayloadAction<any[]>) => {
  87. state.inputs = {
  88. dataSources: action.payload.filter((p) => p.type === InputType.DataSource),
  89. constants: action.payload.filter((p) => p.type === InputType.Constant),
  90. libraryPanels: [],
  91. };
  92. },
  93. setLibraryPanelInputs: (state: Draft<ImportDashboardState>, action: PayloadAction<LibraryPanelInput[]>) => {
  94. state.inputs.libraryPanels = action.payload;
  95. },
  96. fetchFailed: (state: Draft<ImportDashboardState>) => {
  97. state.dashboard = {};
  98. state.state = LoadingState.Error;
  99. },
  100. fetchDashboard: (state: Draft<ImportDashboardState>) => {
  101. state.state = LoadingState.Loading;
  102. },
  103. },
  104. });
  105. export const {
  106. clearDashboard,
  107. setInputs,
  108. setGcomDashboard,
  109. setJsonDashboard,
  110. setLibraryPanelInputs,
  111. fetchFailed,
  112. fetchDashboard,
  113. } = importDashboardSlice.actions;
  114. export const importDashboardReducer = importDashboardSlice.reducer;
  115. export default {
  116. importDashboard: importDashboardReducer,
  117. };