gitscope
← marzneshin / v0.3.0-alpha3 / dashboard/src/features/nodes/services/settings.mutate.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/nodes/services/settings.mutate.ts · TypeScript
Blame Raw
 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 { NodesSettingsQueryFetchKey } from "./settings.query";
 7
 8export async function fetchUpdateNodesSettings(node: NodeType): Promise<NodeType> {
 9    return fetch(`/nodes/${node.id}/xray_config`, { 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: [NodesSettingsQueryFetchKey] })
29}
30
31
32const NodesSettingsUpdateFetchKey = ["nodes", "xray_config"];
33
34export const useNodesSettingsMutation = () => {
35    return useMutation({
36        mutationKey: NodesSettingsUpdateFetchKey,
37        mutationFn: fetchUpdateNodesSettings,
38        onError: handleError,
39        onSuccess: handleSuccess,
40    })
41}