https://dev.kronoz.win/r/marzneshin.git
1import { useMutation } from "@tanstack/react-query";
2import { fetch, queryClient } from "@marzneshin/utils";
3import { toast } from "sonner";
4import i18n from "@marzneshin/features/i18n";
5import {
6 UserMutationType,
7} from "@marzneshin/features/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}
38
39
40const UsersStatusEnabledFetchKey = "users";
41
42export const useUserStatusEnable = () => {
43 return useMutation({
44 mutationKey: [UsersStatusEnabledFetchKey],
45 mutationFn: userStatusEnabled,
46 onError: handleError,
47 onSuccess: handleSuccess,
48 })
49}