cd9ab4b17a657a3d77db1da736edde5f317721e8
[arvados-workbench2.git] / src / store / details-panel / details-panel-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from '~/common/unionize';
6 import { RootState } from '~/store/store';
7 import { Dispatch } from 'redux';
8 import { dialogActions } from '~/store/dialog/dialog-actions';
9 import { getResource } from '~/store/resources/resources';
10 import { ProjectResource } from "~/models/project";
11 import { ServiceRepository } from '~/services/services';
12 import { TagProperty } from '~/models/tag';
13 import { startSubmit, stopSubmit } from 'redux-form';
14 import { resourcesActions } from '~/store/resources/resources-actions';
15 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
16
17 export const detailsPanelActions = unionize({
18     TOGGLE_DETAILS_PANEL: ofType<{}>(),
19     LOAD_DETAILS_PANEL: ofType<string>()
20 });
21
22 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
23
24 export const PROJECT_PROPERTIES_FORM_NAME = 'projectPropertiesFormName';
25 export const PROJECT_PROPERTIES_DIALOG_NAME = 'projectPropertiesDialogName';
26
27 export const loadDetailsPanel = (uuid: string) => detailsPanelActions.LOAD_DETAILS_PANEL(uuid);
28
29 export const openProjectPropertiesDialog = () =>
30     (dispatch: Dispatch) => {
31         dispatch<any>(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } }));
32     };
33
34 export const deleteProjectProperty = (key: string) =>
35     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36         const { detailsPanel, resources } = getState();
37         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
38         try {
39             if (project) {
40                 delete project.properties[key];
41                 const updatedProject = await services.projectService.update(project.uuid, project);
42                 dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
43                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000 }));
44             }
45             return;
46         } catch (e) {
47             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME }));
48             throw new Error('Could not remove property from the project.');
49         }
50     };
51
52 export const createProjectProperty = (data: TagProperty) =>
53     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
54         const { detailsPanel, resources } = getState();
55         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
56         dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME));
57         try {
58             if (project) {
59                 project.properties[data.key] = data.value;
60                 const updatedProject = await services.projectService.update(project.uuid, project);
61                 dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
62                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000 }));
63                 dispatch(stopSubmit(PROJECT_PROPERTIES_FORM_NAME));
64             }
65             return;
66         } catch (e) {
67             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME }));
68             throw new Error('Could not add property to the project.');
69         }
70     };