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