21128: clicking row now checks box Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[arvados-workbench2.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 import { selectOne } from 'store/multiselect/multiselect-actions';
17
18 export const SLIDE_TIMEOUT = 500;
19
20 export const detailsPanelActions = unionize({
21     TOGGLE_DETAILS_PANEL: ofType<{}>(),
22     OPEN_DETAILS_PANEL: ofType<number>(),
23     LOAD_DETAILS_PANEL: ofType<string>()
24 });
25
26 export type DetailsPanelAction = UnionOf<typeof detailsPanelActions>;
27
28 export const loadDetailsPanel = (uuid: string) =>
29     (dispatch: Dispatch, getState: () => RootState) => {
30         if (getState().detailsPanel.isOpened) {
31             switch(extractUuidKind(uuid)) {
32                 case ResourceKind.COLLECTION:
33                     const c = getResource<CollectionResource>(uuid)(getState().resources);
34                     dispatch<any>(refreshCollectionVersionsList(c!.currentVersionUuid));
35                     break;
36                 default:
37                     break;
38             }
39         }
40         dispatch<any>(selectOne(uuid))
41         dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
42     };
43
44 export const openDetailsPanel = (uuid?: string, tabNr: number = 0) =>
45     (dispatch: Dispatch) => {
46         dispatch(detailsPanelActions.OPEN_DETAILS_PANEL(tabNr));
47         if (uuid !== undefined) {
48             dispatch<any>(loadDetailsPanel(uuid));
49         }
50     };
51
52 export const refreshCollectionVersionsList = (uuid: string) =>
53     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
54         services.collectionService.list({
55             filters: new FilterBuilder()
56                 .addEqual('current_version_uuid', uuid)
57                 .getFilters(),
58             includeOldVersions: true,
59             order: new OrderBuilder<CollectionResource>().addDesc("version").getOrder()
60         }).then(versions => dispatch(resourcesActions.SET_RESOURCES(versions.items))
61         ).catch(e => snackbarActions.OPEN_SNACKBAR({
62             message: `Couldn't retrieve versions: ${e.errors[0]}`,
63             hideDuration: 2000,
64             kind: SnackbarKind.ERROR })
65         );
66     };
67
68 export const toggleDetailsPanel = () => (dispatch: Dispatch, getState: () => RootState) => {
69     // because of material-ui issue resizing details panel breaks tabs.
70     // triggering window resize event fixes that.
71     setTimeout(() => {
72         window.dispatchEvent(new Event('resize'));
73     }, SLIDE_TIMEOUT);
74     dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
75     if (getState().detailsPanel.isOpened) {
76         dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));
77     }
78 };