https://dev.kronoz.win/r/marzneshin.git
1import { UserMutationType, UsersQueryFetchKey } from "@marzneshin/modules/users";
2import { useMutation } from "@tanstack/react-query";
3import { fetch, queryClient } from "@marzneshin/common/utils";
4import { toast } from "sonner";
5import i18n from "@marzneshin/features/i18n";
6
7export async function fetchCreateUser(user: UserMutationType): Promise<UserMutationType> {
8 return fetch('/users', { method: 'post', body: user }).then((user) => {
9 return user;
10 });
11}
12
13const handleError = (error: Error, value: UserMutationType) => {
14 toast.error(
15 i18n.t('events.create.error', { name: value.username }),
16 {
17 description: error.message
18 })
19}
20
21const handleSuccess = (value: UserMutationType) => {
22 toast.success(
23 i18n.t('events.create.success.title', { name: value.username }),
24 {
25 description: i18n.t('events.create.success.desc')
26 })
27 queryClient.invalidateQueries({ queryKey: [UsersQueryFetchKey] })
28}
29
30
31const UsersCreateFetchKey = "users";
32
33export const useUsersCreationMutation = () => {
34 return useMutation({
35 mutationKey: [UsersCreateFetchKey],
36 mutationFn: fetchCreateUser,
37 onError: handleError,
38 onSuccess: handleSuccess,
39 })
40}