21700: Install Bundler system-wide in Rails postinst
[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 toggleMSToolbar = (isVisible: boolean) => {
49     return dispatch => {
50         dispatch({ type: multiselectActionConstants.TOGGLE_VISIBLITY, payload: isVisible });
51     };
52 };
53
54 export const setCheckedListOnStore = (checkedList: TCheckedList) => {
55     return dispatch => {
56         dispatch(setSelectedUuid(isExactlyOneSelected(checkedList)))
57         dispatch({ type: multiselectActionConstants.SET_CHECKEDLIST, payload: checkedList });
58     };
59 };
60
61 export const selectOne = (uuid: string) => {
62     return dispatch => {
63         dispatch({ type: multiselectActionConstants.SELECT_ONE, payload: uuid });
64     };
65 };
66
67 export const deselectOne = (uuid: string) => {
68     return dispatch => {
69         dispatch({ type: multiselectActionConstants.DESELECT_ONE, payload: uuid });
70     };
71 };
72
73 export const deselectAllOthers = (uuid: string) => {
74     return dispatch => {
75         dispatch({ type: multiselectActionConstants.DESELECT_ALL_OTHERS, payload: uuid });
76     };
77 };
78
79 export const toggleOne = (uuid: string) => {
80     return dispatch => {
81         dispatch({ type: multiselectActionConstants.TOGGLE_ONE, payload: uuid });
82     };
83 };
84
85 export const setSelectedUuid = (uuid: string | null) => {
86     return dispatch => {
87         dispatch({ type: multiselectActionConstants.SET_SELECTED_UUID, payload: uuid });
88     };
89 };
90
91 export const addDisabledButton = (buttonName: string) => {
92     return dispatch => {
93         dispatch({ type: multiselectActionConstants.ADD_DISABLED, payload: buttonName });
94     };
95 };
96
97 export const removeDisabledButton = (buttonName: string) => {
98     return dispatch => {
99         dispatch({ type: multiselectActionConstants.REMOVE_DISABLED, payload: buttonName });
100     };
101 };
102