import { createFileRoute, useNavigate, useParams } from '@tanstack/react-router' import { trpc } from '../lib/trpc-client' import { useState, useEffect } from 'react' import { ChevronLeft, CreditCard, CheckCircle, Zap, Edit2, Tag, XCircle, X, AlertCircle, Loader2, MapPin } from 'lucide-react' import { BottomDialog } from 'web-components' import ComplaintForm from '../components/ComplaintForm' import { orderStatusManipulator } from '../lib/string-manipulators' import dayjs from 'dayjs' export const Route = createFileRoute('/me/orders/$id')({ component: OrderDetailPage, }) function OrderDetailPage() { const { id } = useParams({ from: '/me/orders/$id' }) const navigate = useNavigate() const { data: orderData, isLoading, error, refetch, } = trpc.user.order.getOrderById.useQuery( { orderId: id }, { enabled: !!id } ) const [isEditingNotes, setIsEditingNotes] = useState(false) const [notesText, setNotesText] = useState('') const [notesInput, setNotesInput] = useState('') const [cancelDialogOpen, setCancelDialogOpen] = useState(false) const [cancelReason, setCancelReason] = useState('') const [complaintDialogOpen, setComplaintDialogOpen] = useState(false) const updateNotesMutation = trpc.user.order.updateUserNotes.useMutation({ onSuccess: () => { setNotesText(notesInput) setIsEditingNotes(false) refetch() alert('Notes updated successfully') }, onError: (error: any) => { alert(error.message || 'Failed to update notes') }, }) const cancelOrderMutation = trpc.user.order.cancelOrder.useMutation({ onSuccess: () => { setCancelDialogOpen(false) setCancelReason('') refetch() alert('Order cancelled successfully') }, onError: (error: any) => { alert(error.message || 'Failed to cancel order') }, }) const handleCancelOrder = () => { if (!order) return if (!cancelReason.trim()) { alert('Please enter a reason for cancellation') return } cancelOrderMutation.mutate({ id: order.id, reason: cancelReason }) } useEffect(() => { if (orderData?.userNotes) { setNotesText(orderData.userNotes) setNotesInput(orderData.userNotes) } }, [orderData]) if (isLoading) { return (

Loading details...

) } if (error || !orderData) { return (

Failed to load

) } const order = orderData const orderAny = order as any const getStatusConfig = (status: string) => { const s = status.toLowerCase() switch (s) { case 'delivered': case 'success': return { label: 'Delivered', color: '#10B981' } case 'cancelled': case 'failed': return { label: 'Cancelled', color: '#EF4444' } case 'pending': case 'processing': return { label: 'Pending', color: '#F59E0B' } default: return { label: status, color: '#1570EF' } } } const subtotal = order.items.reduce((sum: number, item: any) => sum + item.amount, 0) const discountAmount = order.discountAmount || 0 const deliveryCharge = orderAny.deliveryCharge || 0 const totalAmount = order.orderAmount const statusConfig = getStatusConfig(order.deliveryStatus || 'pending') return (
{/* Header */}

Order #{order.orderId || order.id}

{dayjs(order.orderDate || order.createdAt).format('DD MMM, h:mm A')}

{!!(order as any).regionName && (

{(order as any).regionName}

)} {order.isFlashDelivery && (

⚡ 1 Hr Delivery

)}

{orderStatusManipulator(statusConfig.label)}

{order.isFlashDelivery && (

⚡

)}
{/* 1 Hr Delivery Banner */} {order.isFlashDelivery && (

1 Hr Delivery Order

Your order will be delivered within 1 hr of placement

)} {/* Main Info Card */}
{(order.deliveryDate || order.isFlashDelivery) && ['delivered', 'success'].includes((order.deliveryStatus || '').toLowerCase()) && (
{order.isFlashDelivery ? ( ) : ( )}

{order.isFlashDelivery ? `Flash Delivered on ${dayjs(order.createdAt || order.orderDate).add(30, 'minutes').format('DD MMM YYYY, h:mm A')}` : `Delivered on ${dayjs(order.deliveryDate).format('DD MMM YYYY, h:mm A')}`}

)} {order.isFlashDelivery && !['delivered', 'success'].includes((order.deliveryStatus || '').toLowerCase()) && (

1 Hr Delivery: {dayjs(order.createdAt || order.orderDate).add(30, 'minutes').format('DD MMM YYYY, h:mm A')}

)}
{/* Special Instructions */}

Special Instructions

{isEditingNotes ? (
) : ( )}
{isEditingNotes ? (