https://dev.kronoz.win/r/marzneshin.git
1
2import { NodeType, NodesQueryFetchKey } from "@marzneshin/modules/nodes";
3import { useMutation } from "@tanstack/react-query";
4import { fetch, queryClient } from "@marzneshin/utils";
5import { toast } from "sonner";
6import i18n from "@marzneshin/features/i18n";
7
8export async function fetchDeleteNode(node: NodeType): Promise<NodeType> {
9 return fetch(`/nodes/${node.id}`, { method: 'delete' }).then((node) => {
10 return node;
11 });
12}
13
14const NodesDeleteFetchKey = "nodes";
15
16
17const handleError = (error: Error, value: NodeType) => {
18 toast.error(
19 i18n.t('events.delete.error', { name: value.name }),
20 {
21 description: error.message
22 })
23}
24
25const handleSuccess = (value: NodeType) => {
26 toast.success(
27 i18n.t('events.delete.success.title', { name: value.name }),
28 {
29 description: i18n.t('events.delete.success.desc')
30 })
31 queryClient.invalidateQueries({ queryKey: [NodesQueryFetchKey] })
32}
33
34export const useNodesDeletionMutation = () => {
35 return useMutation({
36 mutationKey: [NodesDeleteFetchKey],
37 mutationFn: fetchDeleteNode,
38 onError: handleError,
39 onSuccess: handleSuccess,
40 })
41}