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