| 1 |
6955aba |
Mrdn |
2y ago |
import { useMutation } from "@tanstack/react-query"; |
| 2 |
6955aba |
Mrdn |
2y ago |
import { fetch, queryClient } from "@marzneshin/utils"; |
| 3 |
6955aba |
Mrdn |
2y ago |
import { toast } from "sonner"; |
| 4 |
6955aba |
Mrdn |
2y ago |
import i18n from "@marzneshin/features/i18n"; |
| 5 |
6955aba |
Mrdn |
2y ago |
import { |
| 6 |
6955aba |
Mrdn |
2y ago |
UserMutationType |
| 7 |
e6e6467 |
Dawsh |
2y ago |
} from "@marzneshin/modules/users"; |
| 8 |
6955aba |
Mrdn |
2y ago |
|
| 9 |
6955aba |
Mrdn |
2y ago |
export async function userSubscriptionRvoke(user: UserMutationType): Promise<UserMutationType> { |
| 10 |
6955aba |
Mrdn |
2y ago |
return fetch(`/users/${user.username}/revoke_sub`, { method: 'post', body: user }).then((user) => { |
| 11 |
6955aba |
Mrdn |
2y ago |
return user; |
| 12 |
6955aba |
Mrdn |
2y ago |
}); |
| 13 |
6955aba |
Mrdn |
2y ago |
} |
| 14 |
6955aba |
Mrdn |
2y ago |
|
| 15 |
6955aba |
Mrdn |
2y ago |
const handleError = (error: Error, value: UserMutationType) => { |
| 16 |
6955aba |
Mrdn |
2y ago |
toast.error( |
| 17 |
6955aba |
Mrdn |
2y ago |
i18n.t('events.user_revoke.error', { name: value.username }), |
| 18 |
6955aba |
Mrdn |
2y ago |
{ |
| 19 |
6955aba |
Mrdn |
2y ago |
description: error.message |
| 20 |
6955aba |
Mrdn |
2y ago |
}) |
| 21 |
6955aba |
Mrdn |
2y ago |
} |
| 22 |
6955aba |
Mrdn |
2y ago |
|
| 23 |
6955aba |
Mrdn |
2y ago |
const handleSuccess = (value: UserMutationType) => { |
| 24 |
6955aba |
Mrdn |
2y ago |
toast.success( |
| 25 |
6955aba |
Mrdn |
2y ago |
i18n.t('events.user_revoke.success.title', { name: value.username }), |
| 26 |
6955aba |
Mrdn |
2y ago |
{ |
| 27 |
6955aba |
Mrdn |
2y ago |
description: i18n.t('events.user_revoke.success.desc') |
| 28 |
6955aba |
Mrdn |
2y ago |
}) |
| 29 |
6955aba |
Mrdn |
2y ago |
queryClient.invalidateQueries({ queryKey: [UsersSubscriptionFetchKey] }) |
| 30 |
6955aba |
Mrdn |
2y ago |
} |
| 31 |
6955aba |
Mrdn |
2y ago |
|
| 32 |
6955aba |
Mrdn |
2y ago |
|
| 33 |
6955aba |
Mrdn |
2y ago |
const UsersSubscriptionFetchKey = "users"; |
| 34 |
6955aba |
Mrdn |
2y ago |
|
| 35 |
6955aba |
Mrdn |
2y ago |
export const useUserSubscriptionRevokeCmd = () => { |
| 36 |
6955aba |
Mrdn |
2y ago |
return useMutation({ |
| 37 |
6955aba |
Mrdn |
2y ago |
mutationKey: [UsersSubscriptionFetchKey, "revoke_sub"], |
| 38 |
6955aba |
Mrdn |
2y ago |
mutationFn: userSubscriptionRvoke, |
| 39 |
6955aba |
Mrdn |
2y ago |
onError: handleError, |
| 40 |
6955aba |
Mrdn |
2y ago |
onSuccess: handleSuccess, |
| 41 |
6955aba |
Mrdn |
2y ago |
}) |
| 42 |
6955aba |
Mrdn |
2y ago |
} |