X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/9fb0741fb5dc74d7142c1d7b217ba60c242c3266..562687ce72e709d485aa47773117a51a764a6606:/services/workbench2/src/store/multiselect/multiselect-reducer.tsx

diff --git a/services/workbench2/src/store/multiselect/multiselect-reducer.tsx b/services/workbench2/src/store/multiselect/multiselect-reducer.tsx
index 5f99511255..b73a4be4bb 100644
--- a/services/workbench2/src/store/multiselect/multiselect-reducer.tsx
+++ b/services/workbench2/src/store/multiselect/multiselect-reducer.tsx
@@ -5,21 +5,33 @@
 import { multiselectActionConstants } from "./multiselect-actions";
 import { TCheckedList } from "components/data-table/data-table";
 
-type MultiselectToolbarState = {
+export type MultiselectToolbarState = {
     isVisible: boolean;
     checkedList: TCheckedList;
-    selectedUuid: string;
     disabledButtons: string[];
 };
 
 const multiselectToolbarInitialState = {
     isVisible: false,
     checkedList: {},
-    selectedUuid: '',
     disabledButtons: []
 };
 
-const { TOGGLE_VISIBLITY, SET_CHECKEDLIST, SELECT_ONE, DESELECT_ONE, TOGGLE_ONE, SET_SELECTED_UUID, ADD_DISABLED, REMOVE_DISABLED } = multiselectActionConstants;
+const uncheckAllOthers = (inputList: TCheckedList, uuid: string) => {
+    const checkedlist = {...inputList}
+    for (const key in checkedlist) {
+        if (key !== uuid) checkedlist[key] = false;
+    }
+    return checkedlist;
+};
+
+const toggleOneCheck = (inputList: TCheckedList, uuid: string)=>{
+    const checkedlist = { ...inputList };
+    const isOnlyOneSelected = Object.values(checkedlist).filter(x => x === true).length === 1;
+    return { ...inputList, [uuid]: (checkedlist[uuid] && checkedlist[uuid] === true) && isOnlyOneSelected ? false : true };
+}
+
+const { TOGGLE_VISIBLITY, SET_CHECKEDLIST, SELECT_ONE, DESELECT_ONE, DESELECT_ALL_OTHERS, TOGGLE_ONE, ADD_DISABLED, REMOVE_DISABLED } = multiselectActionConstants;
 
 export const multiselectReducer = (state: MultiselectToolbarState = multiselectToolbarInitialState, action) => {
     switch (action.type) {
@@ -31,10 +43,10 @@ export const multiselectReducer = (state: MultiselectToolbarState = multiselectT
             return { ...state, checkedList: { ...state.checkedList, [action.payload]: true } };
         case DESELECT_ONE:
             return { ...state, checkedList: { ...state.checkedList, [action.payload]: false } };
+        case DESELECT_ALL_OTHERS:
+            return { ...state, checkedList: uncheckAllOthers(state.checkedList, action.payload) };
         case TOGGLE_ONE:
-            return { ...state, checkedList: { ...state.checkedList, [action.payload]: !state.checkedList[action.payload] } };
-        case SET_SELECTED_UUID:
-            return {...state, selectedUuid: action.payload || ''}
+            return { ...state, checkedList: toggleOneCheck(state.checkedList, action.payload) };
         case ADD_DISABLED:
             return { ...state, disabledButtons: [...state.disabledButtons, action.payload]}
         case REMOVE_DISABLED:
