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