diff --git a/src/components/orders/OrderRow.tsx b/src/components/orders/OrderRow.tsx index 11c4773..f33ccbd 100644 --- a/src/components/orders/OrderRow.tsx +++ b/src/components/orders/OrderRow.tsx @@ -1,7 +1,9 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import { type OrderDetailsResponse, type OrderItemDetails } from "../../types/orders.ts"; import { getStatusColor, getStatusLabel } from "../../types/orders.ts"; import { formatDate } from "../../utils/string.ts"; +import { getAllVariantDetails } from "../../api/product-service"; +import type { GetVariantResponseDTO } from "../../types/products"; import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown"; import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp"; import { @@ -18,6 +20,8 @@ import { TableCell, TableRow, Typography, + CircularProgress, + Avatar, } from "@mui/material"; const calculateTotalPrice = (items: OrderItemDetails[]): number => { @@ -62,6 +66,34 @@ interface OrderItemRowProps { } const OrderItemRow: React.FC = ({ item, index }) => { + const [variantData, setVariantData] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + useEffect(() => { + const fetchVariantData = async () => { + try { + setLoading(true); + setError(null); + const data = await getAllVariantDetails(item.variantId); + setVariantData(data); + } catch (err) { + console.error("Error fetching variant details:", err); + setError("Failed to load product details"); + } finally { + setLoading(false); + } + }; + + if (item.variantId) { + fetchVariantData(); + } + }, [item.variantId]); + + const productName = variantData?.variant?.name || `Product #${index + 1}`; + const variantImages = variantData?.variant?.variantImages || []; + const firstImage = variantImages.length > 0 ? variantImages[0].url : null; + return ( = ({ item, index }) => { px: 2, }} > - - - Product #{index + 1} - - + + + Loading product details... + + + ) : error ? ( + + + {error} + + + ) : ( + <> + {firstImage && ( + { + const target = e.target as HTMLImageElement; + target.style.display = "none"; + }, + }} /> - {item.isReturnable && ( - - )} - - } - secondary={ - - - Product ID: {item.itemId} + )} + + + {productName} + + + {item.isReturnable && ( + + )} + + } + secondary={ + + + Product ID: {item.itemId} + + + Variant ID: {item.variantId} + + {item.description && ( + + Description: {item.description} + + )} + + } + /> + + + {(item.price * item.quantity).toFixed(2)} PLN - - Variant ID: {item.variantId} + + {item.price.toFixed(2)} PLN / pcs. - {item.description && ( - - Description: {item.description} - - )} - } - /> - - - {(item.price * item.quantity).toFixed(2)} PLN - - - {item.price.toFixed(2)} PLN / pcs. - - + + )} );