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