diff --git a/app/routes/admin.sports-seasons.$id.participants.tsx b/app/routes/admin.sports-seasons.$id.participants.tsx index 2143e5f..b2d4cb1 100644 --- a/app/routes/admin.sports-seasons.$id.participants.tsx +++ b/app/routes/admin.sports-seasons.$id.participants.tsx @@ -64,6 +64,39 @@ export async function action({ request, params }: Route.ActionArgs) { return { success: true }; } + if (intent === "update-participant") { + const participantId = formData.get("participantId"); + const newName = formData.get("newName"); + const newExternalId = formData.get("newExternalId"); + + if (typeof participantId !== "string") { + return { error: "Invalid participant", intent: "update-participant" }; + } + + if (typeof newName !== "string" || !newName.trim()) { + return { error: "Name is required", intent: "update-participant" }; + } + + const trimmedName = newName.trim(); + const existing = await findParticipantByName(params.id, trimmedName); + if (existing && existing.id !== participantId) { + return { error: `"${trimmedName}" already exists in this season.`, intent: "update-participant" }; + } + + const externalIdValue = + typeof newExternalId === "string" && newExternalId.trim() + ? newExternalId.trim() + : null; + + try { + await updateParticipant(participantId, { name: trimmedName, externalId: externalIdValue }); + return { success: true, intent: "update-participant" }; + } catch (error) { + logger.error("Error updating participant:", error); + return { error: "Failed to update participant. Please try again.", intent: "update-participant" }; + } + } + if (intent === "update-name") { const participantId = formData.get("participantId"); const newName = formData.get("newName"); @@ -171,7 +204,7 @@ export async function action({ request, params }: Route.ActionArgs) { export default function ManageParticipants({ loaderData, actionData }: Route.ComponentProps) { const { sportsSeason, participants } = loaderData; - const [editing, setEditing] = useState<{ id: string; name: string } | null>(null); + const [editing, setEditing] = useState<{ id: string; name: string; externalId: string } | null>(null); const cancelEdit = () => setEditing(null); @@ -179,7 +212,7 @@ export default function ManageParticipants({ loaderData, actionData }: Route.Com const formKey = actionData?.success ? Date.now() : 'static'; useEffect(() => { - if (actionData?.success && actionData?.intent === "update-name") { + if (actionData?.success && (actionData?.intent === "update-name" || actionData?.intent === "update-participant")) { cancelEdit(); } }, [actionData]); @@ -221,13 +254,13 @@ export default function ManageParticipants({ loaderData, actionData }: Route.Com /> - {actionData?.error && !actionData?.count && actionData?.intent !== "update-name" && ( + {actionData?.error && !actionData?.count && actionData?.intent !== "update-name" && actionData?.intent !== "update-participant" && (
{actionData.error}
)}