"use client"; import { useEffect, useState } from "react"; import { useParams } from "next/navigation"; import Layout from "@/components/kokonutui/layout"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Clipboard, Package, } from "lucide-react"; export default function OrderDetailsPage() { const [order, setOrder] = useState(null); const [trackingNumber, setTrackingNumber] = useState(""); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const [productNames, setProductNames] = useState>({}); // Map of productId to productName const params = useParams(); const orderId = params.id; useEffect(() => { const fetchOrderDetails = async () => { try { if (!orderId) return; const authToken = document.cookie.split("Authorization=")[1]; const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/orders/${orderId}`, { method: "GET", headers: { Authorization: `Bearer ${authToken}` }, }); if (!res.ok) throw new Error("Failed to fetch order details"); const data = await res.json(); setOrder(data); // Fetch product names for the order const productIds = data.products.map((product: any) => product.productId); const productNamesMap = await fetchProductNames(productIds, authToken); setProductNames(productNamesMap); } catch (err: any) { setError(err.message); } finally { setLoading(false); } }; const fetchProductNames = async (productIds: string[], authToken: string) => { const productNamesMap: Record = {}; try { const promises = productIds.map((id) => fetch(`${process.env.NEXT_PUBLIC_API_URL}/products/${id}`, { method: "GET", headers: { Authorization: `Bearer ${authToken}` }, }) ); const responses = await Promise.all(promises); const results = await Promise.all(responses.map((res) => res.json())); results.forEach((product, index) => { productNamesMap[productIds[index]] = product.name || "Unknown Product"; }); } catch (err) { console.error("Failed to fetch product names:", err); } return productNamesMap; }; fetchOrderDetails(); }, [orderId]); const handleAddTracking = async () => { if (!trackingNumber) return; try { const authToken = document.cookie.split("Authorization=")[1]; const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/orders/${orderId}/tracking`, { method: "PUT", headers: { "Content-Type": "application/json", Authorization: `Bearer ${authToken}`, }, body: JSON.stringify({ trackingNumber }), }); if (!res.ok) throw new Error("Failed to update tracking number"); console.log("Tracking number updated successfully!"); } catch (err: any) { console.error(err.message); } }; const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text); }; if (loading) return
Loading order details...
; if (error) return
Error: {error}
; return (

Order Details: {order.orderId}

{order.status}
PGP Encrypted Address Securely encrypted delivery address