1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
15 export const apiClientAuthorizationsActions = unionize({
16 SET_API_CLIENT_AUTHORIZATIONS: ofType<ApiClientAuthorization[]>(),
17 REMOVE_API_CLIENT_AUTHORIZATION: ofType<string>()
20 export type ApiClientAuthorizationsActions = UnionOf<typeof apiClientAuthorizationsActions>;
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';
26 export const loadApiClientAuthorizationsPanel = () =>
27 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
28 const user = getState().auth.user;
29 if (user && user.isAdmin) {
31 dispatch(setBreadcrumbs([{ label: 'Api client authorizations' }]));
32 const response = await services.apiClientAuthorizationService.list();
33 dispatch(apiClientAuthorizationsActions.SET_API_CLIENT_AUTHORIZATIONS(response.items));
35 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000 }));
39 dispatch(navigateToRootProject);
40 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "You don't have permissions to view this page", hideDuration: 2000 }));
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 } }));
50 export const openApiClientAuthorizationRemoveDialog = (uuid: string) =>
51 (dispatch: Dispatch, getState: () => RootState) => {
52 dispatch(dialogActions.OPEN_DIALOG({
53 id: API_CLIENT_AUTHORIZATION_REMOVE_DIALOG,
55 title: 'Remove api client authorization',
56 text: 'Are you sure you want to remove this api client authorization?',
57 confirmButtonLabel: 'Remove',
63 export const removeApiClientAuthorization = (uuid: string) =>
64 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
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 }));
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 } }));