import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import InputError from '@/components/InputError'; import { csrfToken } from '@/lib/utils'; import { LoaderCircle } from 'lucide-react'; import { FormEventHandler, useState } from 'react'; interface SetCountFormProps { currentCount: number; onClose: () => void; onSuccess: (count: number) => void; } export default function SetCountForm({ currentCount, onClose, onSuccess }: SetCountFormProps) { const [value, setValue] = useState(String(currentCount)); const [processing, setProcessing] = useState(false); const [error, setError] = useState(); const submit: FormEventHandler = async (e) => { e.preventDefault(); setProcessing(true); setError(undefined); try { const response = await fetch('/count', { method: 'PATCH', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrfToken(), Accept: 'application/json', }, body: JSON.stringify({ count: Number(value) }), }); if (!response.ok) { setError('Enter a whole number of zero or more.'); return; } const { count } = await response.json(); onSuccess(count); onClose(); } catch { setError('Something went wrong. Please try again.'); } finally { setProcessing(false); } }; return (
setValue(e.target.value)} className="bg-black border-red-500 text-red-400 focus:border-red-300 font-mono text-sm rounded-none border-2 focus:ring-0 focus:outline-none placeholder:text-red-400/40 transition-all glow-red" />
); }