add keep services panel with state and all dialogs for actions
[arvados-workbench2.git] / src / store / keep-services / keep-services-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 { KeepServiceResource } from '~/models/keep-services';
11 import { dialogActions } from '~/store/dialog/dialog-actions';
12 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
13
14 export const keepServicesActions = unionize({
15     SET_KEEP_SERVICES: ofType<KeepServiceResource[]>(),
16     REMOVE_KEEP_SERVICE: ofType<string>(),
17     RESET_KEEP_SERVICES: ofType<{}>()
18 });
19
20 export type KeepServicesActions = UnionOf<typeof keepServicesActions>;
21
22 export const KEEP_SERVICE_REMOVE_DIALOG = 'keepServiceRemoveDialog';
23 export const KEEP_SERVICE_ATTRIBUTES_DIALOG = 'keepServiceAttributesDialog';
24
25 // ToDo: access denied for loading keepService and reset data and redirect
26 export const loadKeepServicesPanel = () =>
27     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
28         try {
29             dispatch(setBreadcrumbs([{ label: 'Keep Services' }]));
30             const response = await services.keepService.list();
31             dispatch(keepServicesActions.SET_KEEP_SERVICES(response.items));
32         } catch (e) {
33             return;
34         }
35     };
36
37 export const openKeepServiceAttributesDialog = (index: number) =>
38     (dispatch: Dispatch, getState: () => RootState) => {
39         const keepService = getState().keepServices[index];
40         dispatch(dialogActions.OPEN_DIALOG({ id: KEEP_SERVICE_ATTRIBUTES_DIALOG, data: { keepService } }));
41     };
42
43 export const openKeepServiceRemoveDialog = (uuid: string) =>
44     (dispatch: Dispatch, getState: () => RootState) => {
45         dispatch(dialogActions.OPEN_DIALOG({
46             id: KEEP_SERVICE_REMOVE_DIALOG,
47             data: {
48                 title: 'Remove keep service',
49                 text: 'Are you sure you want to remove this keep service?',
50                 confirmButtonLabel: 'Remove',
51                 uuid
52             }
53         }));
54     };
55
56 // ToDo: access denied for removing keepService and reset data and redirect
57 export const removeKeepService = (uuid: string) =>
58     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
59         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
60         try {
61             await services.keepService.delete(uuid);
62             dispatch(keepServicesActions.REMOVE_KEEP_SERVICE(uuid));
63             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Keep service has been successfully removed.', hideDuration: 2000 }));
64         } catch (e) {
65             return;
66         }
67     };