'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' export function AlertForm({ productId }: { productId: string }) { const [type, setType] = useState<'target_price' | 'all_time_low' | 'percent_drop'>('target_price') const [threshold, setThreshold] = useState('') const [percent, setPercent] = useState('10') const [lookback, setLookback] = useState('7') const router = useRouter() async function submit(e: React.FormEvent) { e.preventDefault() let config: Record = {} if (type === 'target_price') config = { threshold: Number(threshold) } if (type === 'percent_drop') config = { percent: Number(percent), lookback_days: Number(lookback) } const res = await fetch('/api/alerts', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ productId, type, config }), }) if (res.ok) { setThreshold('') router.refresh() } } return (
{type === 'target_price' && ( setThreshold(e.target.value)} placeholder="€" className="w-24 rounded border border-zinc-700 bg-zinc-900 px-2 py-1 text-sm" /> )} {type === 'percent_drop' && ( <> setPercent(e.target.value)} className="w-16 rounded border border-zinc-700 bg-zinc-900 px-2 py-1 text-sm" /> % in setLookback(e.target.value)} className="w-16 rounded border border-zinc-700 bg-zinc-900 px-2 py-1 text-sm" /> Tagen )}
) }