import React from 'react' import { Head, Link, usePage } from '@inertiajs/react' function requestJson(url, { method = 'GET', body } = {}) { return fetch(url, { method, credentials: 'same-origin', headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '', 'X-Requested-With': 'XMLHttpRequest', }, body: body ? JSON.stringify(body) : undefined, }).then(async (response) => { const payload = await response.json().catch(() => ({})) if (!response.ok) throw new Error(payload?.message || 'Request failed') return payload }) } export default function NovaCardsChallengeAdmin() { const { props } = usePage() const [challenges, setChallenges] = React.useState(props.challenges || []) const [selectedId, setSelectedId] = React.useState(null) const [form, setForm] = React.useState({ slug: '', title: '', description: '', prompt: '', rules_json: {}, status: 'draft', official: true, featured: false, winner_card_id: '', starts_at: '', ends_at: '' }) const endpoints = props.endpoints || {} const cards = props.cards || [] function loadChallenge(challenge) { setSelectedId(challenge.id) setForm({ slug: challenge.slug, title: challenge.title, description: challenge.description || '', prompt: challenge.prompt || '', rules_json: challenge.rules_json || {}, status: challenge.status || 'draft', official: Boolean(challenge.official), featured: Boolean(challenge.featured), winner_card_id: challenge.winner_card_id || '', starts_at: challenge.starts_at || '', ends_at: challenge.ends_at || '', }) } function resetForm() { setSelectedId(null) setForm({ slug: '', title: '', description: '', prompt: '', rules_json: {}, status: 'draft', official: true, featured: false, winner_card_id: '', starts_at: '', ends_at: '' }) } async function saveChallenge() { const isExisting = Boolean(selectedId) const url = isExisting ? String(endpoints.updatePattern || '').replace('__CHALLENGE__', String(selectedId)) : endpoints.store const response = await requestJson(url, { method: isExisting ? 'PATCH' : 'POST', body: { ...form, winner_card_id: form.winner_card_id || null } }) if (isExisting) { setChallenges((current) => current.map((challenge) => (challenge.id === selectedId ? response.challenge : challenge))) } else { setChallenges((current) => [...current, response.challenge]) setSelectedId(response.challenge.id) } } return (
Challenge system
Program official challenge prompts, track featured runs, and connect winner cards.