add admin links feature - model, service, dialogs and panel
[arvados-workbench2.git] / src / store / link-panel / link-panel-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from 'redux';
6 import { RootState } from '~/store/store';
7 import { ServiceRepository } from '~/services/services';
8 import { bindDataExplorerActions } from '~/store/data-explorer/data-explorer-action';
9 import { setBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
10 import { dialogActions } from '~/store/dialog/dialog-actions';
11 import { LinkResource } from '~/models/link';
12 import { getResource } from '~/store/resources/resources';
13 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
14
15 export const LINK_PANEL_ID = "linkPanelId";
16 export const linkPanelActions = bindDataExplorerActions(LINK_PANEL_ID);
17
18 export const LINK_REMOVE_DIALOG = 'linkRemoveDialog';
19 export const LINK_ATTRIBUTES_DIALOG = 'linkAttributesDialog';
20
21 export const openLinkAttributesDialog = (uuid: string) =>
22     (dispatch: Dispatch, getState: () => RootState) => {
23         const { resources } = getState();
24         const link = getResource<LinkResource>(uuid)(resources);
25         dispatch(dialogActions.OPEN_DIALOG({ id: LINK_ATTRIBUTES_DIALOG, data: { link } }));
26     };
27
28 export const openLinkRemoveDialog = (uuid: string) =>
29     (dispatch: Dispatch, getState: () => RootState) => {
30         dispatch(dialogActions.OPEN_DIALOG({
31             id: LINK_REMOVE_DIALOG,
32             data: {
33                 title: 'Remove link',
34                 text: 'Are you sure you want to remove this link?',
35                 confirmButtonLabel: 'Remove',
36                 uuid
37             }
38         }));
39     };
40
41 export const loadLinkPanel = () =>
42     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43         dispatch(setBreadcrumbs([{ label: 'Links' }]));
44         dispatch(linkPanelActions.REQUEST_ITEMS());
45     };
46
47 export const removeLink = (uuid: string) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
50         try {
51             await services.linkService.delete(uuid);
52             dispatch(linkPanelActions.REQUEST_ITEMS());
53             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Link has been successfully removed.', hideDuration: 2000 }));
54         } catch (e) {
55             return;
56         }
57     };