gitscope
← marzneshin / v0.7.4 / dashboard/src/modules/users/api/commands/user-status.command.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/modules/users/api/commands/user-status.command.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 {
 6    UserMutationType,
 7} from "@marzneshin/modules/users";
 8
 9
10interface UsersStatusEnabledQuery {
11    user: UserMutationType;
12    enabled: boolean;
13}
14
15export async function userStatusEnabled({ user, enabled }: UsersStatusEnabledQuery): Promise<UserMutationType> {
16    const action = enabled ? 'enable' : 'disable';
17    return fetch(`/users/${user.username}/${action}`, { method: 'post' }).then((user) => {
18        return user;
19    });
20}
21
22const handleError = (error: Error, value: UsersStatusEnabledQuery) => {
23    toast.error(
24        i18n.t('events.user_status.error', { name: value.user.username }),
25        {
26            description: error.message
27        })
28}
29
30const handleSuccess = (value: UserMutationType) => {
31    toast.success(
32        i18n.t('events.user_status.success.title', { name: value.username }),
33        {
34            description: i18n.t('events.user_status.success.desc')
35        })
36    queryClient.invalidateQueries({ queryKey: [UsersStatusEnabledFetchKey] })
37    queryClient.invalidateQueries({ queryKey: [UsersStatusEnabledFetchKey, value.username] })
38}
39
40
41const UsersStatusEnabledFetchKey = "users";
42
43export const useUserStatusEnable = () => {
44    return useMutation({
45        mutationKey: [UsersStatusEnabledFetchKey],
46        mutationFn: userStatusEnabled,
47        onError: handleError,
48        onSuccess: handleSuccess,
49    })
50}