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 { dialogActions } from 'store/dialog/dialog-actions';
9 import { getResource } from 'store/resources/resources';
10 import { ServiceRepository } from 'services/services';
11 import { resourcesActions } from 'store/resources/resources-actions';
12 import {snackbarActions, SnackbarKind} from 'store/snackbar/snackbar-actions';
13 import { FilterBuilder } from 'services/api/filter-builder';
14 import { OrderBuilder } from 'services/api/order-builder';
15 import { CollectionResource } from 'models/collection';
16 import { extractUuidKind, ResourceKind } from 'models/resource';
18 export const SLIDE_TIMEOUT = 500;
20 export const detailsPanelActions = unionize({
21 TOGGLE_DETAILS_PANEL: ofType<{}>(),
22 OPEN_DETAILS_PANEL: ofType<number>(),
23 LOAD_DETAILS_PANEL: ofType<string>()
26 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
28 export const RESOURCE_PROPERTIES_FORM_NAME = 'resourcePropertiesFormName';
29 export const RESOURCE_PROPERTIES_DIALOG_NAME = 'resourcePropertiesDialogName';
31 export const loadDetailsPanel = (uuid: string) =>
32 (dispatch: Dispatch, getState: () => RootState) => {
33 if (getState().detailsPanel.isOpened) {
34 switch(extractUuidKind(uuid)) {
35 case ResourceKind.COLLECTION:
36 const c = getResource<CollectionResource>(uuid)(getState().resources);
37 dispatch<any>(refreshCollectionVersionsList(c!.currentVersionUuid));
43 dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
46 export const openDetailsPanel = (uuid?: string, tabNr: number = 0) =>
47 (dispatch: Dispatch) => {
48 dispatch(detailsPanelActions.OPEN_DETAILS_PANEL(tabNr));
49 if (uuid !== undefined) {
50 dispatch<any>(loadDetailsPanel(uuid));
54 export const openResourcePropertiesDialog = () =>
55 (dispatch: Dispatch) => {
56 dispatch<any>(dialogActions.OPEN_DIALOG({ id: RESOURCE_PROPERTIES_DIALOG_NAME, data: { } }));
59 export const refreshCollectionVersionsList = (uuid: string) =>
60 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
61 services.collectionService.list({
62 filters: new FilterBuilder()
63 .addEqual('current_version_uuid', uuid)
65 includeOldVersions: true,
66 order: new OrderBuilder<CollectionResource>().addDesc("version").getOrder()
67 }).then(versions => dispatch(resourcesActions.SET_RESOURCES(versions.items))
68 ).catch(e => snackbarActions.OPEN_SNACKBAR({
69 message: `Couldn't retrieve versions: ${e.errors[0]}`,
71 kind: SnackbarKind.ERROR })
75 export const toggleDetailsPanel = () => (dispatch: Dispatch, getState: () => RootState) => {
76 // because of material-ui issue resizing details panel breaks tabs.
77 // triggering window resize event fixes that.
79 window.dispatchEvent(new Event('resize'));
81 dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
82 if (getState().detailsPanel.isOpened) {
83 dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));