X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/18a8117437056f65e3c9d84b0231c5f64b1346d1..cc493b89840b48f40c2beaf626994724331aa196:/src/store/context-menu/context-menu-actions.ts diff --git a/src/store/context-menu/context-menu-actions.ts b/src/store/context-menu/context-menu-actions.ts index 89d65244..40623999 100644 --- a/src/store/context-menu/context-menu-actions.ts +++ b/src/store/context-menu/context-menu-actions.ts @@ -2,14 +2,81 @@ // // SPDX-License-Identifier: AGPL-3.0 -// import { default as unionize, ofType, UnionOf } from "unionize"; +import { unionize, ofType, UnionOf } from '~/common/unionize'; +import { ContextMenuPosition, ContextMenuResource } 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'; -// const actions = unionize({ -// OPEN_CONTEXT_MENU: ofType<{position: {x: number, y: number}}>() -// }, { -// tag: 'type', -// value: 'payload' -// }); +export const contextMenuActions = unionize({ + OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(), + CLOSE_CONTEXT_MENU: ofType<{}>() +}); -// export type ContextMenuAction = UnionOf; -// export default actions; \ No newline at end of file +export type ContextMenuAction = UnionOf; + +export const openContextMenu = (event: React.MouseEvent, resource: { name: string; uuid: string; description?: string; kind: ContextMenuKind; }) => + (dispatch: Dispatch) => { + event.preventDefault(); + dispatch( + contextMenuActions.OPEN_CONTEXT_MENU({ + position: { x: event.clientX, y: event.clientY }, + resource + }) + ); + }; + +export const openRootProjectContextMenu = (event: React.MouseEvent, projectUuid: string) => + (dispatch: Dispatch, getState: () => RootState) => { + const userResource = getResource(projectUuid)(getState().resources); + if (userResource) { + dispatch(openContextMenu(event, { + name: '', + uuid: userResource.uuid, + kind: ContextMenuKind.ROOT_PROJECT + })); + } + }; + +export const openProjectContextMenu = (event: React.MouseEvent, projectUuid: string) => + (dispatch: Dispatch, getState: () => RootState) => { + const projectResource = getResource(projectUuid)(getState().resources); + if (projectResource) { + dispatch(openContextMenu(event, { + name: projectResource.name, + uuid: projectResource.uuid, + kind: ContextMenuKind.PROJECT + })); + } + }; + +export const openSidePanelContextMenu = (event: React.MouseEvent, id: string) => + (dispatch: Dispatch, getState: () => RootState) => { + if (!isSidePanelTreeCategory(id)) { + const kind = extractUuidKind(id); + if (kind === ResourceKind.USER) { + dispatch(openRootProjectContextMenu(event, id)); + } else if (kind === ResourceKind.PROJECT) { + dispatch(openProjectContextMenu(event, id)); + } + } + }; + +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.USER: + return ContextMenuKind.ROOT_PROJECT; + default: + return; + } +};