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