X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/f0a64666816383d2641d5fa7ea22019441ac4464..c952afae1af2fb31b68be04f70bd7ae6f9d52aba:/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 68746714..b708ad62 100644 --- a/src/store/details-panel/details-panel-action.ts +++ b/src/store/details-panel/details-panel-action.ts @@ -2,87 +2,75 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { unionize, ofType, UnionOf } from '~/common/unionize'; -import { RootState } from '~/store/store'; +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 { getResource } from 'store/resources/resources'; +import { ServiceRepository } from 'services/services'; +import { resourcesActions } from 'store/resources/resources-actions'; +import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions'; +import { FilterBuilder } from 'services/api/filter-builder'; +import { OrderBuilder } from 'services/api/order-builder'; +import { CollectionResource } from 'models/collection'; +import { extractUuidKind, ResourceKind } from 'models/resource'; export const SLIDE_TIMEOUT = 500; export const detailsPanelActions = unionize({ TOGGLE_DETAILS_PANEL: ofType<{}>(), - OPEN_DETAILS_PANEL: ofType(), + OPEN_DETAILS_PANEL: ofType(), LOAD_DETAILS_PANEL: ofType() }); export type DetailsPanelAction = UnionOf; -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 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) => - 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, { properties: project.properties }); - dispatch(resourcesActions.SET_RESOURCES([updatedProject])); - dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS })); +export const loadDetailsPanel = (uuid: string) => + (dispatch: Dispatch, getState: () => RootState) => { + if (getState().detailsPanel.isOpened) { + switch(extractUuidKind(uuid)) { + case ResourceKind.COLLECTION: + const c = getResource(uuid)(getState().resources); + dispatch(refreshCollectionVersionsList(c!.currentVersionUuid)); + break; + default: + break; } - } catch (e) { - dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME })); - throw new Error('Could not remove property from the project.'); } + dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid)); }; -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 updatedProject = await services.projectService.update( - project.uuid, { - properties: { - ...JSON.parse(JSON.stringify(project.properties)), - [data.keyID || data.key]: data.valueID || data.value - } - } - ); - 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 openDetailsPanel = (uuid?: string, tabNr: number = 0) => + (dispatch: Dispatch) => { + dispatch(detailsPanelActions.OPEN_DETAILS_PANEL(tabNr)); + if (uuid !== undefined) { + dispatch(loadDetailsPanel(uuid)); } }; -export const toggleDetailsPanel = () => (dispatch: Dispatch) => { + +export const refreshCollectionVersionsList = (uuid: string) => + (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + services.collectionService.list({ + filters: new FilterBuilder() + .addEqual('current_version_uuid', uuid) + .getFilters(), + includeOldVersions: true, + order: new OrderBuilder().addDesc("version").getOrder() + }).then(versions => dispatch(resourcesActions.SET_RESOURCES(versions.items)) + ).catch(e => snackbarActions.OPEN_SNACKBAR({ + message: `Couldn't retrieve versions: ${e.errors[0]}`, + hideDuration: 2000, + kind: SnackbarKind.ERROR }) + ); + }; + +export const toggleDetailsPanel = () => (dispatch: Dispatch, getState: () => RootState) => { // 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()); + if (getState().detailsPanel.isOpened) { + dispatch(loadDetailsPanel(getState().detailsPanel.resourceUuid)); + } };