21846: applied select to context menu open
[arvados.git] / services / workbench2 / src / store / api-client-authorizations / api-client-authorizations-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 { ServiceRepository } from "services/services";
9 import { dialogActions } from 'store/dialog/dialog-actions';
10 import { snackbarActions } from 'store/snackbar/snackbar-actions';
11 import { navigateToRootProject } from 'store/navigation/navigation-action';
12 import { ApiClientAuthorization } from 'models/api-client-authorization';
13 import { bindDataExplorerActions } from 'store/data-explorer/data-explorer-action';
14 import { getResource } from 'store/resources/resources';
15
16
17 export const API_CLIENT_AUTHORIZATION_PANEL_ID = 'apiClientAuthorizationPanelId';
18 export const apiClientAuthorizationsActions = bindDataExplorerActions(API_CLIENT_AUTHORIZATION_PANEL_ID);
19
20 export const API_CLIENT_AUTHORIZATION_REMOVE_DIALOG = 'apiClientAuthorizationRemoveDialog';
21 export const API_CLIENT_AUTHORIZATION_ATTRIBUTES_DIALOG = 'apiClientAuthorizationAttributesDialog';
22 export const API_CLIENT_AUTHORIZATION_HELP_DIALOG = 'apiClientAuthorizationHelpDialog';
23
24
25 export const loadApiClientAuthorizationsPanel = () =>
26     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
27         const user = getState().auth.user;
28         if (user && user.isAdmin) {
29             try {
30                 dispatch(setBreadcrumbs([{ label: 'Api client authorizations' }]));
31                 dispatch(apiClientAuthorizationsActions.REQUEST_ITEMS());
32             } catch (e) {
33                 return;
34             }
35         } else {
36             dispatch(navigateToRootProject);
37             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000 }));
38         }
39     };
40
41 export const openApiClientAuthorizationAttributesDialog = (uuid: string) =>
42     (dispatch: Dispatch, getState: () => RootState) => {
43         const { resources } = getState();
44         const apiClientAuthorization = getResource<ApiClientAuthorization>(uuid)(resources);
45         dispatch(dialogActions.OPEN_DIALOG({ id: API_CLIENT_AUTHORIZATION_ATTRIBUTES_DIALOG, data: { apiClientAuthorization } }));
46     };
47
48 export const openApiClientAuthorizationRemoveDialog = (uuid: string) =>
49     (dispatch: Dispatch, getState: () => RootState) => {
50         dispatch(dialogActions.OPEN_DIALOG({
51             id: API_CLIENT_AUTHORIZATION_REMOVE_DIALOG,
52             data: {
53                 title: 'Remove api client authorization',
54                 text: 'Are you sure you want to remove this api client authorization?',
55                 confirmButtonLabel: 'Remove',
56                 uuid
57             }
58         }));
59     };
60
61 export const removeApiClientAuthorization = (uuid: string) =>
62     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
63         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
64         try {
65             await services.apiClientAuthorizationService.delete(uuid);
66             dispatch(apiClientAuthorizationsActions.REQUEST_ITEMS());
67             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Api client authorization has been successfully removed.', hideDuration: 2000 }));
68         } catch (e) {
69             return;
70         }
71     };
72
73 export const openApiClientAuthorizationsHelpDialog = () =>
74     (dispatch: Dispatch, getState: () => RootState) => {
75         const apiHost = getState().properties.apiHost;
76         const user = getState().auth.user;
77         const email = user ? user.email : '';
78         const apiToken = getState().auth.apiToken;
79         dispatch(dialogActions.OPEN_DIALOG({ id: API_CLIENT_AUTHORIZATION_HELP_DIALOG, data: { apiHost, apiToken, email } }));
80     };