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 OnboardingFlowProps { onComplete?: () => void; } export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) { const [startingValue, setStartingValue] = useState('0'); const [processing, setProcessing] = useState(false); const [error, setError] = useState(); const submit: FormEventHandler = async (e) => { e.preventDefault(); setProcessing(true); setError(undefined); const headers = { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrfToken(), Accept: 'application/json', }; try { const trackerResponse = await fetch(route('tracker.store'), { method: 'POST', headers, body: JSON.stringify({}), }); if (!trackerResponse.ok && trackerResponse.status !== 409) { setError('Could not create the counter. Please try again.'); return; } const count = Number(startingValue); if (count > 0) { const countResponse = await fetch('/count', { method: 'PATCH', headers, body: JSON.stringify({ count }), }); if (!countResponse.ok) { setError('Could not save the starting value. Please try again.'); return; } } onComplete?.(); } catch { setError('Something went wrong. Please try again.'); } finally { setProcessing(false); } }; return (
setStartingValue(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" />
); }