import React, { useEffect, useState } from 'react'; import { IonButton, IonButtons, IonContent, IonHeader, IonIcon, IonList, IonPage, IonRefresher, IonRefresherContent, IonText, IonToolbar, useIonViewWillEnter, } from '@ionic/react'; import { chevronBackOutline, chevronForwardOutline, receiptOutline, } from 'ionicons/icons'; import { RefresherEventDetail } from '@ionic/core'; import { useHistory } from 'react-router-dom'; import { supabase } from '../supabase'; import type { Tables } from '../database.types'; import { useAuth } from '../context/AuthContext'; import { setStatusBarStyle, Style } from '../utils/statusBar'; type Order = Tables<'orders'>; const statusCopy: Record = { confirmed: 'Confirmed', packed: 'Packed', out_for_delivery: 'Out for delivery', delivered: 'Delivered', }; const MyOrders: React.FC = () => { const history = useHistory(); const { user } = useAuth(); const [orders, setOrders] = useState([]); const [isLoading, setIsLoading] = useState(true); const [errorMessage, setErrorMessage] = useState(''); useIonViewWillEnter(() => { void setStatusBarStyle(Style.Dark); }); const loadOrders = async () => { if (!user) { setOrders([]); setErrorMessage(''); setIsLoading(false); return; } try { setErrorMessage(''); const { data, error } = await supabase .from('orders') .select('*') .eq('user_id', user.id) .order('created_at', { ascending: false }); if (error) { throw error; } setOrders(data ?? []); } catch (error) { console.error('Failed to load orders', error); setOrders([]); setErrorMessage('We could not load your orders right now.'); window.setTimeout(() => setErrorMessage(''), 4000); } finally { setIsLoading(false); } }; useEffect(() => { if (!user) { setOrders([]); setErrorMessage(''); setIsLoading(false); return; } setIsLoading(true); void loadOrders(); }, [user?.id]); const handleRefresh = async (event: CustomEvent) => { await loadOrders(); event.detail.complete(); }; return (
history.replace('/home')} >
Tracking My Orders
{errorMessage && (

{errorMessage}

)} {!user ? (

No tracked orders yet

Sign in later to sync orders, or place an order now and track it from the confirmation screen.

history.push('/home')}> Continue shopping
) : isLoading ? (

Loading your orders…

We are fetching your latest order updates.

) : orders.length === 0 ? (

No orders yet

Your placed orders will appear here so you can track their status.

history.push('/home')}> Start shopping
) : ( {orders.map((order) => ( ))} )}
); }; export default MyOrders;