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...
Failed to load
Order #{order.orderId || order.id}
{dayjs(order.orderDate || order.createdAt).format('DD MMM, h:mm A')}
{!!(order as any).regionName && (
⚡ 1 Hr Delivery
)}{orderStatusManipulator(statusConfig.label)}
⚡
1 Hr Delivery Order
Your order will be delivered within 1 hr of placement
{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')}`}
1 Hr Delivery: {dayjs(order.createdAt || order.orderDate).add(30, 'minutes').format('DD MMM YYYY, h:mm A')}
Special Instructions
{isEditingNotes ? (Cancellation Reason
{order.cancelReason}
Order Items
{item.productName}
{item.quantity}{item.unitNotation ? ` × ${item.unitNotation}` : ''} · ₹{item.price}
₹{item.amount}
{order.couponCode}
Coupon Applied
-₹{order.discountAmount}
Subtotal
₹{subtotal}
Discount
-₹{discountAmount}
Delivery
{deliveryCharge > 0 ? `₹${deliveryCharge}` : 'Free'}
Total Amount
₹{totalAmount}
Cancel Order
Are you sure you want to cancel this order? This action cannot be undone.
Reason for cancellation