From 2715d3a0e73676060c51aa0ebcb8cc7e73ce2acd Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Tue, 18 Mar 2025 18:19:24 +0100 Subject: [PATCH] fix --- components/dashboard/BuyerOrderInfo.tsx | 167 ++++++++++++++++++++++++ components/dashboard/ChatDetail.tsx | 31 +++-- 2 files changed, 185 insertions(+), 13 deletions(-) create mode 100644 components/dashboard/BuyerOrderInfo.tsx diff --git a/components/dashboard/BuyerOrderInfo.tsx b/components/dashboard/BuyerOrderInfo.tsx new file mode 100644 index 0000000..a88e78f --- /dev/null +++ b/components/dashboard/BuyerOrderInfo.tsx @@ -0,0 +1,167 @@ +"use client"; + +import React, { useState, useEffect } from "react"; +import { Package, ShoppingBag, Info, ExternalLink } from "lucide-react"; +import { Badge } from "@/components/ui/badge"; +import { Button } from "@/components/ui/button"; +import { + Tooltip, + TooltipContent, + TooltipProvider, + TooltipTrigger, +} from "@/components/ui/tooltip"; +import { getCookie } from "@/lib/client-utils"; +import axios from "axios"; +import { useRouter } from "next/navigation"; + +interface Order { + _id: string; + orderId: number; + status: string; + totalPrice: number; + orderDate: string; + products: Array<{ + productId: string; + quantity: number; + pricePerUnit: number; + totalItemPrice: number; + }>; +} + +interface BuyerOrderInfoProps { + buyerId: string; +} + +export default function BuyerOrderInfo({ buyerId }: BuyerOrderInfoProps) { + const router = useRouter(); + const [loading, setLoading] = useState(true); + const [orders, setOrders] = useState([]); + + useEffect(() => { + // Only load if we have a buyerId + if (!buyerId) return; + + const fetchBuyerOrders = async () => { + try { + const authToken = getCookie("Authorization"); + + if (!authToken) return; + + const authAxios = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_URL, + headers: { + Authorization: `Bearer ${authToken}` + } + }); + + // Fetch orders for this specific buyer + const response = await authAxios.get(`/orders/buyer/${buyerId}?limit=5`); + + if (response.data && response.data.orders) { + setOrders(response.data.orders); + } + + setLoading(false); + } catch (error) { + console.error("Error fetching buyer orders:", error); + setLoading(false); + } + }; + + fetchBuyerOrders(); + }, [buyerId]); + + const handleViewOrder = (orderId: string) => { + router.push(`/dashboard/orders/${orderId}`); + }; + + // Format the price as currency + const formatPrice = (price: number) => { + return `£${price.toFixed(2)}`; + }; + + // Don't show anything while loading or if no orders + if (loading || orders.length === 0) { + return null; + } + + // Count products across all orders + const productCount = orders.reduce((total, order) => { + return total + order.products.reduce((sum, product) => sum + product.quantity, 0); + }, 0); + + return ( + + + + + + +
+
+ Recent Orders from this Customer + +
+
+ {orders.map((order) => ( +
handleViewOrder(order._id)} + > +
+
+ + Order #{order.orderId} +
+ + {order.status.toUpperCase()} + +
+ +
+
+ {order.products.length} {order.products.length === 1 ? 'product' : 'products'} + · + {formatPrice(order.totalPrice)} +
+ {new Date(order.orderDate).toLocaleDateString()} +
+
+ ))} +
+
+ +
+
+
+
+
+ ); +} \ No newline at end of file diff --git a/components/dashboard/ChatDetail.tsx b/components/dashboard/ChatDetail.tsx index e0ef055..b902bf4 100644 --- a/components/dashboard/ChatDetail.tsx +++ b/components/dashboard/ChatDetail.tsx @@ -13,6 +13,7 @@ import { toast } from "sonner"; import { ArrowLeft, Send, RefreshCw, File, FileText, Image as ImageIcon, Download } from "lucide-react"; import { getCookie } from "@/lib/client-utils"; import { ImageViewerModal } from "@/components/modals/image-viewer-modal"; +import BuyerOrderInfo from "./BuyerOrderInfo"; interface Message { _id: string; @@ -381,20 +382,24 @@ export default function ChatDetail({ chatId }: { chatId: string }) { return (
-
- -
-

- Chat with Customer {chat.buyerId.slice(-4)} -

- {chat.telegramUsername && ( - - @{chat.telegramUsername} - - )} +
+
+ +
+

+ Chat with Customer {chat.buyerId.slice(-4)} +

+ {chat.telegramUsername && ( + + @{chat.telegramUsername} + + )} +
+ +