18368: initial impl
[arvados.git] / src / store / details-panel / details-panel-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
16
17 export const SLIDE_TIMEOUT = 500;
18
19 export const detailsPanelActions = unionize({
20     TOGGLE_DETAILS_PANEL: ofType<{}>(),
21     OPEN_DETAILS_PANEL: ofType<number>(),
22     LOAD_DETAILS_PANEL: ofType<string>()
23 });
24
25 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
26
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));
34                     break;
35                 default:
36                     break;
37             }
38         }
39         dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
40     };
41
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));
47         }
48     };
49
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)
55                 .getFilters(),
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]}`,
61             hideDuration: 2000,
62             kind: SnackbarKind.ERROR })
63         );
64     };
65
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.
69     setTimeout(() => {
70         window.dispatchEvent(new Event('resize'));
71     }, SLIDE_TIMEOUT);
72     dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
73     if (getState().detailsPanel.isOpened) {
74         dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));
75     }
76 };