feat: auto-create tournament when uploading matches without selection
This commit is contained in:
@@ -82,7 +82,7 @@ export default function UploadMatchesPage() {
|
||||
.catch((err) => console.error("Failed to fetch players:", err))
|
||||
}, [])
|
||||
|
||||
const createDefaultTournament = async () => {
|
||||
const createDefaultTournament = async (): Promise<{ id: number; name: string } | null> => {
|
||||
try {
|
||||
const response = await fetch(`${window.location.origin}/api/tournaments`, {
|
||||
method: "POST",
|
||||
@@ -99,9 +99,12 @@ export default function UploadMatchesPage() {
|
||||
setTournaments(newTournaments)
|
||||
setSelectedTournament(data.tournament.id.toString())
|
||||
setManualTournament(data.tournament.id.toString())
|
||||
return data.tournament
|
||||
}
|
||||
return null
|
||||
} catch (err) {
|
||||
console.error("Failed to create default tournament:", err)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,17 +131,24 @@ export default function UploadMatchesPage() {
|
||||
return
|
||||
}
|
||||
|
||||
if (!selectedTournament) {
|
||||
setCsvError("Please select a tournament")
|
||||
return
|
||||
}
|
||||
|
||||
setIsCsvLoading(true)
|
||||
|
||||
try {
|
||||
let tournamentId = selectedTournament
|
||||
|
||||
// If no tournament is selected, create a default one
|
||||
if (!tournamentId) {
|
||||
const newTournament = await createDefaultTournament()
|
||||
if (newTournament) {
|
||||
tournamentId = newTournament.id.toString()
|
||||
} else {
|
||||
throw new Error("Failed to create default tournament")
|
||||
}
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append("csvFile", selectedFile)
|
||||
formData.append("eventId", selectedTournament)
|
||||
formData.append("eventId", tournamentId)
|
||||
|
||||
const response = await fetch(`${window.location.origin}/api/matches/upload`, {
|
||||
method: "POST",
|
||||
@@ -207,11 +217,6 @@ export default function UploadMatchesPage() {
|
||||
setManualError("")
|
||||
setManualSuccess("")
|
||||
|
||||
if (!manualTournament) {
|
||||
setManualError("Please select a tournament")
|
||||
return
|
||||
}
|
||||
|
||||
// Validate at least one match has required data
|
||||
const validMatches = manualMatches.filter(
|
||||
(m) =>
|
||||
@@ -226,8 +231,20 @@ export default function UploadMatchesPage() {
|
||||
setIsManualLoading(true)
|
||||
|
||||
try {
|
||||
let tournamentId = manualTournament
|
||||
|
||||
// If no tournament is selected, create a default one
|
||||
if (!tournamentId) {
|
||||
const newTournament = await createDefaultTournament()
|
||||
if (newTournament) {
|
||||
tournamentId = newTournament.id.toString()
|
||||
} else {
|
||||
throw new Error("Failed to create default tournament")
|
||||
}
|
||||
}
|
||||
|
||||
const matchesData = validMatches.map((m) => ({
|
||||
eventId: parseInt(manualTournament),
|
||||
eventId: parseInt(tournamentId),
|
||||
round: m.round ? parseInt(m.round) : undefined,
|
||||
table: m.table || undefined,
|
||||
team1P1Name: m.team1P1,
|
||||
|
||||
Reference in New Issue
Block a user