21700: Install Bundler system-wide in Rails postinst
[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 = () => (dispatch: Dispatch, getState: () => RootState) => {
70     // because of material-ui issue resizing details panel breaks tabs.
71     // triggering window resize event fixes that.
72     setTimeout(() => {
73         window.dispatchEvent(new Event('resize'));
74     }, SLIDE_TIMEOUT);
75     startDetailsPanelTransition(dispatch)
76     dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
77     if (getState().detailsPanel.isOpened) {
78         dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));
79     }
80 };
81
82 const startDetailsPanelTransition = (dispatch) => {
83         dispatch(detailsPanelActions.START_TRANSITION())
84     setTimeout(() => {
85         dispatch(detailsPanelActions.END_TRANSITION())
86     }, SLIDE_TIMEOUT);
87 }