21764: Add more type safety to TreePicker
[arvados.git] / services / workbench2 / src / store / multiselect / multiselect-reducer.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { multiselectActionConstants } from "./multiselect-actions";
6 import { TCheckedList } from "components/data-table/data-table";
7
8 export type MultiselectToolbarState = {
9     isVisible: boolean;
10     checkedList: TCheckedList;
11     disabledButtons: string[];
12 };
13
14 const multiselectToolbarInitialState = {
15     isVisible: false,
16     checkedList: {},
17     disabledButtons: []
18 };
19
20 const uncheckAllOthers = (inputList: TCheckedList, uuid: string) => {
21     const checkedlist = {...inputList}
22     for (const key in checkedlist) {
23         if (key !== uuid) checkedlist[key] = false;
24     }
25     return checkedlist;
26 };
27
28 const toggleOneCheck = (inputList: TCheckedList, uuid: string)=>{
29     const checkedlist = { ...inputList };
30     const isOnlyOneSelected = Object.values(checkedlist).filter(x => x === true).length === 1;
31     return { ...inputList, [uuid]: (checkedlist[uuid] && checkedlist[uuid] === true) && isOnlyOneSelected ? false : true };
32 }
33
34 const { TOGGLE_VISIBLITY, SET_CHECKEDLIST, SELECT_ONE, DESELECT_ONE, DESELECT_ALL_OTHERS, TOGGLE_ONE, ADD_DISABLED, REMOVE_DISABLED } = multiselectActionConstants;
35
36 export const multiselectReducer = (state: MultiselectToolbarState = multiselectToolbarInitialState, action) => {
37     switch (action.type) {
38         case TOGGLE_VISIBLITY:
39             return { ...state, isVisible: action.payload };
40         case SET_CHECKEDLIST:
41             return { ...state, checkedList: action.payload };
42         case SELECT_ONE:
43             return { ...state, checkedList: { ...state.checkedList, [action.payload]: true } };
44         case DESELECT_ONE:
45             return { ...state, checkedList: { ...state.checkedList, [action.payload]: false } };
46         case DESELECT_ALL_OTHERS:
47             return { ...state, checkedList: uncheckAllOthers(state.checkedList, action.payload) };
48         case TOGGLE_ONE:
49             return { ...state, checkedList: toggleOneCheck(state.checkedList, action.payload) };
50         case ADD_DISABLED:
51             return { ...state, disabledButtons: [...state.disabledButtons, action.payload]}
52         case REMOVE_DISABLED:
53             return { ...state, disabledButtons: state.disabledButtons.filter((button) => button !== action.payload) };
54         default:
55             return state;
56     }
57 };