18219: Adds property editor to groups create dialog.
[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 { 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';
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 RESOURCE_PROPERTIES_FORM_NAME = 'resourcePropertiesFormName';
29 export const RESOURCE_PROPERTIES_DIALOG_NAME = 'resourcePropertiesDialogName';
30
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));
38                     break;
39                 default:
40                     break;
41             }
42         }
43         dispatch(detailsPanelActions.LOAD_DETAILS_PANEL(uuid));
44     };
45
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));
51         }
52     };
53
54 export const openResourcePropertiesDialog = () =>
55     (dispatch: Dispatch) => {
56         dispatch<any>(dialogActions.OPEN_DIALOG({ id: RESOURCE_PROPERTIES_DIALOG_NAME, data: { } }));
57     };
58
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)
64                 .getFilters(),
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]}`,
70             hideDuration: 2000,
71             kind: SnackbarKind.ERROR })
72         );
73     };
74
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.
78     setTimeout(() => {
79         window.dispatchEvent(new Event('resize'));
80     }, SLIDE_TIMEOUT);
81     dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
82     if (getState().detailsPanel.isOpened) {
83         dispatch<any>(loadDetailsPanel(getState().detailsPanel.resourceUuid));
84     }
85 };