gitscope
← marzneshin / v0.3.0-alpha7 / 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
 8interface UpdateNodesSettings {
 9    node: NodeType
10    config: string
11}
12
13export async function fetchUpdateNodesSettings({ node, config }: UpdateNodesSettings): Promise<NodeType> {
14    return fetch(`/nodes/${node.id}/xray_config`, { method: 'put', body: config }).then((node) => {
15        return node;
16    });
17}
18
19const handleError = (error: Error, value: UpdateNodesSettings) => {
20    toast.error(
21        i18n.t('events.update.error', { name: value.node.name }),
22        {
23            description: error.message
24        })
25}
26
27const handleSuccess = (value: NodeType) => {
28    toast.success(
29        i18n.t('events.update.success.title', { name: value.name }),
30        {
31            description: i18n.t('events.update.success.desc')
32        })
33    queryClient.invalidateQueries({ queryKey: [NodesSettingsQueryFetchKey] })
34}
35
36
37const NodesSettingsUpdateFetchKey = ["nodes", "xray_config"];
38
39export const useNodesSettingsMutation = () => {
40    return useMutation({
41        mutationKey: NodesSettingsUpdateFetchKey,
42        mutationFn: fetchUpdateNodesSettings,
43        onError: handleError,
44        onSuccess: handleSuccess,
45    })
46}