-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.htm
166 lines (150 loc) · 5.75 KB
/
page.htm
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Product Display</title>
<link
href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
rel="stylesheet"
/>
<style>
.no-scrollbar::-webkit-scrollbar {
display: none;
}
body {
background-color: #f1f2f3;
}
</style>
</head>
<body class="w-screen flex flex-col items-center justify-center no-scrollbar">
<div class="m-4">
<label for="search" class="text-xl font-bold mr-2">Search by Name:</label>
<input
type="text"
id="search"
class="p-2 border border-gray-300 rounded"
/>
<button id="searchBtn" class="p-2 bg-blue-500 text-white rounded">
Search
</button>
</div>
<h1 class="col-span-3 w-full text-center text-4xl font-bold font-poppins">
Most Salled Products
</h1>
<div
id="products-container"
class="grid grid-cols-3 gap-8 overflow-auto no-scrollbar"
></div>
<script>
// document.addEventListener("DOMContentLoaded", function () {
// // Fetch products
// fetch("http://localhost:3000/products") // Replace with your actual API endpoint
// .then((response) => response.json())
// .then((products) => {
// // Display products
// const productsContainer =
// document.getElementById("products-container");
// products.forEach((product) => {
// const productDiv = document.createElement("div");
// productDiv.classList.add("product");
// productDiv.innerHTML = `
// <div class="relative w-full h-full grid place-items-center max-w-xs overflow-hidden rounded-lg bg-white shadow-md">
// <a href="${product.link}" target="_blank">
// <img class="h-60 rounded-t-lg object-cover m-auto" src="${
// product.image_url
// }" alt="product image" />
// </a>
// <div class="mt-4 px-5 pb-5">
// <a href="#">
// <h5 class="text-xl font-semibold tracking-tight text-slate-900">${
// product.name
// }</h5>
// </a>
// <div class="flex flex-col items-start justify-between w-full">
// <p>
// <span class="w-full text-2xl font-bold text-slate-900">${
// product.price
// }</span>
// <span class="w-full text-sm text-slate-900 line-through ${
// !product.discounted_price && "hidden"
// }">
// ${product.discounted_price}
// </span>
// </p>
// </div>
// </div>
// </div>
// `;
// productsContainer.appendChild(productDiv);
// });
// })
// .catch((error) => console.error("Error fetching products:", error));
// });
document.addEventListener("DOMContentLoaded", function () {
const productsContainer = document.getElementById("products-container");
const searchInput = document.getElementById("search");
const searchBtn = document.getElementById("searchBtn");
// Function to display products
function displayProducts(products) {
productsContainer.innerHTML = "";
products.forEach((product) => {
const productDiv = document.createElement("div");
productDiv.classList.add("product");
productDiv.innerHTML = `
<div class="relative w-full h-full grid place-items-center max-w-xs overflow-hidden rounded-lg bg-white shadow-md">
<a href="${product.link}" target="_blank">
<img class="h-60 rounded-t-lg object-cover m-auto" src="${
product.image_url
}" alt="product image" />
</a>
<div class="mt-4 px-5 pb-5">
<a href="#">
<h5 class="text-xl font-semibold tracking-tight text-slate-900">${
product.name
}</h5>
</a>
<div class="flex flex-col items-start justify-between w-full">
<p>
<span class="w-full text-2xl font-bold text-slate-900">${
product.price
}</span>
<span class="w-full text-sm text-slate-900 line-through ${
!product.discounted_price && "hidden"
}">
${product.discounted_price}
</span>
</p>
</div>
</div>
</div>
`;
productsContainer.appendChild(productDiv);
});
}
// Function to fetch products based on search term
function fetchProducts(searchTerm) {
let url = "http://localhost:3000/products";
// If a search term is provided, append it to the URL
if (searchTerm) {
url += `?name=${searchTerm}`;
}
console.log(url);
fetch(url)
.then((response) => response.json())
.then((products) => {
displayProducts(products);
})
.catch((error) => console.error("Error fetching products:", error));
}
// Initial fetch without search term
fetchProducts();
// Event listener for search button click
searchBtn.addEventListener("click", function () {
const searchTerm = searchInput.value;
fetchProducts(searchTerm);
});
});
</script>
</body>
</html>