import React, { useState, useEffect } from 'react'; import { useParams, Link } from 'react-router-dom'; import { ArrowLeft } from 'lucide-react'; import { api } from '@/lib/api/client'; import { toast } from 'sonner'; interface OrderItem { id: number; name: string; quantity: number; total: string; image?: string; } interface ShippingLine { id: number; method_title: string; method_id: string; total: string; } interface Order { id: number; order_number: string; date: string; status: string; total: string; subtotal: string; shipping_total: string; tax_total: string; items: OrderItem[]; billing: any; shipping: any; payment_method_title: string; needs_shipping: boolean; shipping_lines?: ShippingLine[]; // Tracking info (may be added by shipping plugins) tracking_number?: string; tracking_url?: string; meta_data?: Array<{ key: string; value: string }>; } export default function OrderDetails() { const { orderId } = useParams(); const [loading, setLoading] = useState(true); const [order, setOrder] = useState(null); useEffect(() => { if (orderId) { loadOrder(); } }, [orderId]); const loadOrder = async () => { try { const data = await api.get(`/account/orders/${orderId}`); setOrder(data); } catch (error) { console.error('Load order error:', error); toast.error('Failed to load order details'); } finally { setLoading(false); } }; const getStatusColor = (status: string) => { const colors: Record = { 'completed': 'bg-green-100 text-green-800', 'processing': 'bg-blue-100 text-blue-800', 'pending': 'bg-yellow-100 text-yellow-800', 'on-hold': 'bg-orange-100 text-orange-800', 'cancelled': 'bg-red-100 text-red-800', 'refunded': 'bg-gray-100 text-gray-800', 'failed': 'bg-red-100 text-red-800', }; return colors[status] || 'bg-gray-100 text-gray-800'; }; const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', }); }; if (loading) { return (
Loading order...
); } if (!order) { return (
Back to Orders

Order not found

); } return (
Back to Orders

Order #{order.order_number}

{order.status.replace('-', ' ').toUpperCase()}
Placed on {formatDate(order.date)}
{/* Order Items */}

Order Items

{(order.items || []).map((item) => (
{item.image && ( {item.name} )}

{item.name}

Quantity: {item.quantity}

{item.total}
))}
{/* Order Summary */}

Order Summary

Subtotal {order.subtotal}
Shipping {order.shipping_total}
Tax {order.tax_total}
Total {order.total}
{/* Addresses */}

Billing Address

{order.billing && (

{order.billing.first_name} {order.billing.last_name}

{order.billing.company &&

{order.billing.company}

}

{order.billing.address_1}

{order.billing.address_2 &&

{order.billing.address_2}

}

{order.billing.city}, {order.billing.state} {order.billing.postcode}

{order.billing.country}

{order.billing.email &&

Email: {order.billing.email}

} {order.billing.phone &&

Phone: {order.billing.phone}

}
)}
{/* Only show shipping address if order needs shipping (not virtual-only) */} {order.needs_shipping && (

Shipping Address

{order.shipping && (

{order.shipping.first_name} {order.shipping.last_name}

{order.shipping.company &&

{order.shipping.company}

}

{order.shipping.address_1}

{order.shipping.address_2 &&

{order.shipping.address_2}

}

{order.shipping.city}, {order.shipping.state} {order.shipping.postcode}

{order.shipping.country}

)}
)}
{/* Payment Method */}

Payment Method

{order.payment_method_title || 'Not specified'}
{/* Shipping Method - only for physical product orders */} {order.needs_shipping && order.shipping_lines && order.shipping_lines.length > 0 && (

Shipping Method

{order.shipping_lines.map((line) => (
{line.method_title} {line.total}
))} {/* AWB Tracking - show for processing or completed orders */} {(order.status === 'processing' || order.status === 'completed') && (
{order.tracking_number ? (
Tracking Number {order.tracking_number}
{order.tracking_url ? ( Track Shipment ) : (

Use the tracking number above to track your shipment on your courier's website.

)}
) : (

Your order is being processed. Tracking information will be available once your order has been shipped.

)}
)}
)}
); }