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>(),
23 START_TRANSITION: ofType<{}>(),
24 END_TRANSITION: ofType<{}>(),
27 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
29 export const loadDetailsPanel = (uuid: string) =>
30 (dispatch: Dispatch, getState: () => RootState) => {
31 if (getState().detailsPanel.isOpened) {
32 switch(extractUuidKind(uuid)) {
33 case ResourceKind.COLLECTION:
34 const c = getResource<CollectionResource>(uuid)(getState().resources);
35 dispatch<any>(refreshCollectionVersionsList(c!.currentVersionUuid));
41 dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
44 export const openDetailsPanel = (uuid?: string, tabNr: number = 0) =>
45 (dispatch: Dispatch) => {
46 startDetailsPanelTransition(dispatch)
47 dispatch(detailsPanelActions.OPEN_DETAILS_PANEL(tabNr));
48 if (uuid !== undefined) {
49 dispatch<any>(loadDetailsPanel(uuid));
53 export const refreshCollectionVersionsList = (uuid: string) =>
54 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55 services.collectionService.list({
56 filters: new FilterBuilder()
57 .addEqual('current_version_uuid', uuid)
59 includeOldVersions: true,
60 order: new OrderBuilder<CollectionResource>().addDesc("version").getOrder()
61 }).then(versions => dispatch(resourcesActions.SET_RESOURCES(versions.items))
62 ).catch(e => snackbarActions.OPEN_SNACKBAR({
63 message: `Couldn't retrieve versions: ${e.errors[0]}`,
65 kind: SnackbarKind.ERROR })
69 export const toggleDetailsPanel = () => (dispatch: Dispatch, getState: () => RootState) => {
70 // because of material-ui issue resizing details panel breaks tabs.
71 // triggering window resize event fixes that.
73 window.dispatchEvent(new Event('resize'));
75 startDetailsPanelTransition(dispatch)
76 dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
77 if (getState().detailsPanel.isOpened) {
78 dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));
82 const startDetailsPanelTransition = (dispatch) => {
83 dispatch(detailsPanelActions.START_TRANSITION())
85 dispatch(detailsPanelActions.END_TRANSITION())