X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/a71b18d036e642ace1ae4bdd06f7df8409faf1b4..0a0db4ca8433ae210df5cf1475dd2b77e4aabec9:/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 b8021fb6..6aebc050 100644 --- a/src/store/details-panel/details-panel-action.ts +++ b/src/store/details-panel/details-panel-action.ts @@ -2,48 +2,68 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { unionize, ofType, UnionOf } from "unionize"; -import { Dispatch } from "redux"; -import { Resource, ResourceKind } from "~/models/resource"; -import { RootState } from "../store"; -import { ServiceRepository } from "~/services/services"; +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 } from '~/store/snackbar/snackbar-actions'; export const detailsPanelActions = unionize({ TOGGLE_DETAILS_PANEL: ofType<{}>(), - LOAD_DETAILS: ofType<{ uuid: string, kind: ResourceKind }>(), - LOAD_DETAILS_SUCCESS: ofType<{ item: Resource }>(), - UPDATE_DETAILS: ofType<{ item: Resource }>() -}, { tag: 'type', value: 'payload' }); + LOAD_DETAILS_PANEL: ofType() +}); export type DetailsPanelAction = UnionOf; -export const loadDetails = (uuid: string, kind: ResourceKind) => - async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { - dispatch(detailsPanelActions.LOAD_DETAILS({ uuid, kind })); - const item = await getService(services, kind).get(uuid); - dispatch(detailsPanelActions.LOAD_DETAILS_SUCCESS({ item })); +export const PROJECT_PROPERTIES_FORM_NAME = 'projectPropertiesFormName'; +export const PROJECT_PROPERTIES_DIALOG_NAME = 'projectPropertiesDialogName'; + +export const loadDetailsPanel = (uuid: string) => detailsPanelActions.LOAD_DETAILS_PANEL(uuid); + +export const openProjectPropertiesDialog = () => + (dispatch: Dispatch) => { + dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } })); }; -export const updateDetails = (item: Resource) => - async (dispatch: Dispatch, getState: () => RootState) => { - const currentItem = getState().detailsPanel.item; - if (currentItem && (currentItem.uuid === item.uuid)) { - dispatch(detailsPanelActions.UPDATE_DETAILS({ item })); - dispatch(detailsPanelActions.LOAD_DETAILS_SUCCESS({ item })); +export const deleteProjectProperty = (key: string) => + async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + const { detailsPanel, resources } = getState(); + const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource; + try { + if (project) { + delete project.properties[key]; + const updatedProject = await services.projectService.update(project.uuid, project); + dispatch(resourcesActions.SET_RESOURCES([updatedProject])); + dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000 })); + } + } catch (e) { + dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME })); + throw new Error('Could not remove property from the project.'); } }; - -const getService = (services: ServiceRepository, kind: ResourceKind) => { - switch (kind) { - case ResourceKind.PROJECT: - return services.projectService; - case ResourceKind.COLLECTION: - return services.collectionService; - default: - return services.projectService; - } -}; - - - +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) { + project.properties[data.key] = data.value; + const updatedProject = await services.projectService.update(project.uuid, project); + dispatch(resourcesActions.SET_RESOURCES([updatedProject])); + dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000 })); + 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.'); + } + }; \ No newline at end of file