Files
gridpilot.gg/apps/website/hooks/league/useCreateLeagueWithBlockers.ts
2026-01-19 12:35:16 +01:00

50 lines
1.8 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query';
import { useInject } from '@/lib/di/hooks/useInject';
import { LEAGUE_SERVICE_TOKEN } from '@/lib/di/tokens';
import { ApiError } from '@/lib/api/base/ApiError';
import { CreateLeagueInputDTO } from '@/lib/types/generated/CreateLeagueInputDTO';
import { CreateLeagueOutputDTO } from '@/lib/types/generated/CreateLeagueOutputDTO';
export interface CreateLeagueInput {
name: string;
description: string;
maxDrivers: number;
scoringPresetId: string;
}
export interface CreateLeagueResult {
success: boolean;
leagueId: string;
error?: string;
}
export function useCreateLeagueWithBlockers(
options?: Omit<UseMutationOptions<CreateLeagueResult, ApiError, CreateLeagueInput>, 'mutationFn'>
) {
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
return useMutation<CreateLeagueResult, ApiError, CreateLeagueInput>({
mutationFn: async (input) => {
try {
// Transform input to DTO - note: maxDrivers and scoringPresetId are not in the DTO
// This hook may need to be updated based on actual API requirements
const inputDto: CreateLeagueInputDTO = {
name: input.name,
description: input.description,
visibility: 'public', // Default value
ownerId: '', // Will be set by the service
};
const result: CreateLeagueOutputDTO = await leagueService.createLeague(inputDto);
return { success: result.success, leagueId: result.leagueId };
} catch (error) {
// Check if it's a rate limit error
if (error instanceof ApiError && error.type === 'RATE_LIMIT_ERROR') {
return { success: false, leagueId: '', error: 'Rate limited' };
}
throw error;
}
},
...options,
});
}