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 (
Track and manage your order history
Placed on {new Date(order.createdAt).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}
Sold by {item.product?.store?.name || 'Unknown Seller'}
Qty: {item.quantity}
₹{parseFloat(item.price).toFixed(2)}
Payment: {order.paymentMethod === 'cod' ? 'Cash on Delivery' : 'UPI'}
You haven't placed any orders yet. Start shopping to see your orders here!