UI Improvements
[arvados-workbench2.git] / src / store / compute-nodes / compute-nodes-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from '~/store/store';
7 import { setBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
8 import { dialogActions } from '~/store/dialog/dialog-actions';
9 import {snackbarActions, SnackbarKind} from '~/store/snackbar/snackbar-actions';
10 import { navigateToRootProject } from '~/store/navigation/navigation-action';
11 import { bindDataExplorerActions } from '~/store/data-explorer/data-explorer-action';
12 import { getResource } from '~/store/resources/resources';
13 import { ServiceRepository } from "~/services/services";
14 import { NodeResource } from '~/models/node';
15
16 export const COMPUTE_NODE_PANEL_ID = "computeNodeId";
17 export const computeNodesActions = bindDataExplorerActions(COMPUTE_NODE_PANEL_ID);
18
19 export const COMPUTE_NODE_REMOVE_DIALOG = 'computeNodeRemoveDialog';
20 export const COMPUTE_NODE_ATTRIBUTES_DIALOG = 'computeNodeAttributesDialog';
21
22 export const loadComputeNodesPanel = () =>
23     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
24         const user = getState().auth.user;
25         if (user && user.isAdmin) {
26             try {
27                 dispatch(setBreadcrumbs([{ label: 'Compute Nodes' }]));
28                 dispatch(computeNodesActions.REQUEST_ITEMS());
29             } catch (e) {
30                 return;
31             }
32         } else {
33             dispatch(navigateToRootProject);
34             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000, kind: SnackbarKind.ERROR }));
35         }
36     };
37
38 export const openComputeNodeAttributesDialog = (uuid: string) =>
39     (dispatch: Dispatch, getState: () => RootState) => {
40         const { resources } = getState();
41         const computeNode = getResource<NodeResource>(uuid)(resources);
42         dispatch(dialogActions.OPEN_DIALOG({ id: COMPUTE_NODE_ATTRIBUTES_DIALOG, data: { computeNode } }));
43     };
44
45 export const openComputeNodeRemoveDialog = (uuid: string) =>
46     (dispatch: Dispatch, getState: () => RootState) => {
47         dispatch(dialogActions.OPEN_DIALOG({
48             id: COMPUTE_NODE_REMOVE_DIALOG,
49             data: {
50                 title: 'Remove compute node',
51                 text: 'Are you sure you want to remove this compute node?',
52                 confirmButtonLabel: 'Remove',
53                 uuid
54             }
55         }));
56     };
57
58 export const removeComputeNode = (uuid: string) =>
59     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
60         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
61         try {
62             await services.nodeService.delete(uuid);
63             dispatch(computeNodesActions.REQUEST_ITEMS());
64             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Compute node has been successfully removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
65         } catch (e) {
66             return;
67         }
68     };