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