Merge branch 'master' into 16848-token-handling-improvements
[arvados.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, SnackbarKind} 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 loadLinkPanel = () =>
22     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
23         dispatch(setBreadcrumbs([{ label: 'Links' }]));
24         dispatch(linkPanelActions.REQUEST_ITEMS());
25     };
26
27 export const openLinkAttributesDialog = (uuid: string) =>
28     (dispatch: Dispatch, getState: () => RootState) => {
29         const { resources } = getState();
30         const link = getResource<LinkResource>(uuid)(resources);
31         dispatch(dialogActions.OPEN_DIALOG({ id: LINK_ATTRIBUTES_DIALOG, data: { link } }));
32     };
33
34 export const openLinkRemoveDialog = (uuid: string) =>
35     (dispatch: Dispatch, getState: () => RootState) => {
36         dispatch(dialogActions.OPEN_DIALOG({
37             id: LINK_REMOVE_DIALOG,
38             data: {
39                 title: 'Remove link',
40                 text: 'Are you sure you want to remove this link?',
41                 confirmButtonLabel: 'Remove',
42                 uuid
43             }
44         }));
45     };
46
47 export const removeLink = (uuid: string) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
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, kind: SnackbarKind.SUCCESS }));
54         } catch (e) {
55             return;
56         }
57     };