/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @next/next/no-img-element */ import Link from "next/link"; import { useSearchParams } from "next/navigation"; import { cryptoHandler, formatDate, formatTime, getFormattedAddress, getOrderStatusInfo, truncateWithTooltip, } from "../../../lib/Helper"; import React, { useState } from "react"; import { getOrderHistroy } from "../../../Api/OrderApi"; import HourGlassLoader from "../../Loader/Loader"; import { OrderFlowType } from "@/Constant/enums"; export default function OrderTracking() { const storedUser = localStorage.getItem("ALL_DATA"); const allData = storedUser ? JSON.parse(storedUser)?.data : null; const searchParams: any = useSearchParams(); const [orderData, setOrderData] = React.useState({}); const [isLoading, setIsLoading] = React.useState(true); const [orderId, setOrderId] = useState(null); React.useEffect(() => { const fetchOrderDetails = async () => { try { const encryptedId: any = searchParams.get("id") || ""; if (!encryptedId) { console.error("No id found in URL"); setIsLoading(false); return; } const decryptedId = cryptoHandler(encryptedId, "decrypt"); if (!decryptedId) { console.error("Decryption failed"); setIsLoading(false); return; } const orderDetails = await getOrderHistroy(decryptedId); setOrderId(decryptedId); setOrderData(orderDetails); } catch (error) { console.error("Error fetching order details:", error); } finally { setIsLoading(false); } }; fetchOrderDetails(); }, [searchParams]); const timelineStages = [ { name: "Order Placed", statusIds: [1], // OrderPlaced description: "Order has been placed successfully", }, { name: "Order Picked", statusIds: [2], // OrderPicked description: "Order has been picked up from customer", }, { name: "At Warehouse", statusIds: [3], // AtWarehouse description: "Order is at warehouse", }, { name: "In Transit", statusIds: [9], // InTransitToWarehouse description: "Order is in transit to destination warehouse", }, { name: "At Destination Warehouse", statusIds: [10], // AtDestinationWarehouse description: "Order is at destination warehouse", }, { name: "Out For Delivery", statusIds: [4], // OutForDeliveryFromWarehouse, OutForDelivery description: "Order is out for delivery", }, { name: "Exception", statusIds: [6], // ExceptionOccurred, Exception description: "Exception occurred during delivery", }, { name: "Out For Delivery After Exception", statusIds: [7], // OutForDeliveryAfterException description: "Order is out for delivery after resolving exception", }, { name: "Delivered", statusIds: [5, 8], // DeliveredAfterException, Delivered description: "Order has been delivered successfully", }, ]; // For non-transit orders (simpler flow) const nonTransitTimelineStages = [ { name: "Order Placed", statusIds: [1], // OrderPlaced description: "Order has been placed successfully", }, { name: "Order Picked", statusIds: [2], // OrderPicked description: "Order has been picked up from customer", }, { name: "At Warehouse", statusIds: [3], // AtWarehouse description: "Order is at warehouse", }, { name: "Out For Delivery", statusIds: [4], // OutForDelivery description: "Order is out for delivery", }, { name: "Exception", statusIds: [6], // ExceptionOccurred, Exception description: "Exception occurred during delivery", }, { name: "Delivered", statusIds: [5], // Delivered description: "Order has been delivered successfully", }, ]; // Determine if this is a transit order based on order history const isTransitOrder = orderData?.order_history?.some((h: any) => [15, 16, 17, 18, 19, 20].includes(h.action.id) ) || false; const currentStatusId = orderData?.status?.id; const isCancelled = currentStatusId === 12; // Cancelled status // Use appropriate timeline based on order type const activeTimelineStages = isTransitOrder ? timelineStages : nonTransitTimelineStages; return ( <> {isLoading && }
Order Tracking