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