123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import { createSlice, PayloadAction } from '@reduxjs/toolkit';
- import { DataSourceApi, DataSourceRef } from '@grafana/data';
- import { VariablePayload } from '../state/types';
- import { VariableQueryEditorType } from '../types';
- export interface AdHocVariableEditorState {
- infoText?: string;
- dataSources: Array<{ text: string; value: DataSourceRef | null }>;
- }
- export interface DataSourceVariableEditorState {
- dataSourceTypes: Array<{ text: string; value: string }>;
- }
- export interface QueryVariableEditorState {
- VariableQueryEditor: VariableQueryEditorType;
- dataSource: DataSourceApi | null;
- }
- type VariableEditorExtension = AdHocVariableEditorState | DataSourceVariableEditorState | QueryVariableEditorState;
- export interface VariableEditorState {
- id: string;
- name: string;
- errors: Record<string, string>;
- isValid: boolean;
- extended: VariableEditorExtension | null;
- }
- export const initialVariableEditorState: VariableEditorState = {
- id: '',
- isValid: true,
- errors: {},
- name: '',
- extended: null,
- };
- const variableEditorReducerSlice = createSlice({
- name: 'templating/editor',
- initialState: initialVariableEditorState,
- reducers: {
- setIdInEditor: (state: VariableEditorState, action: PayloadAction<{ id: string }>) => {
- state.id = action.payload.id;
- },
- clearIdInEditor: (state: VariableEditorState, action: PayloadAction<undefined>) => {
- state.id = '';
- },
- variableEditorMounted: (state: VariableEditorState, action: PayloadAction<{ name: string }>) => {
- state.name = action.payload.name;
- },
- variableEditorUnMounted: (state: VariableEditorState, action: PayloadAction<VariablePayload>) => {
- return initialVariableEditorState;
- },
- changeVariableNameSucceeded: (
- state: VariableEditorState,
- action: PayloadAction<VariablePayload<{ newName: string }>>
- ) => {
- state.name = action.payload.data.newName;
- delete state.errors['name'];
- state.isValid = Object.keys(state.errors).length === 0;
- },
- changeVariableNameFailed: (
- state: VariableEditorState,
- action: PayloadAction<{ newName: string; errorText: string }>
- ) => {
- state.name = action.payload.newName;
- state.errors.name = action.payload.errorText;
- state.isValid = Object.keys(state.errors).length === 0;
- },
- addVariableEditorError: (
- state: VariableEditorState,
- action: PayloadAction<{ errorProp: string; errorText: any }>
- ) => {
- state.errors[action.payload.errorProp] = action.payload.errorText;
- state.isValid = Object.keys(state.errors).length === 0;
- },
- removeVariableEditorError: (state: VariableEditorState, action: PayloadAction<{ errorProp: string }>) => {
- delete state.errors[action.payload.errorProp];
- state.isValid = Object.keys(state.errors).length === 0;
- },
- changeVariableEditorExtended: (state: VariableEditorState, action: PayloadAction<VariableEditorExtension>) => {
- state.extended = {
- ...state.extended,
- ...action.payload,
- };
- },
- cleanEditorState: () => initialVariableEditorState,
- },
- });
- export const variableEditorReducer = variableEditorReducerSlice.reducer;
- export const {
- setIdInEditor,
- clearIdInEditor,
- changeVariableNameSucceeded,
- changeVariableNameFailed,
- variableEditorMounted,
- variableEditorUnMounted,
- changeVariableEditorExtended,
- addVariableEditorError,
- removeVariableEditorError,
- cleanEditorState,
- } = variableEditorReducerSlice.actions;
|