1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
18 export const SLIDE_TIMEOUT = 500;
20 export const detailsPanelActions = unionize({
21 TOGGLE_DETAILS_PANEL: ofType<{}>(),
22 OPEN_DETAILS_PANEL: ofType<string>(),
23 LOAD_DETAILS_PANEL: ofType<string>()
26 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
28 export const PROJECT_PROPERTIES_FORM_NAME = 'projectPropertiesFormName';
29 export const PROJECT_PROPERTIES_DIALOG_NAME = 'projectPropertiesDialogName';
31 export const loadDetailsPanel = (uuid: string) => detailsPanelActions.LOAD_DETAILS_PANEL(uuid);
33 export const openDetailsPanel = (uuid: string) => detailsPanelActions.OPEN_DETAILS_PANEL(uuid);
35 export const openProjectPropertiesDialog = () =>
36 (dispatch: Dispatch) => {
37 dispatch<any>(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } }));
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;
46 project.properties = deleteProperty(project.properties, key, value);
47 const updatedProject = await services.projectService.update(project.uuid, { properties: project.properties });
48 dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
49 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
52 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME }));
53 throw new Error('Could not remove property from the project.');
57 export const createProjectProperty = (data: TagProperty) =>
58 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
59 const { detailsPanel, resources } = getState();
60 const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
61 dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME));
64 const key = data.keyID || data.key;
65 const value = data.valueID || data.value;
66 project.properties = addProperty(project.properties, key, value);
67 const updatedProject = await services.projectService.update(
69 properties: {...project.properties}
72 dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
73 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
74 dispatch(stopSubmit(PROJECT_PROPERTIES_FORM_NAME));
78 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME }));
79 throw new Error('Could not add property to the project.');
82 export const toggleDetailsPanel = () => (dispatch: Dispatch) => {
83 // because of material-ui issue resizing details panel breaks tabs.
84 // triggering window resize event fixes that.
86 window.dispatchEvent(new Event('resize'));
88 dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());