significant refactor
This commit is contained in:
@@ -1,41 +1,26 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { authQueryConfig } from "@/contexts/auth-context";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { useNavigate } from "@tanstack/react-router";
|
||||
import { associatePlayer, createPlayer } from "@/features/players/server";
|
||||
import toast from '@/lib/sonner';
|
||||
import { playerKeys } from "@/features/players/queries";
|
||||
import { useServerMutation } from "@/lib/tanstack-query/hooks";
|
||||
|
||||
const useCreateUser = () => {
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (data: { first_name: string, last_name: string } | string) => {
|
||||
if (typeof data === 'string') {
|
||||
return associatePlayer({ data });
|
||||
} else {
|
||||
return createPlayer({ data });
|
||||
}
|
||||
},
|
||||
return useServerMutation({
|
||||
mutationFn: (data: { first_name: string, last_name: string } | string) =>
|
||||
typeof data === 'string' ?
|
||||
associatePlayer({ data })
|
||||
: createPlayer({ data }),
|
||||
successMessage: 'Account created successfully!',
|
||||
onSuccess: (data) => {
|
||||
if (!data) {
|
||||
toast.error('There was an issue creating your account. Please try again later.');
|
||||
navigate({ to: '/login' });
|
||||
} else {
|
||||
queryClient.setQueryData(authQueryConfig.queryKey, (old: any) => ({
|
||||
queryClient.setQueryData(playerKeys.auth, (old: any) => ({
|
||||
...old,
|
||||
user: data
|
||||
}));
|
||||
toast.success('Account created successfully!');
|
||||
navigate({ to: '/' });
|
||||
}
|
||||
},
|
||||
onError: (error: any) => {
|
||||
if (error.message) {
|
||||
toast.error(error.message);
|
||||
} else {
|
||||
toast.error('An unexpected error occurred when trying to create an account. Please try again later.');
|
||||
}
|
||||
},
|
||||
navigate({ to: '/' });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user