-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremainpage.jsx
More file actions
85 lines (80 loc) · 3.71 KB
/
remainpage.jsx
File metadata and controls
85 lines (80 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React, { useState, useEffect } from "react";
import filter from "./filter.jpg";
import "./remainpage.css";
const RemainPage = () => {
const [showDropdown, setShowDropdown] = useState(false);
const [productData, setProductData] = useState([]);
// Fetch updated products from the backend
const fetchProducts = async () => {
try {
const res = await fetch("http://localhost:3003/products");
if (!res.ok) throw new Error(`HTTP error! Status: ${res.status}`);
const data = await res.json();
setProductData(data); // ✅ Restore correctly grouped data
} catch (error) {
console.error("Error fetching products:", error);
alert("Failed to load products. Please try again later.");
}
};
// Fetch products on component mount & refresh every 10s
useEffect(() => {
fetchProducts();
const interval = setInterval(fetchProducts, 10000);
return () => clearInterval(interval);
}, []);
return (
<div className="container">
<div className="up">
<h1>Buy the Old Products</h1>
</div>
<div className="search">
<input type="search" placeholder="Search..." className="search-bar" />
<button onClick={fetchProducts}>Refresh</button>
<img
src={filter}
alt="filter"
className="logo"
onClick={() => setShowDropdown(!showDropdown)}
/>
{showDropdown && (
<select className="dropdown">
<option value="option1">Brand</option>
<option value="option2">Location</option>
<option value="option3">Latest</option>
<option value="option4">Oldest</option>
</select>
)}
</div>
<div className="post">
<h2>Product Inventory</h2>
{productData.length === 0 ? (
<p>No products available.</p>
) : (
productData.reduce((acc, product) => {
// If username is already added, just push the product to existing user's array
let userEntry = acc.find((entry) => entry.username === product.username);
if (!userEntry) {
userEntry = { username: product.username, products: [] };
acc.push(userEntry);
}
userEntry.products.push(product);
return acc;
}, []).map(({ username, products }, idx) => (
<div key={idx} style={{ border: "1px solid #ddd", padding: "10px", margin: "5px", borderRadius: "5px" }}>
<p><strong>User:</strong> {username} has posted these products:</p>
<ul>
{products.map((product, idx) => (
<li key={idx}>
<strong>Type:</strong> {product.type}, <strong>Brand:</strong> {product.brand}, <strong>Quantity:</strong> {product.quantity}
{product.description && ` | Description: ${product.description}`}
</li>
))}
</ul>
</div>
))
)}
</div>
</div>
);
};
export default RemainPage;