import { useLocation } from "wouter"; import { useAuth } from "@/hooks/use-auth"; import { useQuery, useMutation } from "@tanstack/react-query"; import { ordersApi } from "@/lib/api"; import { apiRequest, queryClient } from "@/lib/queryClient"; import Header from "@/components/layout/header"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Skeleton } from "@/components/ui/skeleton"; import { useToast } from "@/hooks/use-toast"; import { Package, ArrowLeft, Eye, RotateCcw, X } from "lucide-react"; import { useState } from "react"; export default function Orders() { const [, setLocation] = useLocation(); const { user } = useAuth(); const [searchQuery, setSearchQuery] = useState(""); const { toast } = useToast(); // Redirect if not authenticated if (!user) { setLocation('/auth'); return null; } const { data: orders = [], isLoading } = useQuery({ queryKey: ['/api/orders'], queryFn: async () => { const response = await ordersApi.get(); return response.json(); }, }); const getStatusColor = (status: string) => { switch (status.toLowerCase()) { case 'delivered': return 'default'; case 'shipped': return 'secondary'; case 'pending': return 'outline'; case 'cancelled': return 'destructive'; default: return 'outline'; } }; const handleTrackOrder = (orderId: string) => { // For now, just show an alert - in a real app you'd open a tracking modal or page alert(`Tracking order ${orderId.slice(0, 8)}...`); }; const handleReorder = (order: any) => { // For now, just navigate to home - in a real app you'd add items to cart alert('Items added to cart!'); setLocation('/'); }; const cancelOrderMutation = useMutation({ mutationFn: async (orderId: string) => { const response = await apiRequest('PATCH', `/api/orders/${orderId}/cancel`); return response.json(); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['/api/orders'] }); toast({ title: "Order Cancelled", description: "Your order has been successfully cancelled.", }); }, onError: (error: any) => { toast({ title: "Failed to Cancel Order", description: error.message || "Something went wrong. Please try again.", variant: "destructive", }); }, }); const handleCancelOrder = (orderId: string) => { if (confirm('Are you sure you want to cancel this order? This action cannot be undone.')) { cancelOrderMutation.mutate(orderId); } }; return (
{/* Back Button */}
{/* Header */}

My Orders

Track and manage your order history

{/* Orders List */} Order History {isLoading ? (
{[...Array(3)].map((_, i) => (
))}
) : orders.length > 0 ? (
{orders.map((order: any) => (
{/* Order Header */}

Order #{order.id.slice(0, 8).toUpperCase()}

Placed on {new Date(order.createdAt).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}

{order.status.charAt(0).toUpperCase() + order.status.slice(1)}
{/* Order Items */} {order.items && order.items.length > 0 && (
{order.items.map((item: any) => (
{item.product?.images && item.product.images.length > 0 ? ( {item.product.title} ) : (
)}

{item.product?.title || 'Unknown Product'}

Sold by {item.product?.store?.name || 'Unknown Seller'}

Qty: {item.quantity}

₹{parseFloat(item.price).toFixed(2)}

))}
)} {/* Order Summary */}
Total: ₹{parseFloat(order.total).toFixed(2)}

Payment: {order.paymentMethod === 'cod' ? 'Cash on Delivery' : 'UPI'}

{order.status === 'pending' && ( )}
))}
) : (

No orders yet

You haven't placed any orders yet. Start shopping to see your orders here!

)}
); }