https://dev.kronoz.win/r/marzneshin.git
1import { NodeType, NodesQueryFetchKey } from "@marzneshin/modules/nodes";
2import { useMutation } from "@tanstack/react-query";
3import { fetch, queryClient } from "@marzneshin/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}