]> git.arvados.org - arvados.git/blob - services/workbench2/src/store/multiselect/multiselect-actions.tsx
22408: added condition for >1 selected
[arvados.git] / services / workbench2 / src / store / multiselect / multiselect-actions.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { TCheckedList } from "components/data-table/data-table";
6 import { ContainerRequestResource } from "models/container-request";
7 import { Dispatch } from "redux";
8 import { navigateTo } from "store/navigation/navigation-action";
9 import { snackbarActions } from "store/snackbar/snackbar-actions";
10 import { RootState } from "store/store";
11 import { ServiceRepository } from "services/services";
12 import { SnackbarKind } from "store/snackbar/snackbar-actions";
13 import { ContextMenuResource } from 'store/context-menu/context-menu-actions';
14
15 export const multiselectActionConstants = {
16     TOGGLE_VISIBLITY: "TOGGLE_VISIBLITY",
17     SET_CHECKEDLIST: "SET_CHECKEDLIST",
18     SELECT_ONE: 'SELECT_ONE',
19     DESELECT_ONE: "DESELECT_ONE",
20     DESELECT_ALL_OTHERS: 'DESELECT_ALL_OTHERS',
21     TOGGLE_ONE: 'TOGGLE_ONE',
22     SET_SELECTED_UUID: 'SET_SELECTED_UUID',
23     ADD_DISABLED: 'ADD_DISABLED',
24     REMOVE_DISABLED: 'REMOVE_DISABLED',
25 };
26
27 export const msNavigateToOutput = (resource: ContextMenuResource | ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
28     try {
29         await services.collectionService.get(resource.outputUuid || '');
30         dispatch<any>(navigateTo(resource.outputUuid || ''));
31     } catch {
32         dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Output collection was trashed or deleted.", hideDuration: 4000, kind: SnackbarKind.WARNING }));
33     }
34 };
35
36 export const isExactlyOneSelected = (checkedList: TCheckedList) => {
37     let tally = 0;
38     let current = '';
39     for (const uuid in checkedList) {
40         if (checkedList[uuid] === true) {
41             tally++;
42             current = uuid;
43         }
44     }
45     return tally === 1 ? current : null
46 };
47
48 export const isMoreThanOneSelected = (checkedList: TCheckedList) => {
49     let tally = 0;
50     for (const uuid in checkedList) {
51         if (checkedList[uuid] === true) {
52             tally++;
53         }
54     }
55     return tally > 1 ? true : false
56 };
57
58 export const toggleMSToolbar = (isVisible: boolean) => {
59     return dispatch => {
60         dispatch({ type: multiselectActionConstants.TOGGLE_VISIBLITY, payload: isVisible });
61     };
62 };
63
64 export const setCheckedListOnStore = (checkedList: TCheckedList) => {
65     return dispatch => {
66         dispatch(setSelectedUuid(isExactlyOneSelected(checkedList)))
67         dispatch({ type: multiselectActionConstants.SET_CHECKEDLIST, payload: checkedList });
68     };
69 };
70
71 export const selectOne = (uuid: string) => {
72     return dispatch => {
73         dispatch({ type: multiselectActionConstants.SELECT_ONE, payload: uuid });
74     };
75 };
76
77 export const deselectOne = (uuid: string) => {
78     return dispatch => {
79         dispatch({ type: multiselectActionConstants.DESELECT_ONE, payload: uuid });
80     };
81 };
82
83 export const deselectAllOthers = (uuid: string) => {
84     return dispatch => {
85         dispatch({ type: multiselectActionConstants.DESELECT_ALL_OTHERS, payload: uuid });
86     };
87 };
88
89 export const toggleOne = (uuid: string) => {
90     return dispatch => {
91         dispatch({ type: multiselectActionConstants.TOGGLE_ONE, payload: uuid });
92     };
93 };
94
95 export const setSelectedUuid = (uuid: string | null) => {
96     return dispatch => {
97         dispatch({ type: multiselectActionConstants.SET_SELECTED_UUID, payload: uuid });
98     };
99 };
100
101 export const addDisabledButton = (buttonName: string) => {
102     return dispatch => {
103         dispatch({ type: multiselectActionConstants.ADD_DISABLED, payload: buttonName });
104     };
105 };
106
107 export const removeDisabledButton = (buttonName: string) => {
108     return dispatch => {
109         dispatch({ type: multiselectActionConstants.REMOVE_DISABLED, payload: buttonName });
110     };
111 };
112