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