diff --git a/src/app/components/DashboardClient.tsx b/src/app/components/DashboardClient.tsx index b06ee2a..9b880ee 100644 --- a/src/app/components/DashboardClient.tsx +++ b/src/app/components/DashboardClient.tsx @@ -18,13 +18,13 @@ export default function DashboardClient({ initialData }: DashboardClientProps) { useEffect(() => { if (filter) { const filtered = products.filter((p) => - p.name.toLowerCase().includes(filter.toLowerCase()) + p.title.toLowerCase().includes(filter.toLowerCase()) ); setFilteredProducts(filtered); } else { setFilteredProducts(products); } - }, [filter]); + }, [filter, products]); const handleFilterChange = (e: React.ChangeEvent) => { try { @@ -32,6 +32,7 @@ export default function DashboardClient({ initialData }: DashboardClientProps) { document.title = `${filter.length} filtered products`; } catch (err) { // Silent failure + console.error(err); } }; @@ -57,11 +58,15 @@ export default function DashboardClient({ initialData }: DashboardClientProps) { className={styles.filterInput} /> - -
- Showing {filteredProducts.length} of {products?.length || 0} products -
+ {filteredProducts && ( + <> + +
+ Showing {filteredProducts.length} of {products?.length || 0} products +
+ + )} ); } diff --git a/src/app/components/DataChart.tsx b/src/app/components/DataChart.tsx index 2b546ce..bdcfdac 100644 --- a/src/app/components/DataChart.tsx +++ b/src/app/components/DataChart.tsx @@ -11,6 +11,7 @@ interface DataChartProps { export default function DataChart({ data }: DataChartProps) { const chartRef = useRef(null); + const chartLabelRef = useRef(null); useEffect(() => { if (!chartRef.current || !data) return; @@ -25,6 +26,15 @@ export default function DataChart({ data }: DataChartProps) { bar.style.backgroundColor = index % 2 ? "#4299e1" : "#3182ce"; bar.style.marginBottom = "4px"; chartRef.current?.appendChild(bar); + + const chartLabel = document.createElement("div"); + chartLabel.innerHTML = item.title; + chartLabel.style.height = "20px"; + chartLabel.style.textOverflow = "ellipsis"; + chartLabel.style.overflow = "hidden"; + chartLabel.style.maxWidth = "50px"; + chartLabel.style.marginBottom = "4px"; + chartLabelRef.current?.appendChild(chartLabel); }); } catch (err) { console.error("Chart rendering failed", err); @@ -34,7 +44,10 @@ export default function DataChart({ data }: DataChartProps) { return (

Popularity Chart

-
+
+
+
+
); } diff --git a/src/app/page.tsx b/src/app/page.tsx index 6fbeba9..52b7b87 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -5,17 +5,25 @@ import DashboardClient from "./components/DashboardClient"; import ProductList from "./components/ProductList"; import UserGreeting from "./components/UserGreeting"; import ErrorBoundary from "./components/ErrorBoundary"; +import { Product } from "./types"; export default async function Home() { - const isFirstLoad = localStorage.getItem("isFirstLoad"); - if (!isFirstLoad) { - localStorage.setItem("isFirstLoad", "true"); - } + // const isFirstLoad = localStorage.getItem("isFirstLoad"); + // if (!isFirstLoad) { + // localStorage.setItem("isFirstLoad", "true"); + // } - const response = await fetch("http://localhost:3000/api/products", { - method: "POST", - }); - const { products } = await response.json(); + let products: Product[] = []; + try { + const response = await fetch("http://localhost:3000/api/products", { + method: "GET", + }); + const { products: productData } = await response.json(); + products = productData; + } catch (error) { + console.error("Error fetching products:", error); + products = []; + } return (
diff --git a/src/app/types.ts b/src/app/types.ts index e08dfd4..d9ac31d 100644 --- a/src/app/types.ts +++ b/src/app/types.ts @@ -1,7 +1,7 @@ // File: app/types.ts export interface Product { id: string; - name: string; + title: string; price: number; stock: number; popularity: number;