'use client'; import { useSession } from '@/lib/better-auth'; import { useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; import Link from 'next/link'; export default function WatchlistPage() { const { data: session, isPending } = useSession(); const router = useRouter(); const [watchlist, setWatchlist] = useState([]); const [loading, setLoading] = useState(true); const [showAddModal, setShowAddModal] = useState(false); const [newStock, setNewStock] = useState({ ticker: '', company_name: '', sector: '' }); useEffect(() => { if (!isPending && !session) { router.push('/auth/signin'); return; } if (session?.user?.id) { fetchWatchlist(session.user.id); } }, [session, isPending, router]); const fetchWatchlist = async (userId: string) => { try { const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/watchlist/${userId}`); const data = await response.json(); setWatchlist(data); } catch (error) { console.error('Error fetching watchlist:', error); } finally { setLoading(false); } }; const handleAddStock = async (e: React.FormEvent) => { e.preventDefault(); const userId = session?.user?.id; if (!userId) return; try { await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/watchlist`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user_id: userId, ticker: newStock.ticker.toUpperCase(), company_name: newStock.company_name, sector: newStock.sector }) }); setShowAddModal(false); setNewStock({ ticker: '', company_name: '', sector: '' }); fetchWatchlist(userId); } catch (error) { console.error('Error adding stock:', error); } }; const handleDeleteStock = async (id: number) => { if (!confirm('Are you sure you want to remove this stock from watchlist?')) return; const userId = session?.user?.id; if (!userId) return; try { await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/watchlist/${id}`, { method: 'DELETE' }); fetchWatchlist(userId); } catch (error) { console.error('Error deleting stock:', error); } }; if (loading) { return
Loading...
; } return (

Watchlist

{watchlist.map((stock: any) => (

{stock.ticker}

{stock.company_name}

{stock.sector && (
{stock.sector}
)}
Filings Add to Portfolio
))} {watchlist.length === 0 && (

Your watchlist is empty

Add stocks to track their SEC filings and monitor performance

)}
{showAddModal && (

Add to Watchlist

setNewStock({...newStock, ticker: e.target.value})} className="w-full bg-slate-700 border border-slate-600 rounded-lg px-4 py-3 text-white" placeholder="AAPL" required />
setNewStock({...newStock, company_name: e.target.value})} className="w-full bg-slate-700 border border-slate-600 rounded-lg px-4 py-3 text-white" placeholder="Apple Inc." required />
setNewStock({...newStock, sector: e.target.value})} className="w-full bg-slate-700 border border-slate-600 rounded-lg px-4 py-3 text-white" placeholder="Technology" />
)}
); }