reducer.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import { DataSourceApi, DataSourceRef } from '@grafana/data';
  3. import { VariablePayload } from '../state/types';
  4. import { VariableQueryEditorType } from '../types';
  5. export interface AdHocVariableEditorState {
  6. infoText?: string;
  7. dataSources: Array<{ text: string; value: DataSourceRef | null }>;
  8. }
  9. export interface DataSourceVariableEditorState {
  10. dataSourceTypes: Array<{ text: string; value: string }>;
  11. }
  12. export interface QueryVariableEditorState {
  13. VariableQueryEditor: VariableQueryEditorType;
  14. dataSource: DataSourceApi | null;
  15. }
  16. type VariableEditorExtension = AdHocVariableEditorState | DataSourceVariableEditorState | QueryVariableEditorState;
  17. export interface VariableEditorState {
  18. id: string;
  19. name: string;
  20. errors: Record<string, string>;
  21. isValid: boolean;
  22. extended: VariableEditorExtension | null;
  23. }
  24. export const initialVariableEditorState: VariableEditorState = {
  25. id: '',
  26. isValid: true,
  27. errors: {},
  28. name: '',
  29. extended: null,
  30. };
  31. const variableEditorReducerSlice = createSlice({
  32. name: 'templating/editor',
  33. initialState: initialVariableEditorState,
  34. reducers: {
  35. setIdInEditor: (state: VariableEditorState, action: PayloadAction<{ id: string }>) => {
  36. state.id = action.payload.id;
  37. },
  38. clearIdInEditor: (state: VariableEditorState, action: PayloadAction<undefined>) => {
  39. state.id = '';
  40. },
  41. variableEditorMounted: (state: VariableEditorState, action: PayloadAction<{ name: string }>) => {
  42. state.name = action.payload.name;
  43. },
  44. variableEditorUnMounted: (state: VariableEditorState, action: PayloadAction<VariablePayload>) => {
  45. return initialVariableEditorState;
  46. },
  47. changeVariableNameSucceeded: (
  48. state: VariableEditorState,
  49. action: PayloadAction<VariablePayload<{ newName: string }>>
  50. ) => {
  51. state.name = action.payload.data.newName;
  52. delete state.errors['name'];
  53. state.isValid = Object.keys(state.errors).length === 0;
  54. },
  55. changeVariableNameFailed: (
  56. state: VariableEditorState,
  57. action: PayloadAction<{ newName: string; errorText: string }>
  58. ) => {
  59. state.name = action.payload.newName;
  60. state.errors.name = action.payload.errorText;
  61. state.isValid = Object.keys(state.errors).length === 0;
  62. },
  63. addVariableEditorError: (
  64. state: VariableEditorState,
  65. action: PayloadAction<{ errorProp: string; errorText: any }>
  66. ) => {
  67. state.errors[action.payload.errorProp] = action.payload.errorText;
  68. state.isValid = Object.keys(state.errors).length === 0;
  69. },
  70. removeVariableEditorError: (state: VariableEditorState, action: PayloadAction<{ errorProp: string }>) => {
  71. delete state.errors[action.payload.errorProp];
  72. state.isValid = Object.keys(state.errors).length === 0;
  73. },
  74. changeVariableEditorExtended: (state: VariableEditorState, action: PayloadAction<VariableEditorExtension>) => {
  75. state.extended = {
  76. ...state.extended,
  77. ...action.payload,
  78. };
  79. },
  80. cleanEditorState: () => initialVariableEditorState,
  81. },
  82. });
  83. export const variableEditorReducer = variableEditorReducerSlice.reducer;
  84. export const {
  85. setIdInEditor,
  86. clearIdInEditor,
  87. changeVariableNameSucceeded,
  88. changeVariableNameFailed,
  89. variableEditorMounted,
  90. variableEditorUnMounted,
  91. changeVariableEditorExtended,
  92. addVariableEditorError,
  93. removeVariableEditorError,
  94. cleanEditorState,
  95. } = variableEditorReducerSlice.actions;