diff --git a/src/components/TournamentForm/TournamentForm.schema.ts b/src/components/TournamentForm/TournamentForm.schema.ts index 53584fbf..0848dd9f 100644 --- a/src/components/TournamentForm/TournamentForm.schema.ts +++ b/src/components/TournamentForm/TournamentForm.schema.ts @@ -1,4 +1,3 @@ -import { zodResolver } from '@hookform/resolvers/zod'; import { CurrencyCode, GameSystem, @@ -15,8 +14,12 @@ import { validateGameSystemConfig, } from '~/components/GameSystemConfigFields'; +interface CreateSchemaConfig { + competitorCount: number; +} + // TODO: Convert gameSystemConfig to union of other game systems -export const tournamentFormSchema = z.object({ +export const createSchema = (config: CreateSchemaConfig) => z.object({ // General title: z.string().min(3, 'Title must be at least 3 characters.').max(40, 'Titles are limited to 40 characters.'), @@ -97,15 +100,20 @@ export const tournamentFormSchema = z.object({ path: ['pairingConfig'], }); } + if (data.maxCompetitors < config.competitorCount) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: `Can not set tournament size to ${data.maxCompetitors} when there are already ${config.competitorCount} ${data.competitorSize > 1 ? 'teams' : 'players'} registered.`, + path: ['maxCompetitors'], + }); + } }); -export const tournamentFormResolver = zodResolver(tournamentFormSchema); - export type TournamentSubmitData = TournamentEditableFields; -export type TournamentFormData = z.infer; +export type TournamentFormData = z.infer>; -export const defaultValues: Omit, 'location'> = { +export const defaultValues: Omit>, 'location'> = { maxCompetitors: 20, competitorSize: 1, useNationalTeams: false, diff --git a/src/components/TournamentForm/TournamentForm.tsx b/src/components/TournamentForm/TournamentForm.tsx index 0fcc9605..a6ec10ad 100644 --- a/src/components/TournamentForm/TournamentForm.tsx +++ b/src/components/TournamentForm/TournamentForm.tsx @@ -12,6 +12,7 @@ import { GameSystemConfigFields } from '~/components/GameSystemConfigFields'; import { Card } from '~/components/generic/Card'; import { Form, FormField } from '~/components/generic/Form'; import { InputSelect } from '~/components/generic/InputSelect'; +import { useGetTournamentCompetitorsByTournament } from '~/services/tournamentCompetitors'; import { useGetTournament } from '~/services/tournaments'; import { validateForm } from '~/utils/validateForm'; import { CompetitorFields } from './components/CompetitorFields'; @@ -19,9 +20,9 @@ import { FormatFields } from './components/FormatFields'; import { GeneralFields } from './components/GeneralFields'; import { PairingFields } from './components/PairingFields'; import { + createSchema, defaultValues, TournamentFormData, - tournamentFormSchema, TournamentSubmitData, } from './TournamentForm.schema'; import { convertDateToEpoch, convertEpochToDate } from './TournamentForm.utils'; @@ -41,7 +42,12 @@ export const TournamentForm = ({ onSubmit: handleSubmit, tournamentId, }: TournamentFormProps): JSX.Element => { - const { data: tournament } = useGetTournament(tournamentId ? { id: tournamentId } : 'skip'); + const { data: tournament } = useGetTournament(tournamentId ? { + id: tournamentId, + } : 'skip'); + const { data: tournamentCompetitors } = useGetTournamentCompetitorsByTournament(tournamentId ? { + tournamentId: tournamentId, + } : 'skip'); const form = useForm({ defaultValues: { ...defaultValues, @@ -57,7 +63,9 @@ export const TournamentForm = ({ mode: 'onSubmit', }); const onSubmit: SubmitHandler = async (formData) => { - const data = validateForm(tournamentFormSchema, formData, form.setError as UseFormSetError); + const data = validateForm(createSchema({ + competitorCount: tournamentCompetitors?.length ?? 0, + }), formData, form.setError as UseFormSetError); if (data) { handleSubmit({ ...data, diff --git a/src/components/TournamentForm/components/CompetitorFields.tsx b/src/components/TournamentForm/components/CompetitorFields.tsx index 3bcd0529..b72f2551 100644 --- a/src/components/TournamentForm/components/CompetitorFields.tsx +++ b/src/components/TournamentForm/components/CompetitorFields.tsx @@ -89,7 +89,7 @@ export const CompetitorFields = ({
- +