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