gitscope
← marzneshin / v0.7.0 / dashboard/src/modules/nodes/api/create-node.mutate.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/modules/nodes/api/create-node.mutate.ts · TypeScript
Blame Raw
 1import { NodeType, NodesQueryFetchKey } from "@marzneshin/modules/nodes";
 2import { useMutation } from "@tanstack/react-query";
 3import { fetch, queryClient } from "@marzneshin/common/utils";
 4import { toast } from "sonner";
 5import i18n from "@marzneshin/features/i18n";
 6
 7export async function fetchCreateNode(node: NodeType): Promise<NodeType> {
 8    return fetch('/nodes', { method: 'post', body: node }).then((node) => {
 9        return node;
10    });
11}
12
13const handleError = (error: Error, value: NodeType) => {
14    toast.error(
15        i18n.t('events.create.error', { name: value.name }),
16        {
17            description: error.message
18        })
19}
20
21const handleSuccess = (value: NodeType) => {
22    toast.success(
23        i18n.t('events.create.success.title', { name: value.name }),
24        {
25            description: i18n.t('events.create.success.desc')
26        })
27    queryClient.invalidateQueries({ queryKey: [NodesQueryFetchKey] })
28}
29
30
31const NodesCreateFetchKey = "nodes";
32
33export const useNodesCreationMutation = () => {
34    return useMutation({
35        mutationKey: [NodesCreateFetchKey],
36        mutationFn: fetchCreateNode,
37        onError: handleError,
38        onSuccess: handleSuccess,
39    })
40}