2020-03-23 13:45:08 +01:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
2021-11-03 08:02:51 +01:00
|
|
|
|
|
|
|
import { AdHocVariableFilter, AdHocVariableModel, initialVariableModelState } from 'app/features/variables/types';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2022-02-18 06:06:04 +01:00
|
|
|
import { getInstanceState } from '../state/selectors';
|
2022-04-22 14:33:13 +01:00
|
|
|
import { initialVariablesState, VariablePayload, VariablesState } from '../state/types';
|
2020-03-23 09:00:36 +01:00
|
|
|
|
|
|
|
export interface AdHocVariabelFilterUpdate {
|
|
|
|
index: number;
|
|
|
|
filter: AdHocVariableFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const initialAdHocVariableModelState: AdHocVariableModel = {
|
2020-10-02 07:02:06 +02:00
|
|
|
...initialVariableModelState,
|
2020-03-23 09:00:36 +01:00
|
|
|
type: 'adhoc',
|
|
|
|
datasource: null,
|
|
|
|
filters: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
export const adHocVariableSlice = createSlice({
|
|
|
|
name: 'templating/adhoc',
|
|
|
|
initialState: initialVariablesState,
|
|
|
|
reducers: {
|
|
|
|
filterAdded: (state: VariablesState, action: PayloadAction<VariablePayload<AdHocVariableFilter>>) => {
|
2022-08-10 16:06:49 +01:00
|
|
|
const instanceState = getInstanceState(state, action.payload.id);
|
|
|
|
if (instanceState.type !== 'adhoc') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-23 09:00:36 +01:00
|
|
|
instanceState.filters.push(action.payload.data);
|
|
|
|
},
|
|
|
|
filterRemoved: (state: VariablesState, action: PayloadAction<VariablePayload<number>>) => {
|
2022-08-10 16:06:49 +01:00
|
|
|
const instanceState = getInstanceState(state, action.payload.id);
|
|
|
|
if (instanceState.type !== 'adhoc') {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-23 09:00:36 +01:00
|
|
|
|
2022-08-10 16:06:49 +01:00
|
|
|
const index = action.payload.data;
|
2020-03-23 09:00:36 +01:00
|
|
|
instanceState.filters.splice(index, 1);
|
|
|
|
},
|
|
|
|
filterUpdated: (state: VariablesState, action: PayloadAction<VariablePayload<AdHocVariabelFilterUpdate>>) => {
|
2022-08-10 16:06:49 +01:00
|
|
|
const instanceState = getInstanceState(state, action.payload.id);
|
|
|
|
if (instanceState.type !== 'adhoc') {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-23 09:00:36 +01:00
|
|
|
|
2022-08-10 16:06:49 +01:00
|
|
|
const { filter, index } = action.payload.data;
|
2020-03-23 09:00:36 +01:00
|
|
|
instanceState.filters[index] = filter;
|
|
|
|
},
|
|
|
|
filtersRestored: (state: VariablesState, action: PayloadAction<VariablePayload<AdHocVariableFilter[]>>) => {
|
2022-08-10 16:06:49 +01:00
|
|
|
const instanceState = getInstanceState(state, action.payload.id);
|
|
|
|
if (instanceState.type !== 'adhoc') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-23 09:00:36 +01:00
|
|
|
instanceState.filters = action.payload.data;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const { filterAdded, filterRemoved, filterUpdated, filtersRestored } = adHocVariableSlice.actions;
|
|
|
|
export const adHocVariableReducer = adHocVariableSlice.reducer;
|