X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/f5e55e7a71f2fc2390d392af752c61b4d3135cb6..670d92f78e9af2390b93e5d984f5fabf59a16071:/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 ffa66b69..b708ad62 100644 --- a/src/store/details-panel/details-panel-action.ts +++ b/src/store/details-panel/details-panel-action.ts @@ -2,43 +2,75 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { unionize, ofType, UnionOf } from "unionize"; -import CommonResourceService, { Resource } from "../../common/api/common-resource-service"; -import { ResourceKind } from "../../models/kinds"; -import { Dispatch } from "redux"; -import { groupsService } from "../../services/services"; -import { serverApi } from "../../common/api/server-api"; - -const actions = unionize({ +import { unionize, ofType, UnionOf } from 'common/unionize'; +import { RootState } from 'store/store'; +import { Dispatch } from 'redux'; +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<{}>(), - 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 default actions; +export type DetailsPanelAction = UnionOf; -export type DetailsPanelAction = UnionOf; +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; + } + } + dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid)); + }; -export const loadDetails = (uuid: string, kind: ResourceKind) => +export const openDetailsPanel = (uuid?: string, tabNr: number = 0) => (dispatch: Dispatch) => { - dispatch(actions.LOAD_DETAILS({ uuid, kind })); - getService(kind) - .get(uuid) - .then(project => { - dispatch(actions.LOAD_DETAILS_SUCCESS({ item: project })); - }); + dispatch(detailsPanelActions.OPEN_DETAILS_PANEL(tabNr)); + if (uuid !== undefined) { + dispatch(loadDetailsPanel(uuid)); + } + }; + +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 }) + ); }; -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 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)); } }; - - -