'use client' import { Plus } from 'lucide-react' import { useState } from 'react' import { addEventWinner } from '@/actions/events' import { Button } from '@/components/ui/button' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { useServerAction } from '@/hooks/use-server-action' interface Winner { id: number userId: number position: number username: string createdAt: Date } interface Props { eventId: number winners: Winner[] } export function EventWinnersManager({ eventId, winners }: Props) { const { run, isPending } = useServerAction() const [showForm, setShowForm] = useState(false) const [userId, setUserId] = useState('') const [position, setPosition] = useState(winners.length + 1) function handleAdd(e: React.FormEvent) { e.preventDefault() run(() => addEventWinner({ eventId, userId: Number(userId), position }), { successMessage: 'Winner added!', onSuccess: () => { setShowForm(false) setUserId('') setPosition(position + 1) }, }) } return ( Winners {showForm && (
setUserId(e.target.value)} required placeholder="12345" />
setPosition(Number(e.target.value))} min={1} />
)} {winners.length === 0 && !showForm && (

No winners yet.

)}
{winners.map((w) => (
{w.position} {w.username} {new Date(w.createdAt).toLocaleDateString()}
))}
) }