/* eslint-disable @next/next/no-img-element */ /* eslint-disable jsx-a11y/role-supports-aria-props */ /* eslint-disable @typescript-eslint/no-explicit-any */ import Link from "next/link"; import React from "react"; // eslint-disable-next-line @typescript-eslint/no-unused-vars import { useRouter, useSearchParams } from "next/navigation"; import { amountFormatWithCurrency, cryptoHandler, formatDate, getFormattedAddress, getOrderStatusInfo, } from "../../../lib/Helper"; import { getOrderDetails, downloadInvoice } from "../../../Api/OrderApi"; import HourGlassLoader from "../../Loader/Loader"; import { toast } from "react-toastify"; import { OrderFlowType, UserTypes } from "@/Constant/enums"; interface OrderPackage { id: string; qrCode: string; generatedOrderPackageId: string; weight: number; packageDescription: string; customerInputPackageValue: string; status: { id: number; name: string; }; } interface OrderData { id: string; generatedOrderId: string; destinationCustomerName: string; destinationMobileNumber: string; destinationAlternateNumber: string; destination_block_details?: { name: string }; destination_road_details?: { name: string }; destination_building_details?: { name: string }; destinationFlatOrOfficeNumber: string; orderType: { name: string }; status: { id: number }; barCode: string; calculatedTotalShippingCharge: number; order_package_list: OrderPackage[]; totalWeight: number; pickupDate: string; deliveryInstructions: string; codAmount: number; } const OrderViewIndex = () => { // const router = useRouter(); const searchParams = useSearchParams(); const [orderData, setOrderData] = React.useState({}); const [isLoading, setIsLoading] = React.useState(true); const [error, setError] = React.useState(null); const [packageShow, setPackageShow] = React.useState(false); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [isDownloading, setIsDownloading] = React.useState(false); const togglePackageShow = () => setPackageShow(!packageShow); React.useEffect(() => { const fetchOrderDetails = async () => { try { const encryptedId = searchParams?.get("id"); if (!encryptedId) { setError("No order ID found in URL"); setIsLoading(false); return; } const decryptedId = cryptoHandler(encryptedId, "decrypt"); if (!decryptedId) { setError("Invalid order ID"); setIsLoading(false); return; } const orderDetails = await getOrderDetails(decryptedId); if (orderDetails && typeof orderDetails === "object") { setOrderData(orderDetails as unknown as OrderData); } else { setError("Invalid order data received"); } } catch (error) { console.error("Error fetching order details:", error); setError("Failed to load order details. Please try again."); } finally { setIsLoading(false); } }; fetchOrderDetails(); }, [searchParams]); const storedUser = localStorage.getItem("ALL_DATA"); const allData = storedUser ? JSON.parse(storedUser)?.data : null; // const handleDeletePackage = async (packageId: string) => { // if (!packageId) { // toast.error("Invalid package ID"); // return; // } // Swal.fire({ // title: "Are you sure?", // text: "Do you really want to delete this package?", // icon: "warning", // showCancelButton: true, // confirmButtonText: "Yes, delete it!", // cancelButtonText: "Cancel", // reverseButtons: true, // customClass: { // confirmButton: "delybell-primary px-4", // cancelButton: "delybell-dark", // }, // }).then(async (response) => { // if (response?.isConfirmed) { // try { // await deletePackage(packageId); // toast.success("Package deleted successfully"); // if (orderData?.id) { // const updatedOrder = await getOrderHistroy(orderData.id); // if (updatedOrder && typeof updatedOrder === "object") { // setOrderData(updatedOrder as unknown as OrderData); // } // } // } catch (error: any) { // console.error("Error deleting package:", error); // toast.error( // error?.response?.data?.message || // "Failed to delete package. Please try again." // ); // } // } // }); // }; // eslint-disable-next-line @typescript-eslint/no-unused-vars const handleDownloadPdf = async () => { try { if (!orderData?.invoice_details?.id) { toast.error("Order ID is not available."); return; } setIsDownloading(true); const response = await downloadInvoice(orderData?.invoice_details?.id); const url = window.URL.createObjectURL(new Blob([response])); const link = document.createElement("a"); link.href = url; link.setAttribute( "download", `invoice-${orderData.generatedOrderId}.pdf` ); document.body.appendChild(link); link.click(); link.parentNode?.removeChild(link); toast.success("Invoice downloaded successfully"); } catch (error: any) { console.error("Error downloading invoice:", error); toast.error( error?.response?.data?.message || "Failed to download invoice. Please try again." ); } finally { setIsDownloading(false); } }; if (isLoading) { return ; } if (error) { return (
{error}
Back to Orders
); } if (!orderData) { return (
No order data found
Back to Orders
); } const handleEdit = (orderDetail: any) => { const url = `/create-order?mode=edit_order&orderId=${ orderDetail?.id }&customerName=${encodeURIComponent( orderDetail?.destinationCustomerName || "" )}`; window.location.href = url; }; // const handleEditPackages = (packageId: any, orderDetail: any) => { // const url = `/create-order?mode=edit_order&orderId=${ // orderDetail?.id // }&customerName=${encodeURIComponent( // orderDetail?.destinationCustomerName || "" // )}`; // window.location.href = url; // }; const handlePrintPackage = (package_id: number) => { http://localhost:3000/address-print?id=&pkg=78 // window.open(`/address-print?pkg=${cryptoHandler(package_id, "encrypt")}`, "_blank"); window.open(`/address-print?id=${orderData?.id}&pkg=${package_id}`, "_blank"); }; return ( <>
{orderData?.generatedOrderId}