Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 14433_properties_i...
[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 SLIDE_TIMEOUT = 500;
18
19 export const detailsPanelActions = unionize({
20     TOGGLE_DETAILS_PANEL: ofType<{}>(),
21     LOAD_DETAILS_PANEL: ofType<string>()
22 });
23
24 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
25
26 export const PROJECT_PROPERTIES_FORM_NAME = 'projectPropertiesFormName';
27 export const PROJECT_PROPERTIES_DIALOG_NAME = 'projectPropertiesDialogName';
28
29 export const loadDetailsPanel = (uuid: string) => detailsPanelActions.LOAD_DETAILS_PANEL(uuid);
30
31 export const openProjectPropertiesDialog = () =>
32     (dispatch: Dispatch) => {
33         dispatch<any>(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } }));
34     };
35
36 export const deleteProjectProperty = (key: string) =>
37     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38         const { detailsPanel, resources } = getState();
39         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
40         try {
41             if (project) {
42                 delete project.properties[key];
43                 const updatedProject = await services.projectService.update(project.uuid, project);
44                 dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
45                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000 }));
46             }
47         } catch (e) {
48             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME }));
49             throw new Error('Could not remove property from the project.');
50         }
51     };
52
53 export const createProjectProperty = (data: TagProperty) =>
54     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55         const { detailsPanel, resources } = getState();
56         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
57         dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME));
58         try {
59             if (project) {
60                 project.properties[data.key] = data.value;
61                 const updatedProject = await services.projectService.update(project.uuid, project);
62                 dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
63                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000 }));
64                 dispatch(stopSubmit(PROJECT_PROPERTIES_FORM_NAME));
65             }
66             return;
67         } catch (e) {
68             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME }));
69             throw new Error('Could not add property to the project.');
70         }
71     };
72 export const toggleDetailsPanel = () => (dispatch: Dispatch) => {
73     // because of material-ui issue resizing details panel breaks tabs.
74     // triggering window resize event fixes that.
75     setTimeout(() => {
76         window.dispatchEvent(new Event('resize'));
77     }, SLIDE_TIMEOUT);
78     dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
79 };