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 { getResource } from 'store/resources/resources';
9 import { ServiceRepository } from 'services/services';
10 import { resourcesActions } from 'store/resources/resources-actions';
11 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
12 import { FilterBuilder } from 'services/api/filter-builder';
13 import { OrderBuilder } from 'services/api/order-builder';
14 import { CollectionResource } from 'models/collection';
15 import { extractUuidKind, ResourceKind } from 'models/resource';
17 export const SLIDE_TIMEOUT = 500;
19 export const detailsPanelActions = unionize({
20 TOGGLE_DETAILS_PANEL: ofType<{}>(),
21 OPEN_DETAILS_PANEL: ofType<number>(),
22 LOAD_DETAILS_PANEL: ofType<string>()
25 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
27 export const loadDetailsPanel = (uuid: string) =>
28 (dispatch: Dispatch, getState: () => RootState) => {
29 if (getState().detailsPanel.isOpened) {
30 switch(extractUuidKind(uuid)) {
31 case ResourceKind.COLLECTION:
32 const c = getResource<CollectionResource>(uuid)(getState().resources);
33 dispatch<any>(refreshCollectionVersionsList(c!.currentVersionUuid));
39 dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
42 export const openDetailsPanel = (uuid?: string, tabNr: number = 0) =>
43 (dispatch: Dispatch) => {
44 dispatch(detailsPanelActions.OPEN_DETAILS_PANEL(tabNr));
45 if (uuid !== undefined) {
46 dispatch<any>(loadDetailsPanel(uuid));
50 export const refreshCollectionVersionsList = (uuid: string) =>
51 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
52 services.collectionService.list({
53 filters: new FilterBuilder()
54 .addEqual('current_version_uuid', uuid)
56 includeOldVersions: true,
57 order: new OrderBuilder<CollectionResource>().addDesc("version").getOrder()
58 }).then(versions => dispatch(resourcesActions.SET_RESOURCES(versions.items))
59 ).catch(e => snackbarActions.OPEN_SNACKBAR({
60 message: `Couldn't retrieve versions: ${e.errors[0]}`,
62 kind: SnackbarKind.ERROR })
66 export const toggleDetailsPanel = () => (dispatch: Dispatch, getState: () => RootState) => {
67 // because of material-ui issue resizing details panel breaks tabs.
68 // triggering window resize event fixes that.
70 window.dispatchEvent(new Event('resize'));
72 dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
73 if (getState().detailsPanel.isOpened) {
74 dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));