gitscope
← marzneshin / v0.3.0-alpha10 / dashboard/src/features/users/services/user-subscription-revoke.command.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/users/services/user-subscription-revoke.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    UserMutationType
 7} from "@marzneshin/features/users";
 8
 9export async function userSubscriptionRvoke(user: UserMutationType): Promise<UserMutationType> {
10    return fetch(`/users/${user.username}/revoke_sub`, { method: 'post', body: user }).then((user) => {
11        return user;
12    });
13}
14
15const handleError = (error: Error, value: UserMutationType) => {
16    toast.error(
17        i18n.t('events.user_revoke.error', { name: value.username }),
18        {
19            description: error.message
20        })
21}
22
23const handleSuccess = (value: UserMutationType) => {
24    toast.success(
25        i18n.t('events.user_revoke.success.title', { name: value.username }),
26        {
27            description: i18n.t('events.user_revoke.success.desc')
28        })
29    queryClient.invalidateQueries({ queryKey: [UsersSubscriptionFetchKey] })
30}
31
32
33const UsersSubscriptionFetchKey = "users";
34
35export const useUserSubscriptionRevokeCmd = () => {
36    return useMutation({
37        mutationKey: [UsersSubscriptionFetchKey, "revoke_sub"],
38        mutationFn: userSubscriptionRvoke,
39        onError: handleError,
40        onSuccess: handleSuccess,
41    })
42}