Skip to content

Commit

Permalink
add filtersExpandSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
craigrbarnes committed Jan 23, 2025
1 parent 01dfbff commit 86babc7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/core/src/features/cohort/filterExpandSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import type { CoreState } from '../../reducers';

// TODO: after multiple cohort support is merged move this to CohortBuilder
type ExpandedFiltersState = Record<string, Record<string, boolean>>;

const initialState: ExpandedFiltersState = {};

const expandSlice = createSlice({
name: 'CohortBuilder/filterExpand',
initialState: initialState,
reducers: {
toggleCohortBuilderCategoryFilter: (
state,
action: PayloadAction<{
index: string;
field: string;
expanded: boolean;
}>,
) => {
return {
...state,
[action.payload.index]: {
[action.payload.field]: action.payload.expanded,
},
};
},
toggleCohortBuilderAllFilters: (
state,
action: PayloadAction<{ index: string; expand: boolean }>,
) => {
return {
...state,
[action.payload.index]: Object.keys(state[action.payload.index]).reduce(
(acc, k) => {
acc[k] = action.payload.expand;
return acc;
},
{} as Record<string, boolean>,
),
};
},
},
});

export const cohortBuilderFiltersExpandedReducer = expandSlice.reducer;

export const {
toggleCohortBuilderCategoryFilter,
toggleCohortBuilderAllFilters,
} = expandSlice.actions;

export const selectFilterExpanded = (
state: CoreState,
index: string,
field: string,
): boolean => state.cohortFiltersExpanded?.[index]?.[field];

export const selectAllFiltersCollapsed = (
state: CoreState,
index: string,
): boolean =>
Object.values(state.cohortFiltersExpanded[index]).every((e) => !e);
7 changes: 7 additions & 0 deletions packages/core/src/features/cohort/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
clearCohortFilters,
} from './cohortSlice';

import {
toggleCohortBuilderCategoryFilter,
toggleCohortBuilderAllFilters,
} from './filterExpandSlice';

export {
selectCohortFilters,
selectIndexFilters,
Expand All @@ -27,4 +32,6 @@ export {
setCohortIndexFilters,
removeCohortFilter,
clearCohortFilters,
toggleCohortBuilderCategoryFilter,
toggleCohortBuilderAllFilters,
};
2 changes: 2 additions & 0 deletions packages/core/src/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
userAuthApiReducerPath,
userAuthApiReducer,
} from './features/user/userSliceRTK';
import { cohortBuilderFiltersExpandedReducer } from './features/cohort/filterExpandSlice';

export const rootReducer = combineReducers({
gen3Services: gen3ServicesReducer,
Expand All @@ -20,6 +21,7 @@ export const rootReducer = combineReducers({
drsHostnames: drsHostnamesReducer,
modals: modalReducer,
cohorts: cohortReducer,
cohortFiltersExpanded: cohortBuilderFiltersExpandedReducer,
activeWorkspace: activeWorkspaceReducer,
dataLibrarySelection: dataLibrarySelectionReducer,
[guppyApiSliceReducerPath]: guppyApiReducer,
Expand Down

0 comments on commit 86babc7

Please sign in to comment.