1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { multiselectActionConstants } from "./multiselect-actions";
6 import { TCheckedList } from "components/data-table/data-table";
8 export type MultiselectToolbarState = {
10 checkedList: TCheckedList;
11 disabledButtons: string[];
14 const multiselectToolbarInitialState = {
20 const uncheckAllOthers = (inputList: TCheckedList, uuid: string) => {
21 const checkedlist = {...inputList}
22 for (const key in checkedlist) {
23 if (key !== uuid) checkedlist[key] = false;
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 };
34 const { TOGGLE_VISIBLITY, SET_CHECKEDLIST, SELECT_ONE, DESELECT_ONE, DESELECT_ALL_OTHERS, TOGGLE_ONE, ADD_DISABLED, REMOVE_DISABLED } = multiselectActionConstants;
36 export const multiselectReducer = (state: MultiselectToolbarState = multiselectToolbarInitialState, action) => {
37 switch (action.type) {
38 case TOGGLE_VISIBLITY:
39 return { ...state, isVisible: action.payload };
41 return { ...state, checkedList: action.payload };
43 return { ...state, checkedList: { ...state.checkedList, [action.payload]: true } };
45 return { ...state, checkedList: { ...state.checkedList, [action.payload]: false } };
46 case DESELECT_ALL_OTHERS:
47 return { ...state, checkedList: uncheckAllOthers(state.checkedList, action.payload) };
49 return { ...state, checkedList: toggleOneCheck(state.checkedList, action.payload) };
51 return { ...state, disabledButtons: [...state.disabledButtons, action.payload]}
53 return { ...state, disabledButtons: state.disabledButtons.filter((button) => button !== action.payload) };