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