Skip to content

Commit

Permalink
delete nodes works as well!
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Sep 5, 2024
1 parent 20bebfe commit 7c9e193
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 42 deletions.
52 changes: 32 additions & 20 deletions ui2/src/components/Commander/DeleteButton.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
import {useContext} from "react"
import {useDisclosure} from "@mantine/hooks"

import {Tooltip, ActionIcon} from "@mantine/core"
import {IconTrash} from "@tabler/icons-react"
import {openModal} from "@/components/modals/Generic"
import {NodeType, PanelMode} from "@/types"
import {PanelMode} from "@/types"
import DeleteNodesModal from "./DeleteModal"
import {useSelector, useDispatch} from "react-redux"
import {
selectSelectedNodes,
selectSelectedNodeIds,
clearNodesSelection
} from "@/slices/dualPanel/dualPanel"
import {RootState} from "@/app/types"

import {selectNodesByIds} from "@/features/nodes/nodesSlice"
import PanelContext from "@/contexts/PanelContext"

export default function DeleteButton() {
const [opened, {open, close}] = useDisclosure(false)
const mode: PanelMode = useContext(PanelContext)
const dispatch = useDispatch()
const nodes = useSelector((state: RootState) =>
selectSelectedNodes(state, mode)
) as NodeType[]
const selectedIds = useSelector((state: RootState) =>
selectSelectedNodeIds(state, mode)
)
const selectedNodes = useSelector((state: RootState) =>
selectNodesByIds(state, selectedIds || [])
)

const onSubmit = () => {
dispatch(clearNodesSelection(mode))
close()
}

const onClick = () => {
openModal<string[], {nodes: NodeType[]}>(DeleteNodesModal, {
nodes: nodes
})
.then(() => {
dispatch(clearNodesSelection(mode))
})
.catch(() => {})
const onCancel = () => {
dispatch(clearNodesSelection(mode))
close()
}

return (
<Tooltip withArrow label="Delete">
<ActionIcon size="lg" onClick={onClick} color={"red"}>
<IconTrash />
</ActionIcon>
</Tooltip>
<>
<Tooltip withArrow label="Delete">
<ActionIcon size="lg" onClick={open} color={"red"}>
<IconTrash />
</ActionIcon>
</Tooltip>
<DeleteNodesModal
opened={opened}
nodes={selectedNodes}
onSubmit={onSubmit}
onCancel={onCancel}
/>
</>
)
}
55 changes: 34 additions & 21 deletions ui2/src/components/Commander/DeleteModal.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
import {useState} from "react"
import {store} from "@/app/store"
import {deleteNodes} from "@/slices/dualPanel/dualPanel"
import GenericModal from "@/components/modals/Generic"

import {Button, Modal, Container, Group, Space, Loader} from "@mantine/core"
import {useDeleteNodesMutation} from "@/features/nodes/apiSlice"

import type {NodeType} from "@/types"

type DeleteNodesModalArgs = {
nodes: NodeType[]
onOK: (value: NodeType[]) => void
onCancel: (reason?: any) => void
opened: boolean
onSubmit: () => void
onCancel: () => void
}

/* Deletes multiple nodes */
export default function DeleteNodesModal({
nodes,
onOK,
opened,
onSubmit,
onCancel
}: DeleteNodesModalArgs) {
const [deletedNodes, {isLoading}] = useDeleteNodesMutation()
const [errorMessage, setErrorMessage] = useState("")
const nodeTitles = nodes.map(g => g.title).join(",")

const handleSubmit = async () => {
store.dispatch(deleteNodes(nodes.map(g => g.id)))
onOK(nodes)
return true
const localSubmit = async () => {
await deletedNodes(nodes.map(n => n.id))
onSubmit()
}
const handleCancel = () => {
const localCancel = () => {
setErrorMessage("")

onCancel()
}

return (
<GenericModal
modal_title={"Delete Nodes"}
onSubmit={handleSubmit}
onCancel={handleCancel}
submit_button_title="Delete"
submit_button_color="red"
>
<p>Are you sure you want to delete following nodes: {nodeTitles}?</p>
{errorMessage}
</GenericModal>
<Modal title="Delete Nodes" opened={opened} onClose={localCancel}>
<Container>
<p>Are you sure you want to delete following nodes: {nodeTitles}?</p>
{errorMessage}
<Space h="md" />
<Group gap="lg" justify="space-between">
<Button variant="default" onClick={localCancel}>
Cancel
</Button>
<Button
leftSection={isLoading && <Loader size={"sm"} />}
onClick={localSubmit}
disabled={isLoading}
color={"red"}
>
Delete
</Button>
</Group>
</Container>
</Modal>
)
}
14 changes: 13 additions & 1 deletion ui2/src/features/nodes/apiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ export const apiSliceWithNodes = apiSlice.injectEndpoints({
body: node.tags
}),
invalidatesTags: ["Node", "Tag"]
}),
deleteNodes: builder.mutation<void, string[]>({
query: nodeIDs => ({
url: `nodes/`,
method: "DELETE",
body: nodeIDs
}),
invalidatesTags: (_result, _error, ids) =>
ids.map(id => {
return {type: "Node", id: id}
})
})
})
})
Expand All @@ -81,5 +92,6 @@ export const {
useGetFolderQuery,
useAddNewFolderMutation,
useRenameFolderMutation,
useUpdateNodeTagsMutation
useUpdateNodeTagsMutation,
useDeleteNodesMutation
} = apiSliceWithNodes

0 comments on commit 7c9e193

Please sign in to comment.