https://dev.kronoz.win/r/marzneshin.git
1import { ServiceMutationType, 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 fetchCreateService(service: ServiceMutationType): Promise<ServiceMutationType> {
8 return fetch('/services', { method: 'post', body: service }).then((service) => {
9 return service;
10 });
11}
12
13const handleError = (error: Error, value: ServiceMutationType) => {
14 toast.error(
15 i18n.t('events.create.error', { name: value.name }),
16 {
17 description: error.message
18 })
19}
20
21const handleSuccess = (value: ServiceMutationType) => {
22 toast.success(
23 i18n.t('events.create.success.title', { name: value.name }),
24 {
25 description: i18n.t('events.create.success.desc')
26 })
27 queryClient.invalidateQueries({ queryKey: [ServicesQueryFetchKey] })
28}
29
30
31const ServicesCreateFetchKey = "services";
32
33export const useServicesCreationMutation = () => {
34 return useMutation({
35 mutationKey: [ServicesCreateFetchKey],
36 mutationFn: fetchCreateService,
37 onError: handleError,
38 onSuccess: handleSuccess,
39 })
40}