14096-move-to-process
[arvados-workbench2.git] / src / store / context-menu / context-menu-actions.ts
index 8630f9c757faef3baacc89d754fb3e10a9ccbc22..eabf41ae715ad715e6072ca46383c0499461d170 100644 (file)
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { default as unionize, ofType, UnionOf } from "unionize";
-import { ContextMenuPosition, ContextMenuResource } from "./context-menu-reducer";
+import { unionize, ofType, UnionOf } from '~/common/unionize';
+import { ContextMenuPosition } from "./context-menu-reducer";
+import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
+import { Dispatch } from 'redux';
+import { RootState } from '~/store/store';
+import { getResource } from '../resources/resources';
+import { ProjectResource } from '~/models/project';
+import { UserResource } from '~/models/user';
+import { isSidePanelTreeCategory } from '~/store/side-panel-tree/side-panel-tree-actions';
+import { extractUuidKind, ResourceKind } from '~/models/resource';
+import { matchProcessRoute } from '~/routes/routes';
 
-const actions = unionize({
+export const contextMenuActions = unionize({
     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
     CLOSE_CONTEXT_MENU: ofType<{}>()
-}, {
-        tag: 'type',
-        value: 'payload'
-    });
+});
 
-export type ContextMenuAction = UnionOf<typeof actions>;
-export default actions;
\ No newline at end of file
+export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
+
+export type ContextMenuResource = {
+    name: string;
+    uuid: string;
+    ownerUuid: string;
+    description?: string;
+    kind: ResourceKind,
+    menuKind: ContextMenuKind;
+    isTrashed?: boolean;
+};
+
+export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
+    (dispatch: Dispatch) => {
+        event.preventDefault();
+        dispatch(
+            contextMenuActions.OPEN_CONTEXT_MENU({
+                position: { x: event.clientX, y: event.clientY },
+                resource
+            })
+        );
+    };
+
+export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
+    (dispatch: Dispatch, getState: () => RootState) => {
+        const res = getResource<UserResource>(projectUuid)(getState().resources);
+        if (res) {
+            dispatch<any>(openContextMenu(event, {
+                name: '',
+                uuid: res.uuid,
+                ownerUuid: res.uuid,
+                kind: res.kind,
+                menuKind: ContextMenuKind.ROOT_PROJECT,
+                isTrashed: false
+            }));
+        }
+    };
+
+export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
+    (dispatch: Dispatch, getState: () => RootState) => {
+        const res = getResource<ProjectResource>(projectUuid)(getState().resources);
+        if (res) {
+            dispatch<any>(openContextMenu(event, {
+                name: res.name,
+                uuid: res.uuid,
+                kind: res.kind,
+                menuKind: ContextMenuKind.PROJECT,
+                ownerUuid: res.ownerUuid,
+                isTrashed: res.isTrashed
+            }));
+        }
+    };
+
+export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
+    (dispatch: Dispatch, getState: () => RootState) => {
+        if (!isSidePanelTreeCategory(id)) {
+            const kind = extractUuidKind(id);
+            if (kind === ResourceKind.USER) {
+                dispatch<any>(openRootProjectContextMenu(event, id));
+            } else if (kind === ResourceKind.PROJECT) {
+                dispatch<any>(openProjectContextMenu(event, id));
+            }
+        }
+    };
+
+export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>) =>
+    (dispatch: Dispatch, getState: () => RootState) => {
+        const { location } = getState().router;
+        const pathname = location ? location.pathname : '';
+        const match = matchProcessRoute(pathname);
+        const uuid = match ? match.params.id : '';
+        const resource = {
+            uuid,
+            ownerUuid: '',
+            kind: ResourceKind.PROCESS,
+            name: '',
+            description: '',
+            menuKind: ContextMenuKind.PROCESS
+        };
+        dispatch<any>(openContextMenu(event, resource));
+    };
+
+export const resourceKindToContextMenuKind = (uuid: string) => {
+    const kind = extractUuidKind(uuid);
+    switch (kind) {
+        case ResourceKind.PROJECT:
+            return ContextMenuKind.PROJECT;
+        case ResourceKind.COLLECTION:
+            return ContextMenuKind.COLLECTION_RESOURCE;
+        case ResourceKind.PROCESS:
+            return ContextMenuKind.PROCESS_RESOURCE;
+        case ResourceKind.USER:
+            return ContextMenuKind.ROOT_PROJECT;
+        default:
+            return;
+    }
+};