significant refactor

This commit is contained in:
2025-08-30 01:42:23 -05:00
parent 7136f646a3
commit 052f53444e
106 changed files with 1994 additions and 1701 deletions

View File

@@ -1,35 +1,21 @@
import { useMutation } from "@tanstack/react-query";
import { useNavigate } from "@tanstack/react-router";
import { createTournament } from "@/features/tournaments/server";
import toast from '@/lib/sonner';
import { TournamentInput } from "@/features/tournaments/types";
import { logger } from "../";
import { useServerMutation } from "@/lib/tanstack-query/hooks";
const useCreateTournament = () => {
const navigate = useNavigate();
return useMutation({
return useServerMutation({
mutationFn: (data: TournamentInput) => createTournament({ data }),
onMutate: (data) => {
logger.info('Creating tournament', data);
},
onSuccess: (data) => {
if (!data) {
toast.error('There was an issue creating your tournament. Please try again later.');
logger.error('Error creating tournament', data);
} else {
logger.info('Tournament created successfully', data);
navigate({ to: '/tournaments' });
}
},
onError: (error: any) => {
logger.error('Error creating tournament', error);
if (error.message) {
toast.error(error.message);
} else {
toast.error('An unexpected error occurred when trying to create a tournament. Please try again later.');
}
onSuccess: () => {
navigate({ to: '/tournaments' });
},
successMessage: 'Tournament created successfully!',
});
};

View File

@@ -1,31 +1,19 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useQueryClient } from "@tanstack/react-query";
import { enrollTeam } from "@/features/tournaments/server";
import toast from '@/lib/sonner';
import { useServerMutation } from "@/lib/tanstack-query/hooks";
const useEnrollTeam = () => {
const queryClient = useQueryClient();
return useMutation({
return useServerMutation({
mutationFn: (data: { tournamentId: string, teamId: string }) => {
return enrollTeam({ data });
},
onSuccess: (data, { tournamentId }) => {
if (!data) {
toast.error('There was an issue enrolling. Please try again later.');
} else {
// Invalidate both tournament details and unenrolled teams queries
queryClient.invalidateQueries({ queryKey: ['tournaments', 'details', tournamentId] });
queryClient.invalidateQueries({ queryKey: ['tournaments', 'unenrolled', tournamentId] });
toast.success('Team enrolled successfully!');
}
},
onError: (error: any) => {
if (error.message) {
toast.error(error.message);
} else {
toast.error('An unexpected error occurred when trying to enroll the team. Please try again later.');
}
queryClient.invalidateQueries({ queryKey: ['tournaments', 'details', tournamentId] });
queryClient.invalidateQueries({ queryKey: ['tournaments', 'unenrolled', tournamentId] });
},
successMessage: 'Team enrolled successfully!',
});
};

View File

@@ -1,31 +1,19 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useQueryClient } from "@tanstack/react-query";
import { unenrollTeam } from "@/features/tournaments/server";
import toast from '@/lib/sonner';
import { useServerMutation } from "@/lib/tanstack-query/hooks";
const useUnenrollTeam = () => {
const queryClient = useQueryClient();
return useMutation({
return useServerMutation({
mutationFn: (data: { tournamentId: string, teamId: string }) => {
return unenrollTeam({ data });
},
onSuccess: (data, { tournamentId }) => {
if (!data) {
toast.error('There was an issue unenrolling. Please try again later.');
} else {
// Invalidate both tournament details and unenrolled teams queries
queryClient.invalidateQueries({ queryKey: ['tournaments', 'details', tournamentId] });
queryClient.invalidateQueries({ queryKey: ['tournaments', 'unenrolled', tournamentId] });
toast.success('Team unenrolled successfully.');
}
},
onError: (error: any) => {
if (error.message) {
toast.error(error.message);
} else {
toast.error('An unexpected error occurred when trying to unenroll the team. Please try again later.');
}
onSuccess: (_, { tournamentId }) => {
queryClient.invalidateQueries({ queryKey: ['tournaments', 'details', tournamentId] });
queryClient.invalidateQueries({ queryKey: ['tournaments', 'unenrolled', tournamentId] });
},
successMessage: 'Team unenrolled successfully.',
});
};

View File

@@ -1,10 +1,10 @@
import { useMutation } from "@tanstack/react-query";
import { updateTournament } from "@/features/tournaments/server";
import { TournamentFormInput } from "@/features/tournaments/types";
import { TournamentInput } from "@/features/tournaments/types";
import { useServerMutation } from "@/lib/tanstack-query/hooks";
const useUpdateTournament = (tournamentId: string) => {
return useMutation({
mutationFn: (data: Partial<TournamentFormInput>) =>
return useServerMutation({
mutationFn: (data: Partial<TournamentInput>) =>
updateTournament({ data: { id: tournamentId, updates: data } }),
});
};