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