Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / 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 multiselectActionContants = {
16     TOGGLE_VISIBLITY: "TOGGLE_VISIBLITY",
17     SET_CHECKEDLIST: "SET_CHECKEDLIST",
18     SELECT_ONE: 'SELECT_ONE',
19     DESELECT_ONE: "DESELECT_ONE",
20     TOGGLE_ONE: 'TOGGLE_ONE',
21     SET_SELECTED_UUID: 'SET_SELECTED_UUID',
22     ADD_DISABLED: 'ADD_DISABLED',
23     REMOVE_DISABLED: 'REMOVE_DISABLED',
24 };
25
26 export const msNavigateToOutput = (resource: ContextMenuResource | ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
27     try {
28         await services.collectionService.get(resource.outputUuid || '');
29         dispatch<any>(navigateTo(resource.outputUuid || ''));
30     } catch {
31         dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Output collection was trashed or deleted.", hideDuration: 4000, kind: SnackbarKind.WARNING }));
32     }
33 };
34
35 export const isExactlyOneSelected = (checkedList: TCheckedList) => {
36     let tally = 0;
37     let current = '';
38     for (const uuid in checkedList) {
39         if (checkedList[uuid] === true) {
40             tally++;
41             current = uuid;
42         }
43     }
44     return tally === 1 ? current : null
45 };
46
47 export const toggleMSToolbar = (isVisible: boolean) => {
48     return dispatch => {
49         dispatch({ type: multiselectActionContants.TOGGLE_VISIBLITY, payload: isVisible });
50     };
51 };
52
53 export const setCheckedListOnStore = (checkedList: TCheckedList) => {
54     return dispatch => {
55         dispatch(setSelectedUuid(isExactlyOneSelected(checkedList)))
56         dispatch({ type: multiselectActionContants.SET_CHECKEDLIST, payload: checkedList });
57     };
58 };
59
60 export const selectOne = (uuid: string) => {
61     return dispatch => {
62         dispatch({ type: multiselectActionContants.SELECT_ONE, payload: uuid });
63     };
64 };
65
66 export const deselectOne = (uuid: string) => {
67     return dispatch => {
68         dispatch({ type: multiselectActionContants.DESELECT_ONE, payload: uuid });
69     };
70 };
71
72 export const toggleOne = (uuid: string) => {
73     return dispatch => {
74         dispatch({ type: multiselectActionContants.TOGGLE_ONE, payload: uuid });
75     };
76 };
77
78 export const setSelectedUuid = (uuid: string | null) => {
79     return dispatch => {
80         dispatch({ type: multiselectActionContants.SET_SELECTED_UUID, payload: uuid });
81     };
82 };
83
84 export const addDisabledButton = (buttonName: string) => {
85     return dispatch => {
86         dispatch({ type: multiselectActionContants.ADD_DISABLED, payload: buttonName });
87     };
88 };
89
90 export const removeDisabledButton = (buttonName: string) => {
91     return dispatch => {
92         dispatch({ type: multiselectActionContants.REMOVE_DISABLED, payload: buttonName });
93     };
94 };
95
96 export const multiselectActions = {
97     toggleMSToolbar,
98     setCheckedListOnStore,
99     selectOne,
100     deselectOne,
101     toggleOne,
102     setSelectedUuid,
103     addDisabledButton,
104     removeDisabledButton,
105 };