gitscope
← marzneshin / v0.5.0 / dashboard/src/features/users/services/delete-user.mutate.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/users/services/delete-user.mutate.ts · TypeScript
Blame Raw
 1import {  UserMutationType, UsersQueryFetchKey } from "@marzneshin/features/users";
 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 fetchDeleteUser(user: UserMutationType): Promise<UserMutationType> {
 8    return fetch(`/users/${user.username}`, { method: 'delete' }).then((user) => {
 9        return user;
10    });
11}
12
13const UsersDeleteFetchKey = "users";
14
15
16const handleError = (error: Error, value: UserMutationType) => {
17    toast.error(
18        i18n.t('events.delete.error', { name: value.username }),
19        {
20            description: error.message
21        })
22}
23
24const handleSuccess = (value: UserMutationType) => {
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: [UsersQueryFetchKey] })
31}
32
33export const useUsersDeletionMutation = () => {
34    return useMutation({
35        mutationKey: [UsersDeleteFetchKey],
36        mutationFn: fetchDeleteUser,
37        onError: handleError,
38        onSuccess: handleSuccess,
39    })
40}