17782: Fixes absolute import paths from '~/somedir/...' to 'somedir/...'
[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                     const c = getResource<CollectionResource>(uuid)(getState().resources);
41                     dispatch<any>(refreshCollectionVersionsList(c!.currentVersionUuid));
42                     break;
43                 default:
44                     break;
45             }
46         }
47         dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
48     };
49
50 export const openDetailsPanel = (uuid?: string, tabNr: number = 0) =>
51     (dispatch: Dispatch) => {
52         dispatch(detailsPanelActions.OPEN_DETAILS_PANEL(tabNr));
53         if (uuid !== undefined) {
54             dispatch<any>(loadDetailsPanel(uuid));
55         }
56     };
57
58 export const openProjectPropertiesDialog = () =>
59     (dispatch: Dispatch) => {
60         dispatch<any>(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } }));
61     };
62
63 export const refreshCollectionVersionsList = (uuid: string) =>
64     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65         services.collectionService.list({
66             filters: new FilterBuilder()
67                 .addEqual('current_version_uuid', uuid)
68                 .getFilters(),
69             includeOldVersions: true,
70             order: new OrderBuilder<CollectionResource>().addDesc("version").getOrder()
71         }).then(versions => dispatch(resourcesActions.SET_RESOURCES(versions.items))
72         ).catch(e => snackbarActions.OPEN_SNACKBAR({
73             message: `Couldn't retrieve versions: ${e.errors[0]}`,
74             hideDuration: 2000,
75             kind: SnackbarKind.ERROR })
76         );
77     };
78
79 export const deleteProjectProperty = (key: string, value: string) =>
80     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
81         const { detailsPanel, resources } = getState();
82         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
83         if (!project) { return; }
84
85         const properties = Object.assign({}, project.properties);
86
87         try {
88             const updatedProject = await services.projectService.update(
89                 project.uuid, {
90                     properties: deleteProperty(properties, key, value),
91                 });
92             dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
93             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
94         } catch (e) {
95             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
96         }
97     };
98
99 export const createProjectProperty = (data: TagProperty) =>
100     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
101         const { detailsPanel, resources } = getState();
102         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
103         if (!project) { return; }
104
105         dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME));
106         try {
107             const key = data.keyID || data.key;
108             const value = data.valueID || data.value;
109             const properties = Object.assign({}, project.properties);
110             const updatedProject = await services.projectService.update(
111                 project.uuid, {
112                     properties: addProperty(properties, key, value),
113                 }
114             );
115             dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
116             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
117             dispatch(stopSubmit(PROJECT_PROPERTIES_FORM_NAME));
118         } catch (e) {
119             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
120         }
121     };
122 export const toggleDetailsPanel = () => (dispatch: Dispatch, getState: () => RootState) => {
123     // because of material-ui issue resizing details panel breaks tabs.
124     // triggering window resize event fixes that.
125     setTimeout(() => {
126         window.dispatchEvent(new Event('resize'));
127     }, SLIDE_TIMEOUT);
128     dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
129     if (getState().detailsPanel.isOpened) {
130         dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));
131     }
132 };