mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
58 lines
2.2 KiB
TypeScript
58 lines
2.2 KiB
TypeScript
|
import { AdHocVariableModel, VariableHide, AdHocVariableFilter } from 'app/features/templating/variable';
|
||
|
import { EMPTY_UUID, getInstanceState, VariablePayload } from '../state/types';
|
||
|
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
|
||
|
import { VariablesState, initialVariablesState } from '../state/variablesReducer';
|
||
|
|
||
|
export interface AdHocVariabelFilterUpdate {
|
||
|
index: number;
|
||
|
filter: AdHocVariableFilter;
|
||
|
}
|
||
|
export interface AdHocVariableEditorState {
|
||
|
infoText: string;
|
||
|
dataSources: Array<{ text: string; value: string }>;
|
||
|
}
|
||
|
|
||
|
export const initialAdHocVariableModelState: AdHocVariableModel = {
|
||
|
uuid: EMPTY_UUID,
|
||
|
global: false,
|
||
|
type: 'adhoc',
|
||
|
name: '',
|
||
|
hide: VariableHide.dontHide,
|
||
|
label: '',
|
||
|
skipUrlSync: false,
|
||
|
index: -1,
|
||
|
initLock: null,
|
||
|
datasource: null,
|
||
|
filters: [],
|
||
|
};
|
||
|
|
||
|
export const adHocVariableSlice = createSlice({
|
||
|
name: 'templating/adhoc',
|
||
|
initialState: initialVariablesState,
|
||
|
reducers: {
|
||
|
filterAdded: (state: VariablesState, action: PayloadAction<VariablePayload<AdHocVariableFilter>>) => {
|
||
|
const instanceState = getInstanceState<AdHocVariableModel>(state, action.payload.uuid);
|
||
|
instanceState.filters.push(action.payload.data);
|
||
|
},
|
||
|
filterRemoved: (state: VariablesState, action: PayloadAction<VariablePayload<number>>) => {
|
||
|
const instanceState = getInstanceState<AdHocVariableModel>(state, action.payload.uuid);
|
||
|
const index = action.payload.data;
|
||
|
|
||
|
instanceState.filters.splice(index, 1);
|
||
|
},
|
||
|
filterUpdated: (state: VariablesState, action: PayloadAction<VariablePayload<AdHocVariabelFilterUpdate>>) => {
|
||
|
const instanceState = getInstanceState<AdHocVariableModel>(state, action.payload.uuid);
|
||
|
const { filter, index } = action.payload.data;
|
||
|
|
||
|
instanceState.filters[index] = filter;
|
||
|
},
|
||
|
filtersRestored: (state: VariablesState, action: PayloadAction<VariablePayload<AdHocVariableFilter[]>>) => {
|
||
|
const instanceState = getInstanceState<AdHocVariableModel>(state, action.payload.uuid);
|
||
|
instanceState.filters = action.payload.data;
|
||
|
},
|
||
|
},
|
||
|
});
|
||
|
|
||
|
export const { filterAdded, filterRemoved, filterUpdated, filtersRestored } = adHocVariableSlice.actions;
|
||
|
export const adHocVariableReducer = adHocVariableSlice.reducer;
|