Skip to content

Commit

Permalink
Merge pull request #231 from tnc-ca-geo/henry-checkbox
Browse files Browse the repository at this point in the history
Update label filter to reduce use of none selected
  • Loading branch information
nathanielrindlaub authored Aug 22, 2024
2 parents 6747eaa + f639ae1 commit d32c239
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
15 changes: 3 additions & 12 deletions src/features/filters/BulkSelectCheckbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const StyledBulkSelectCheckbox = styled('div', {

const BulkSelectCheckbox = ({ filterCat, managedIds, isHeader }) => {
const activeFilters = useSelector(selectActiveFilters);
const [checkboxState, setCheckboxState] = useState('someSelected');
const [checkboxState, setCheckboxState] = useState('allSelected');
const dispatch = useDispatch();

const stateMap = {
noneSelected: {
notAllSelected: {
checked: false,
active: false,
indeterminate: false,
Expand All @@ -40,25 +40,16 @@ const BulkSelectCheckbox = ({ filterCat, managedIds, isHeader }) => {
indeterminate: false,
label: 'unselect all',
},
someSelected: {
checked: false,
active: true,
indeterminate: true,
label: 'clear selection',
},
};

useEffect(() => {
const allSelected = (idsToCheck, activeIds) =>
(activeIds && idsToCheck.every((id) => activeIds.includes(id))) || activeIds === null;
const noneSelected = (idsToCheck, activeIds) => activeIds && idsToCheck.every((id) => !activeIds.includes(id));

if (allSelected(managedIds, activeFilters[filterCat])) {
setCheckboxState('allSelected');
} else if (noneSelected(managedIds, activeFilters[filterCat])) {
setCheckboxState('noneSelected');
} else {
setCheckboxState('someSelected');
setCheckboxState('notAllSelected');
}
}, [activeFilters, filterCat, managedIds]);

Expand Down
6 changes: 3 additions & 3 deletions src/features/filters/filtersSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ export const filtersSlice = createSlice({
const availIds = state.availFilters[filterCat].options.map(({ _id }) => _id);
let newActiveIds;

if (currState === 'noneSelected') {
// none are currently selected, so add all managedIds to activeFilters
if (currState !== 'allSelected') {
// not all labels are currently selected, so add all managedIds to activeFilters
const idsToAdd = managedIds.filter((id) => !activeIds.includes(id));
newActiveIds = activeIds.concat(idsToAdd);
} else {
// some or all managed ids are selected, so unselect all:
// all managed ids are selected, so unselect all:
// i.e, return all available Ids, minus the managed ids
newActiveIds =
activeIds === null
Expand Down

0 comments on commit d32c239

Please sign in to comment.