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 type MultiselectToolbarState = {
10 checkedList: TCheckedList;
12 disabledButtons: string[];
15 const multiselectToolbarInitialState = {
22 const uncheckAllOthers = (inputList: TCheckedList, uuid: string) => {
23 const checkedlist = {...inputList}
24 for (const key in checkedlist) {
25 if (key !== uuid) checkedlist[key] = false;
30 const toggleOneCheck = (inputList: TCheckedList, uuid: string)=>{
31 const checkedlist = { ...inputList };
32 const isOnlyOneSelected = Object.values(checkedlist).filter(x => x === true).length === 1;
33 return { ...inputList, [uuid]: (checkedlist[uuid] && checkedlist[uuid] === true) && isOnlyOneSelected ? false : true };
36 const { TOGGLE_VISIBLITY, SET_CHECKEDLIST, SELECT_ONE, DESELECT_ONE, DESELECT_ALL_OTHERS, TOGGLE_ONE, SET_SELECTED_UUID, ADD_DISABLED, REMOVE_DISABLED } = multiselectActionConstants;
38 export const multiselectReducer = (state: MultiselectToolbarState = multiselectToolbarInitialState, action) => {
39 switch (action.type) {
40 case TOGGLE_VISIBLITY:
41 return { ...state, isVisible: action.payload };
43 return { ...state, checkedList: action.payload };
45 return { ...state, checkedList: { ...state.checkedList, [action.payload]: true } };
47 return { ...state, checkedList: { ...state.checkedList, [action.payload]: false } };
48 case DESELECT_ALL_OTHERS:
49 return { ...state, checkedList: uncheckAllOthers(state.checkedList, action.payload) };
51 return { ...state, checkedList: toggleOneCheck(state.checkedList, action.payload) };
52 case SET_SELECTED_UUID:
53 return {...state, selectedUuid: action.payload || ''}
55 return { ...state, disabledButtons: [...state.disabledButtons, action.payload]}
57 return { ...state, disabledButtons: state.disabledButtons.filter((button) => button !== action.payload) };