diff --git a/app/dashboard/orders/[id]/page.tsx b/app/dashboard/orders/[id]/page.tsx index 51a0cba..b9b2f05 100644 --- a/app/dashboard/orders/[id]/page.tsx +++ b/app/dashboard/orders/[id]/page.tsx @@ -1,122 +1,221 @@ -"use client" +"use client"; -import { useParams } from "next/navigation" -import { useState } from "react" -import Layout from "@/components/kokonutui/layout" -import { Button } from "@/components/ui/button" -import { Input } from "@/components/ui/input" -import { ArrowLeft, Truck, DollarSign, Package, User } from "lucide-react" -import Link from "next/link" +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 OrderDetails() { - const params = useParams() - const orderId = params.id +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 - // In a real application, you would fetch the order details based on the orderId - const [trackingNumber, setTrackingNumber] = useState("") - const order = { - id: orderId, - customer: "John Doe", - email: "john.doe@example.com", - date: "2023-05-01", - total: "$150.00", - status: "Processing", - tracking: "", - items: [ - { id: 1, name: "Product A", quantity: 2, price: "$50.00" }, - { id: 2, name: "Product B", quantity: 1, price: "$50.00" }, - ], - shippingAddress: "123 Main St, Anytown, AN 12345", - } + const params = useParams(); + const orderId = params.id; - const handleSaveTracking = () => { - // TODO: Implement API call to save tracking number - console.log("Tracking number saved:", trackingNumber) - } + 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.id}

- - - +
+

Order Details: {order.orderId}

+ + {order.status} +
-
-
-

- - Customer Information -

-

Name: {order.customer}

-

Email: {order.email}

-

Shipping Address: {order.shippingAddress}

-
- -
-

- - Order Summary -

-

Order Date: {order.date}

-

Total: {order.total}

-

Status: {order.status}

- - {/* Tracking Input Field */} -
-

Tracking Number:

- setTrackingNumber(e.target.value)} - className="mt-2" - /> -