import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import InputError from '@/components/InputError'; import { LoaderCircle } from 'lucide-react'; import { FormEventHandler, useState } from 'react'; import ComponentTitle from '@/components/ui/ComponentTitle'; interface CreateTrackerStepProps { onSuccess: (priceTrackingEnabled: boolean) => void; } export default function CreateTrackerStep({ onSuccess }: CreateTrackerStepProps) { const [label, setLabel] = useState(''); const [unit, setUnit] = useState(''); const [priceTracking, setPriceTracking] = useState(false); const [symbol, setSymbol] = useState(''); const [fullName, setFullName] = useState(''); const [processing, setProcessing] = useState(false); const [errors, setErrors] = useState>({}); const togglePriceTracking = (enabled: boolean) => { setPriceTracking(enabled); if (!enabled) { setSymbol(''); setFullName(''); } }; const submit: FormEventHandler = async (e) => { e.preventDefault(); setProcessing(true); setErrors({}); try { const response = await fetch(route('tracker.store'), { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': (document.querySelector('meta[name="csrf-token"]') as HTMLMetaElement)?.content ?? '', 'Accept': 'application/json', }, body: JSON.stringify({ label, unit, price_tracking_enabled: priceTracking ? 1 : 0, symbol: priceTracking ? symbol : null, full_name: priceTracking ? fullName : null, }), }); if (response.ok || response.status === 201 || response.status === 409) { onSuccess(priceTracking); } else { const data = await response.json(); if (data.errors) { setErrors(data.errors); } else if (data.message) { setErrors({ label: data.message }); } } } catch { setErrors({ label: 'Something went wrong. Please try again.' }); } finally { setProcessing(false); } }; return (
SET UP YOUR TRACKER

[SYSTEM] What are you tracking?

setLabel(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 focus:shadow-[0_0_10px_rgba(239,68,68,0.5)] placeholder:text-red-400/40 transition-all" />

[REQUIRED] e.g. "My Portfolio", "Books Read", "KM Run"

setUnit(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 focus:shadow-[0_0_10px_rgba(239,68,68,0.5)] placeholder:text-red-400/40 transition-all" />

[REQUIRED] e.g. "shares", "books", "km"

Track market price, portfolio value, and P&L. Requires an asset symbol.

{priceTracking && (
setSymbol(e.target.value.toUpperCase())} 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 focus:shadow-[0_0_10px_rgba(239,68,68,0.5)] placeholder:text-red-400/40 transition-all" />
setFullName(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 focus:shadow-[0_0_10px_rgba(239,68,68,0.5)] placeholder:text-red-400/40 transition-all" />
)}
); }