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';
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';
22 export const SLIDE_TIMEOUT = 500;
24 export const detailsPanelActions = unionize({
25 TOGGLE_DETAILS_PANEL: ofType<{}>(),
26 OPEN_DETAILS_PANEL: ofType<number>(),
27 LOAD_DETAILS_PANEL: ofType<string>()
30 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
32 export const PROJECT_PROPERTIES_FORM_NAME = 'projectPropertiesFormName';
33 export const PROJECT_PROPERTIES_DIALOG_NAME = 'projectPropertiesDialogName';
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));
47 dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
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));
58 export const openProjectPropertiesDialog = () =>
59 (dispatch: Dispatch) => {
60 dispatch<any>(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } }));
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)
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]}`,
75 kind: SnackbarKind.ERROR })
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; }
85 const properties = Object.assign({}, project.properties);
88 const updatedProject = await services.projectService.update(
90 properties: deleteProperty(properties, key, value),
92 dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
93 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
95 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
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; }
105 dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME));
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(
112 properties: addProperty(properties, key, value),
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));
119 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
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.
126 window.dispatchEvent(new Event('resize'));
128 dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
129 if (getState().detailsPanel.isOpened) {
130 dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));