gitscope
← marzneshin / v0.6.1 / dashboard/src/modules/admins/services/delete-admin.mutate.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/modules/admins/services/delete-admin.mutate.ts · TypeScript
Blame Raw
 1import { AdminType, AdminsQueryFetchKey } from "@marzneshin/modules/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 fetchDeleteAdmin(admin: AdminType): Promise<AdminType> {
 8    return fetch(`/admins/${admin.username}`, { method: 'delete' }).then((admin) => {
 9        return admin;
10    });
11}
12
13const AdminsDeleteFetchKey = "admins";
14
15
16const handleError = (error: Error, value: AdminType) => {
17    toast.error(
18        i18n.t('events.delete.error', { name: value.username }),
19        {
20            description: error.message
21        })
22}
23
24const handleSuccess = (value: AdminType) => {
25    toast.success(
26        i18n.t('events.delete.success.title', { name: value.username }),
27        {
28            description: i18n.t('events.delete.success.desc')
29        })
30    queryClient.invalidateQueries({ queryKey: [AdminsQueryFetchKey] })
31}
32
33export const useAdminsDeletionMutation = () => {
34    return useMutation({
35        mutationKey: [AdminsDeleteFetchKey],
36        mutationFn: fetchDeleteAdmin,
37        onError: handleError,
38        onSuccess: handleSuccess,
39    })
40}