c5d472ade5a268de6d53ccaa39747b036438488e
[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, SnackbarKind} from '~/store/snackbar/snackbar-actions';
16 import { addProperty, deleteProperty } from '~/lib/resource-properties';
17
18 export const SLIDE_TIMEOUT = 500;
19
20 export const detailsPanelActions = unionize({
21     TOGGLE_DETAILS_PANEL: ofType<{}>(),
22     OPEN_DETAILS_PANEL: ofType<string>(),
23     LOAD_DETAILS_PANEL: ofType<string>()
24 });
25
26 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
27
28 export const PROJECT_PROPERTIES_FORM_NAME = 'projectPropertiesFormName';
29 export const PROJECT_PROPERTIES_DIALOG_NAME = 'projectPropertiesDialogName';
30
31 export const loadDetailsPanel = (uuid: string) => detailsPanelActions.LOAD_DETAILS_PANEL(uuid);
32
33 export const openDetailsPanel = (uuid: string) => detailsPanelActions.OPEN_DETAILS_PANEL(uuid);
34
35 export const openProjectPropertiesDialog = () =>
36     (dispatch: Dispatch) => {
37         dispatch<any>(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } }));
38     };
39
40 export const deleteProjectProperty = (key: string, value: string) =>
41     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
42         const { detailsPanel, resources } = getState();
43         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
44         if (!project) { return; }
45
46         const properties = Object.assign({}, project.properties);
47
48         try {
49             const updatedProject = await services.projectService.update(
50                 project.uuid, {
51                     properties: deleteProperty(properties, key, value),
52                 });
53             dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
54             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
55         } catch (e) {
56             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
57         }
58     };
59
60 export const createProjectProperty = (data: TagProperty) =>
61     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
62         const { detailsPanel, resources } = getState();
63         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
64         if (!project) { return; }
65
66         dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME));
67         try {
68             const key = data.keyID || data.key;
69             const value = data.valueID || data.value;
70             const properties = Object.assign({}, project.properties);
71             const updatedProject = await services.projectService.update(
72                 project.uuid, {
73                     properties: addProperty(properties, key, value),
74                 }
75             );
76             dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
77             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
78             dispatch(stopSubmit(PROJECT_PROPERTIES_FORM_NAME));
79         } catch (e) {
80             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
81         }
82     };
83 export const toggleDetailsPanel = () => (dispatch: Dispatch) => {
84     // because of material-ui issue resizing details panel breaks tabs.
85     // triggering window resize event fixes that.
86     setTimeout(() => {
87         window.dispatchEvent(new Event('resize'));
88     }, SLIDE_TIMEOUT);
89     dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
90 };