import { useState, useEffect } from 'react' import { useNavigate, Link } from 'react-router-dom' import Navbar from '../components/Navbar' import Footer from '../components/Footer' import { API_BASE, type Category } from '../types' import { Search, MapPin, Package, Loader2, ChevronRight } from 'lucide-react' export default function TrackOrderPage() { const navigate = useNavigate() const [categories, setCategories] = useState([]) const [localOrders, setLocalOrders] = useState([]) const [orderId, setOrderId] = useState('') const [phone, setPhone] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { fetch(`${API_BASE}/categories`) .then(res => res.json()) .then(setCategories) .catch(console.error) // Load local orders const storedStr = localStorage.getItem('homaroma_local_orders') if (storedStr) { setLocalOrders(JSON.parse(storedStr)) } }, []) const handleTrackForm = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError(null) try { const res = await fetch(`${API_BASE}/orders/track`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ orderId: parseInt(orderId.replace(/[^0-9]/g, ''), 10) || 0, phone }) }) const data = await res.json() if (res.ok && data.success) { // Automatically save to local history so they don't have to type it again if (!localOrders.includes(data.orderId)) { const newOrders = [data.orderId, ...localOrders] setLocalOrders(newOrders) localStorage.setItem('homaroma_local_orders', JSON.stringify(newOrders)) } navigate(`/order/${data.orderId}`) } else { setError(data.error || 'Failed to authenticate order details.') } } catch (err) { console.error(err) setError('An error occurred. Please check your internet connection.') } finally { setLoading(false) } } return (
{/* Header */}

Track Your Status

Enter your order details below to check shipping status or upload payment slips.

{/* Tracking Form */}

Manual Search

{error && (
{error}
)}
setOrderId(e.target.value)} className="w-full bg-night-950 border border-white/10 rounded-lg p-3.5 text-white focus:border-cyan-glow focus:ring-1 focus:ring-cyan-glow outline-none transition-all placeholder:text-slate-600" placeholder="e.g. 1024" />
setPhone(e.target.value)} className="w-full bg-night-950 border border-white/10 rounded-lg p-3.5 text-white focus:border-cyan-glow focus:ring-1 focus:ring-cyan-glow outline-none transition-all placeholder:text-slate-600" placeholder="The phone number used during checkout" />
{/* Local Order History */}

Recent Device Orders

{localOrders.length === 0 ? (

No recent orders found on this device.

Use manual search if you ordered elsewhere

) : (
{localOrders.map((id) => (

Order

#{id.toString().padStart(4, '0')}

))}
)}
) }