X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/b6a5b173cd4e9f325f371d26204dfe156d911c20..dd0e60751f2551d78344283105e8c91adb9c3e19:/src/store/details-panel/details-panel-action.ts diff --git a/src/store/details-panel/details-panel-action.ts b/src/store/details-panel/details-panel-action.ts index ba330f2e..e0d72017 100644 --- a/src/store/details-panel/details-panel-action.ts +++ b/src/store/details-panel/details-panel-action.ts @@ -2,40 +2,88 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { unionize, ofType, UnionOf } from "unionize"; -import { CommonResourceService } from "../../common/api/common-resource-service"; -import { Dispatch } from "redux"; -import { serverApi } from "../../common/api/server-api"; -import { Resource, ResourceKind } from "../../models/resource"; +import { unionize, ofType, UnionOf } from '~/common/unionize'; +import { RootState } from '~/store/store'; +import { Dispatch } from 'redux'; +import { dialogActions } from '~/store/dialog/dialog-actions'; +import { getResource } from '~/store/resources/resources'; +import { ProjectResource } from "~/models/project"; +import { ServiceRepository } from '~/services/services'; +import { TagProperty } from '~/models/tag'; +import { startSubmit, stopSubmit } from 'redux-form'; +import { resourcesActions } from '~/store/resources/resources-actions'; +import {snackbarActions, SnackbarKind} from '~/store/snackbar/snackbar-actions'; +import { addProperty, deleteProperty } from '~/lib/resource-properties'; + +export const SLIDE_TIMEOUT = 500; export const detailsPanelActions = unionize({ TOGGLE_DETAILS_PANEL: ofType<{}>(), - LOAD_DETAILS: ofType<{ uuid: string, kind: ResourceKind }>(), - LOAD_DETAILS_SUCCESS: ofType<{ item: Resource }>(), -}, { tag: 'type', value: 'payload' }); + OPEN_DETAILS_PANEL: ofType(), + LOAD_DETAILS_PANEL: ofType() +}); export type DetailsPanelAction = UnionOf; -export const loadDetails = (uuid: string, kind: ResourceKind) => - (dispatch: Dispatch) => { - dispatch(detailsPanelActions.LOAD_DETAILS({ uuid, kind })); - getService(kind) - .get(uuid) - .then(project => { - dispatch(detailsPanelActions.LOAD_DETAILS_SUCCESS({ item: project })); - }); - }; +export const PROJECT_PROPERTIES_FORM_NAME = 'projectPropertiesFormName'; +export const PROJECT_PROPERTIES_DIALOG_NAME = 'projectPropertiesDialogName'; -const getService = (kind: ResourceKind) => { - switch (kind) { - case ResourceKind.Project: - return new CommonResourceService(serverApi, "groups"); - case ResourceKind.Collection: - return new CommonResourceService(serverApi, "collections"); - default: - return new CommonResourceService(serverApi, ""); - } -}; +export const loadDetailsPanel = (uuid: string) => detailsPanelActions.LOAD_DETAILS_PANEL(uuid); + +export const openDetailsPanel = (uuid: string) => detailsPanelActions.OPEN_DETAILS_PANEL(uuid); +export const openProjectPropertiesDialog = () => + (dispatch: Dispatch) => { + dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } })); + }; +export const deleteProjectProperty = (key: string, value: string) => + async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + const { detailsPanel, resources } = getState(); + const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource; + try { + if (project) { + project.properties = deleteProperty(project.properties, key, value); + const updatedProject = await services.projectService.update(project.uuid, { properties: project.properties }); + dispatch(resourcesActions.SET_RESOURCES([updatedProject])); + dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS })); + } + } catch (e) { + dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME })); + throw new Error('Could not remove property from the project.'); + } + }; +export const createProjectProperty = (data: TagProperty) => + async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + const { detailsPanel, resources } = getState(); + const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource; + dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME)); + try { + if (project) { + const key = data.keyID || data.key; + const value = data.valueID || data.value; + project.properties = addProperty(project.properties, key, value); + const updatedProject = await services.projectService.update( + project.uuid, { + properties: {...project.properties} + } + ); + dispatch(resourcesActions.SET_RESOURCES([updatedProject])); + dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS })); + dispatch(stopSubmit(PROJECT_PROPERTIES_FORM_NAME)); + } + return; + } catch (e) { + dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME })); + throw new Error('Could not add property to the project.'); + } + }; +export const toggleDetailsPanel = () => (dispatch: Dispatch) => { + // because of material-ui issue resizing details panel breaks tabs. + // triggering window resize event fixes that. + setTimeout(() => { + window.dispatchEvent(new Event('resize')); + }, SLIDE_TIMEOUT); + dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL()); +};