5d3133caee6e8f84516d689515fc648ec402eee2
[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 import { FilterBuilder } from '~/services/api/filter-builder';
18 import { OrderBuilder } from '~/services/api/order-builder';
19 import { CollectionResource } from '~/models/collection';
20 import { extractUuidKind, ResourceKind } from '~/models/resource';
21
22 export const SLIDE_TIMEOUT = 500;
23
24 export const detailsPanelActions = unionize({
25     TOGGLE_DETAILS_PANEL: ofType<{}>(),
26     OPEN_DETAILS_PANEL: ofType<number>(),
27     LOAD_DETAILS_PANEL: ofType<string>()
28 });
29
30 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
31
32 export const PROJECT_PROPERTIES_FORM_NAME = 'projectPropertiesFormName';
33 export const PROJECT_PROPERTIES_DIALOG_NAME = 'projectPropertiesDialogName';
34
35 export const loadDetailsPanel = (uuid: string) =>
36     (dispatch: Dispatch, getState: () => RootState) => {
37         if (getState().detailsPanel.isOpened) {
38             switch(extractUuidKind(uuid)) {
39                 case ResourceKind.COLLECTION:
40                     dispatch<any>(refreshCollectionVersionsList(uuid));
41                     break;
42                 default:
43                     break;
44             }
45         }
46         dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
47     };
48
49 export const openDetailsPanel = (uuid: string, tabNr: number = 0) =>
50     (dispatch: Dispatch) => {
51         dispatch<any>(loadDetailsPanel(uuid));
52         dispatch(detailsPanelActions.OPEN_DETAILS_PANEL(tabNr));
53     };
54
55 export const openProjectPropertiesDialog = () =>
56     (dispatch: Dispatch) => {
57         dispatch<any>(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } }));
58     };
59
60 export const refreshCollectionVersionsList = (uuid: string) =>
61     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
62         const versions = await services.collectionService.list({
63             filters: new FilterBuilder()
64                 .addEqual('current_version_uuid', uuid)
65                 .getFilters(),
66             includeOldVersions: true,
67             order: new OrderBuilder<CollectionResource>().addDesc("version").getOrder()
68         });
69         dispatch(resourcesActions.SET_RESOURCES(versions.items.slice(1)));
70     };
71
72 export const deleteProjectProperty = (key: string, value: string) =>
73     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
74         const { detailsPanel, resources } = getState();
75         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
76         if (!project) { return; }
77
78         const properties = Object.assign({}, project.properties);
79
80         try {
81             const updatedProject = await services.projectService.update(
82                 project.uuid, {
83                     properties: deleteProperty(properties, key, value),
84                 });
85             dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
86             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
87         } catch (e) {
88             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
89         }
90     };
91
92 export const createProjectProperty = (data: TagProperty) =>
93     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
94         const { detailsPanel, resources } = getState();
95         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
96         if (!project) { return; }
97
98         dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME));
99         try {
100             const key = data.keyID || data.key;
101             const value = data.valueID || data.value;
102             const properties = Object.assign({}, project.properties);
103             const updatedProject = await services.projectService.update(
104                 project.uuid, {
105                     properties: addProperty(properties, key, value),
106                 }
107             );
108             dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
109             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
110             dispatch(stopSubmit(PROJECT_PROPERTIES_FORM_NAME));
111         } catch (e) {
112             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
113         }
114     };
115 export const toggleDetailsPanel = () => (dispatch: Dispatch, getState: () => RootState) => {
116     // because of material-ui issue resizing details panel breaks tabs.
117     // triggering window resize event fixes that.
118     setTimeout(() => {
119         window.dispatchEvent(new Event('resize'));
120     }, SLIDE_TIMEOUT);
121     dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
122     if (getState().detailsPanel.isOpened) {
123         dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));
124     }
125 };