Skip to content

Commit 6bca4d5

Browse files
committed
Add filter CombineModeSlice
1 parent 38b67a4 commit 6bca4d5

File tree

17 files changed

+250
-154
lines changed

17 files changed

+250
-154
lines changed

packages/core/src/features/cohort/cohortBuilderConfigSlice.ts

Lines changed: 0 additions & 92 deletions
This file was deleted.

packages/core/src/features/cohort/cohortSlice.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ export const {
173173
} = cohortSlice.actions;
174174

175175
export const selectCohortFilters = (state: CoreState): IndexedFilterSet =>
176-
state.cohorts.cohort.filters;
176+
state.cohort.cohort.cohort.filters;
177177

178178
export const selectCurrentCohortId = (state: CoreState): string =>
179-
state.cohorts.cohort.id;
179+
state.cohort.cohort.cohort.id;
180180

181181
export const selectCurrentCohort = (state: CoreState): Cohort =>
182-
state.cohorts.cohort;
182+
state.cohort.cohort.cohort;
183183

184184
export const selectCurrentCohortName = (state: CoreState): string =>
185-
state.cohorts.cohort.name;
185+
state.cohort.cohort.cohort.name;
186186

187187
/**
188188
* Select a filter by its name from the current cohort. If the filter is not found
@@ -196,7 +196,7 @@ export const selectIndexedFilterByName = (
196196
index: string,
197197
name: string,
198198
): Operation | undefined => {
199-
return state.cohorts.cohort.filters[index]?.root[name];
199+
return state.cohort.cohort.cohort.filters[index]?.root[name];
200200
};
201201

202202
const EmptyFilterSet: FilterSet = { mode: 'and', root: {} };
@@ -211,7 +211,7 @@ export const selectIndexFilters = (
211211
state: CoreState,
212212
index: string,
213213
): FilterSet => {
214-
return state.cohorts.cohort.filters?.[index] ?? EmptyFilterSet; // TODO: check if this is undefined
214+
return state.cohort.cohort.cohort.filters?.[index] ?? EmptyFilterSet; // TODO: check if this is undefined
215215
};
216216

217217
export const cohortReducer = cohortSlice.reducer;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
2+
import type { CoreState } from '../../reducers';
3+
import { CombineMode } from './types';
4+
5+
type CombineModeFiltersState = Record<string, Record<string, CombineMode>>;
6+
7+
const initialState: CombineModeFiltersState = {};
8+
9+
const expandSlice = createSlice({
10+
name: 'CohortBuilder/filterCombineMode',
11+
initialState: initialState,
12+
reducers: {
13+
setCohortFilterCombineMode: (
14+
state,
15+
action: PayloadAction<{
16+
index: string;
17+
field: string;
18+
mode: CombineMode;
19+
}>,
20+
) => {
21+
return {
22+
...state,
23+
[action.payload.index]: {
24+
...state[action.payload.index],
25+
[action.payload.field]: action.payload.mode,
26+
},
27+
};
28+
},
29+
},
30+
});
31+
32+
export const cohortBuilderFiltersCombineModeReducer = expandSlice.reducer;
33+
34+
export const { setCohortFilterCombineMode } = expandSlice.actions;
35+
36+
export const selectCohortFilterCombineMode = (
37+
state: CoreState,
38+
index: string,
39+
field: string,
40+
): CombineMode => state.cohort.filtersCombineMode?.[index]?.[field] ?? 'or';

packages/core/src/features/cohort/filterExpandSlice.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ export const selectCohortFilterExpanded = (
5555
state: CoreState,
5656
index: string,
5757
field: string,
58-
): boolean => state.cohortFiltersExpanded?.[index]?.[field];
58+
): boolean => state.cohort.filtersExpanded?.[index]?.[field];
5959

6060
export const selectAllCohortFiltersCollapsed = (
6161
state: CoreState,
6262
index: string,
6363
): boolean =>
6464
index in state.cohortFiltersExpanded
65-
? Object.values(state.cohortFiltersExpanded?.[index]).every((e) => !e)
65+
? Object.values(state.cohort.filtersExpanded?.[index]).every((e) => !e)
6666
: false;

packages/core/src/features/cohort/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// import all the components from this directory
22
import {
3-
cohortReducer,
43
selectCohortFilters,
54
selectIndexFilters,
65
selectIndexedFilterByName,
@@ -20,6 +19,12 @@ import {
2019
selectCohortFilterExpanded,
2120
selectAllCohortFiltersCollapsed,
2221
} from './filterExpandSlice';
22+
import { type CombineMode } from './types';
23+
24+
import {
25+
setCohortFilterCombineMode,
26+
selectCohortFilterCombineMode,
27+
} from './filterCombineModeSlice';
2328

2429
export {
2530
selectCohortFilters,
@@ -28,14 +33,16 @@ export {
2833
selectCurrentCohortId,
2934
selectCurrentCohortName,
3035
selectCurrentCohort,
31-
cohortReducer,
36+
selectCohortFilterExpanded,
37+
selectAllCohortFiltersCollapsed,
38+
selectCohortFilterCombineMode,
3239
updateCohortFilter,
3340
setCohortFilter,
3441
setCohortIndexFilters,
3542
removeCohortFilter,
3643
clearCohortFilters,
3744
toggleCohortBuilderCategoryFilter,
3845
toggleCohortBuilderAllFilters,
39-
selectCohortFilterExpanded,
40-
selectAllCohortFiltersCollapsed,
46+
setCohortFilterCombineMode,
47+
type CombineMode,
4148
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { combineReducers } from '@reduxjs/toolkit';
2+
import { cohortReducer } from './cohortSlice';
3+
import { cohortBuilderFiltersExpandedReducer } from './filterExpandSlice';
4+
import { cohortBuilderFiltersCombineModeReducer } from './filterCombineModeSlice';
5+
6+
export const cohortReducers = combineReducers({
7+
filtersExpanded: cohortBuilderFiltersExpandedReducer,
8+
filtersCombineMode: cohortBuilderFiltersCombineModeReducer,
9+
cohort: cohortReducer,
10+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type CombineMode = 'and' | 'or';

packages/core/src/features/filters/filters.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,36 @@ import {
2020
Union,
2121
} from './types';
2222

23+
export type OperatorWithFieldAndArrayOfOperands =
24+
| Includes
25+
| Excludes
26+
| ExcludeIfAny;
27+
2328
export const isOperationWithField = (
2429
operation: OperationWithField | Operation,
2530
): operation is OperationWithField => {
2631
return (operation as OperationWithField)?.field !== undefined;
2732
};
33+
34+
export const isOperatorWithFieldAndArrayOfOperands = (
35+
operation: unknown,
36+
): operation is OperatorWithFieldAndArrayOfOperands => {
37+
if (
38+
typeof operation === 'object' &&
39+
operation !== null &&
40+
'operands' in operation &&
41+
Array.isArray((operation as any).operands) &&
42+
'field' in operation &&
43+
typeof (operation as any).field === 'string' // Assuming `field` should be a string
44+
) {
45+
const { operator } = (operation as any).operator;
46+
return (
47+
operator === 'in' || operator === 'exclude' || operator === 'excludeifany'
48+
);
49+
}
50+
return false;
51+
};
52+
2853
export const extractFilterValue = (op: Operation): FilterValue => {
2954
const valueExtractorHandler = new ValueExtractorHandler();
3055
return handleOperation<FilterValue>(valueExtractorHandler, op);

packages/core/src/features/filters/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import {
22
isFilterEmpty,
33
isOperationWithField,
44
convertFilterSetToGqlFilter,
5+
isOperatorWithFieldAndArrayOfOperands,
56
handleOperation,
67
extractEnumFilterValue,
78
extractFilterValue,
9+
type OperatorWithFieldAndArrayOfOperands,
810
type GQLFilter,
911
} from './filters';
1012

@@ -15,8 +17,10 @@ export {
1517
handleOperation,
1618
isFilterEmpty,
1719
isOperationWithField,
20+
isOperatorWithFieldAndArrayOfOperands,
1821
convertFilterSetToGqlFilter,
1922
extractFilterValue,
2023
extractEnumFilterValue,
24+
type OperatorWithFieldAndArrayOfOperands,
2125
type GQLFilter,
2226
};

packages/core/src/reducers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@ import { userReducer } from './features/user/userSlice';
44
import { gen3AppReducer } from './features/gen3Apps/gen3AppsSlice';
55
import { drsHostnamesReducer } from './features/drsResolver';
66
import { modalReducer } from './features/modals/modalsSlice';
7-
import { cohortReducer } from './features/cohort';
87
import { dataLibrarySelectionReducer } from './features/dataLibrary';
98
import { activeWorkspaceReducer } from './features/workspace/workspaceSlice';
109
import { guppyApiReducer, guppyApiSliceReducerPath } from './features/guppy';
1110
import {
1211
userAuthApiReducerPath,
1312
userAuthApiReducer,
1413
} from './features/user/userSliceRTK';
15-
import { cohortBuilderFiltersExpandedReducer } from './features/cohort/filterExpandSlice';
14+
import { cohortReducers } from './features/cohort/reducers';
1615

1716
export const rootReducer = combineReducers({
1817
gen3Services: gen3ServicesReducer,
1918
user: userReducer,
2019
gen3Apps: gen3AppReducer,
2120
drsHostnames: drsHostnamesReducer,
2221
modals: modalReducer,
23-
cohorts: cohortReducer,
24-
cohortFiltersExpanded: cohortBuilderFiltersExpandedReducer,
22+
cohorts: cohortReducers,
2523
activeWorkspace: activeWorkspaceReducer,
2624
dataLibrarySelection: dataLibrarySelectionReducer,
2725
[guppyApiSliceReducerPath]: guppyApiReducer,

0 commit comments

Comments
 (0)