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 AdminsQueryFetchKey,
7 AdminMutationType,
8 AdminType
9} from "@marzneshin/modules/admins";
10
11export async function updateAdmin(admin: AdminMutationType): Promise<AdminType> {
12 return fetch(`/admins/${admin.username}`, { method: 'put', body: admin }).then((admin) => {
13 return admin;
14 });
15}
16
17const handleError = (error: Error, value: AdminMutationType) => {
18 toast.error(
19 i18n.t('events.update.error', { name: value.username }),
20 {
21 description: error.message
22 })
23}
24
25const handleSuccess = (value: AdminType) => {
26 toast.success(
27 i18n.t('events.update.success.title', { name: value.username }),
28 {
29 description: i18n.t('events.update.success.desc')
30 })
31 queryClient.invalidateQueries({ queryKey: [AdminsQueryFetchKey] })
32}
33
34
35const AdminsUpdateFetchKey = "admins";
36
37export const useAdminsUpdateMutation = () => {
38 return useMutation({
39 mutationKey: [AdminsUpdateFetchKey],
40 mutationFn: updateAdmin,
41 onError: handleError,
42 onSuccess: handleSuccess,
43 })
44}