21224: fixed random info button fail Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[arvados.git] / services / workbench2 / 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     START_TRANSITION: ofType<{}>(),
24     END_TRANSITION: ofType<{}>(),
25 });
26
27 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
28
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));
36                     break;
37                 default:
38                     break;
39             }
40         }
41         dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
42     };
43
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));
50         }
51     };
52
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)
58                 .getFilters(),
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]}`,
64             hideDuration: 2000,
65             kind: SnackbarKind.ERROR })
66         );
67     };
68
69 export const toggleDetailsPanel = (uuid: string = '') => (dispatch: Dispatch, getState: () => RootState) => {
70     const { detailsPanel, router }= getState()
71     // because of material-ui issue resizing details panel breaks tabs.
72     // triggering window resize event fixes that.
73     if(uuid !== detailsPanel.resourceUuid  && detailsPanel.isOpened){
74         dispatch<any>(loadDetailsPanel(uuid));
75     } else {
76         setTimeout(() => {
77             window.dispatchEvent(new Event('resize'));
78         }, SLIDE_TIMEOUT);
79         startDetailsPanelTransition(dispatch)
80         dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
81         if (getState().detailsPanel.isOpened) {
82             dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));
83         }
84     }
85     };
86     
87     const startDetailsPanelTransition = (dispatch) => {
88         dispatch(detailsPanelActions.START_TRANSITION())
89     setTimeout(() => {
90         dispatch(detailsPanelActions.END_TRANSITION())
91     }, SLIDE_TIMEOUT);
92 }