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