diff --git a/src/state/valueSets.ts b/src/state/valueSets.ts new file mode 100644 index 000000000..4d92113a0 --- /dev/null +++ b/src/state/valueSets.ts @@ -0,0 +1,38 @@ +import { createSlice, createEntityAdapter, PayloadAction } from '@reduxjs/toolkit' +import { HierarchyElementWithSystem } from 'types' +import { logout } from './me' +import { LabelObject } from 'types/searchCriterias' + +type ValueSetOptions = { + id: string + options: HierarchyElementWithSystem[] +} + +const valueSetsAdapter = createEntityAdapter() + +const valueSetsSlice = createSlice({ + name: 'valueSets', + initialState: valueSetsAdapter.getInitialState({ + loading: false, + error: false, + loaded: false, + cache: {} as { [system: string]: LabelObject[] } + }), + reducers: { + addValueSets: valueSetsAdapter.addMany, + updateCache: (state, action: PayloadAction<{ [system: string]: LabelObject[] }>) => { + return { + ...state, + cache: action.payload + } + } + }, + extraReducers: (builder) => { + builder.addCase(logout.fulfilled, () => + valueSetsAdapter.getInitialState({ loading: false, error: false, loaded: false, cache: {} }) + ) + } +}) + +export const { addValueSets, updateCache } = valueSetsSlice.actions +export default valueSetsSlice.reducer