merge master
[arvados-workbench2.git] / src / store / context-menu / context-menu-actions.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 { ContextMenuPosition } from "./context-menu-reducer";
7 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
8 import { Dispatch } from 'redux';
9 import { RootState } from '~/store/store';
10 import { getResource } from '../resources/resources';
11 import { ProjectResource } from '~/models/project';
12 import { UserResource } from '~/models/user';
13 import { isSidePanelTreeCategory } from '~/store/side-panel-tree/side-panel-tree-actions';
14 import { extractUuidKind, ResourceKind, TrashableResource } from '~/models/resource';
15
16 export const contextMenuActions = unionize({
17     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
18     CLOSE_CONTEXT_MENU: ofType<{}>()
19 });
20
21 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
22
23 export type ContextMenuResource = {
24     name: string;
25     uuid: string;
26     ownerUuid: string;
27     description?: string;
28     kind: ResourceKind,
29     menuKind: ContextMenuKind;
30     isTrashed?: boolean;
31 };
32
33 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
34     (dispatch: Dispatch) => {
35         event.preventDefault();
36         dispatch(
37             contextMenuActions.OPEN_CONTEXT_MENU({
38                 position: { x: event.clientX, y: event.clientY },
39                 resource
40             })
41         );
42     };
43
44 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
45     (dispatch: Dispatch, getState: () => RootState) => {
46         const res = getResource<UserResource>(projectUuid)(getState().resources);
47         if (res) {
48             dispatch<any>(openContextMenu(event, {
49                 name: '',
50                 uuid: res.uuid,
51                 ownerUuid: res.uuid,
52                 kind: res.kind,
53                 menuKind: ContextMenuKind.ROOT_PROJECT,
54                 isTrashed: false
55             }));
56         }
57     };
58
59 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
60     (dispatch: Dispatch, getState: () => RootState) => {
61         const res = getResource<ProjectResource>(projectUuid)(getState().resources);
62         if (res) {
63             dispatch<any>(openContextMenu(event, {
64                 name: res.name,
65                 uuid: res.uuid,
66                 kind: res.kind,
67                 menuKind: ContextMenuKind.PROJECT,
68                 ownerUuid: res.ownerUuid,
69                 isTrashed: res.isTrashed
70             }));
71         }
72     };
73
74 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
75     (dispatch: Dispatch, getState: () => RootState) => {
76         if (!isSidePanelTreeCategory(id)) {
77             const kind = extractUuidKind(id);
78             if (kind === ResourceKind.USER) {
79                 dispatch<any>(openRootProjectContextMenu(event, id));
80             } else if (kind === ResourceKind.PROJECT) {
81                 dispatch<any>(openProjectContextMenu(event, id));
82             }
83         }
84     };
85
86 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>) =>
87     (dispatch: Dispatch, getState: () => RootState) => {
88         const { location } = getState().router;
89         const pathname = location ? location.pathname : '';
90         // ToDo: We get error from matchProcessRoute
91         // const match = matchProcessRoute(pathname); 
92         // console.log('match: ', match);
93         // const uuid = match ? match.params.id : '';
94         const uuid = pathname.split('/').slice(-1)[0];
95         const resource = {
96             uuid: '',
97             ownerUuid: '',
98             kind: ResourceKind.PROCESS,
99             name: '',
100             description: '',
101             menuKind: ContextMenuKind.PROCESS
102         };
103         dispatch<any>(openContextMenu(event, resource));
104     };
105
106 export const resourceKindToContextMenuKind = (uuid: string) => {
107     const kind = extractUuidKind(uuid);
108     switch (kind) {
109         case ResourceKind.PROJECT:
110             return ContextMenuKind.PROJECT;
111         case ResourceKind.COLLECTION:
112             return ContextMenuKind.COLLECTION_RESOURCE;
113         case ResourceKind.USER:
114             return ContextMenuKind.ROOT_PROJECT;
115         default:
116             return;
117     }
118 };