From 717451ac9f5a9f79955b625e2a2ab9aae524aa44 Mon Sep 17 00:00:00 2001 From: g Date: Fri, 7 Feb 2025 13:50:15 +0000 Subject: [PATCH] Update frontend --- app/dashboard/orders/[id]/page.tsx | 146 ++++++++++++++++++++++------- components/ui/badge.tsx | 24 ++--- models/products.ts | 1 - 3 files changed, 123 insertions(+), 48 deletions(-) diff --git a/app/dashboard/orders/[id]/page.tsx b/app/dashboard/orders/[id]/page.tsx index b9b2f05..f205cd2 100644 --- a/app/dashboard/orders/[id]/page.tsx +++ b/app/dashboard/orders/[id]/page.tsx @@ -24,18 +24,63 @@ import { CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; -import { Clipboard, Package, } from "lucide-react"; +import { Clipboard, Truck, Package } from "lucide-react"; + +interface Order { + orderId: string; + status: string; + pgpAddress: string; + shippingMethod: { type: string; price: number }; + products: Array<{ + _id: string; + productId: string; + quantity: number; + pricePerUnit: number; + totalItemPrice: number; + }>; + totalPrice: number; +} export default function OrderDetailsPage() { - const [order, setOrder] = useState(null); + 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 [productNames, setProductNames] = useState>({}); + const [isPaid, setIsPaid] = useState(false); const params = useParams(); - const orderId = params.id; + const orderId = params?.id; + + const handleMarkAsPaid = async () => { + try { + const authToken = document.cookie.split("Authorization=")[1]; + const response = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/orders/${orderId}`, + { + method: "PUT", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${authToken}`, + }, + body: JSON.stringify({ status: "paid" }), + } + ); + + if (response.ok) { + setIsPaid(true); // Update isPaid state + setOrder((prevOrder) => (prevOrder ? { ...prevOrder, status: "paid" } : null)); // Update order status + console.log("Order marked as paid successfully."); + } else { + const errorData = await response.json(); + console.error("Failed to mark order as paid:", errorData.message); + alert(`Error: ${errorData.message}`); + } + } catch (error: any) { + console.error("An error occurred while marking the order as paid:", error.message); + alert("An unexpected error occurred. Please try again."); + } + }; useEffect(() => { const fetchOrderDetails = async () => { @@ -44,20 +89,26 @@ export default function OrderDetailsPage() { 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}` }, - }); + 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(); + const data: Order = await res.json(); setOrder(data); - // Fetch product names for the order - const productIds = data.products.map((product: any) => product.productId); + const productIds = data.products.map((product) => product.productId); const productNamesMap = await fetchProductNames(productIds, authToken); setProductNames(productNamesMap); + + if (data.status === "paid") { + setIsPaid(true); + } } catch (err: any) { setError(err.message); } finally { @@ -65,7 +116,10 @@ export default function OrderDetailsPage() { } }; - const fetchProductNames = async (productIds: string[], authToken: string) => { + const fetchProductNames = async ( + productIds: string[], + authToken: string + ): Promise> => { const productNamesMap: Record = {}; try { const promises = productIds.map((id) => @@ -89,21 +143,23 @@ export default function OrderDetailsPage() { 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 }), - }); + 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"); @@ -117,30 +173,42 @@ export default function OrderDetailsPage() { navigator.clipboard.writeText(text); }; - if (loading) return
Loading order details...
; - if (error) return
Error: {error}
; + if (loading) + return
Loading order details...
; + if (error) + return
Error: {error}
; return (
-

Order Details: {order.orderId}

- - {order.status} +

Order Details: {order?.orderId}

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