-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
224 lines (205 loc) · 6.69 KB
/
Copy pathscript.js
File metadata and controls
224 lines (205 loc) · 6.69 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Mock product data with multiple stores for each product
const mockProducts = [
{
name: "Product A",
image: "https://via.placeholder.com/100",
category: "Electronics",
upc: "123456789012",
stores: [
{
name: "Store X",
location: "Location 1",
price: 100,
availability: "In Stock",
},
{
name: "Store Y",
location: "Location 2",
price: 80,
availability: "Low Stock",
},
{
name: "Store Z",
location: "Location 3",
price: 90,
availability: "Out of Stock",
},
],
},
{
name: "Product B",
image: "https://via.placeholder.com/100",
category: "Appliances",
upc: "987654321098",
stores: [
{
name: "Store A",
location: "Location 1",
price: 150,
availability: "In Stock",
},
{
name: "Store B",
location: "Location 2",
price: 120,
availability: "Low Stock",
},
{
name: "Store C",
location: "Location 3",
price: 130,
availability: "Out of Stock",
},
],
},
{
name: "Product C",
image: "https://via.placeholder.com/100",
category: "Home Goods",
upc: "567890123456",
stores: [
{
name: "Store P",
location: "Location 1",
price: 60,
availability: "Low Stock",
},
{
name: "Store Q",
location: "Location 2",
price: 55,
availability: "In Stock",
},
{
name: "Store R",
location: "Location 3",
price: 70,
availability: "In Stock",
},
],
},
];
const searchBar = document.getElementById("searchQuery");
const suggestionsList = document.querySelector(".suggestions");
console.debug(searchBar, suggestionsList);
searchBar.addEventListener("input", () => {
const query = searchBar.value.toLowerCase();
console.debug(suggestionsList.innerHTML);
suggestionsList.innerHTML = "";
if (!query) {
suggestionsList.style.display = "none";
return;
}
const filteredData = mockProducts
.filter((item) => item.name.toLowerCase().includes(query))
.map((value) => {
return {
name: value.name,
availability: value.stores.length,
};
});
if (filteredData.length > 0) {
filteredData.forEach((item) => {
const li = document.createElement("li");
const name = document.createElement("div");
name.textContent = item.name;
const stores = document.createElement("div");
stores.classList.add("store-availability");
stores.textContent = `${item.availability} stores`;
li.appendChild(name);
li.appendChild(stores);
li.addEventListener("click", () => {
searchBar.value = item.name;
suggestionsList.style.display = "none";
searchProducts();
document.scro;
});
suggestionsList.appendChild(li);
});
suggestionsList.style.display = "block";
} else {
suggestionsList.style.display = "none";
}
});
// Function to handle product search
function searchProducts() {
const searchQuery = document
.getElementById("searchQuery")
.value.toLowerCase();
const searchResults = document.getElementById("searchResults");
searchResults.innerHTML = "";
if (!searchQuery) {
searchResults.innerHTML =
"<p>Please enter a product name to search.</p>";
return;
}
const results = mockProducts.filter((product) =>
product.name.toLowerCase().includes(searchQuery)
);
if (results.length === 0) {
searchResults.innerHTML = "<p>No products found.</p>";
return;
}
results.forEach((product) => {
const productDiv = document.createElement("div");
productDiv.classList.add("product");
productDiv.innerHTML = `
<h3>${product.name}</h3>
<img src="${product.image}" alt="${product.name}">
<p>Category: ${product.category}</p>
<p>UPC: ${product.upc}</p>
<div id="stores_${product.name}" class="stores-info">
${product.stores
.map(
(store, index) => `
<div>
<p><strong>Store:</strong> ${store.name}</p>
<p><strong>Location:</strong> ${store.location}</p>
<p><strong>Price:</strong> $${store.price}</p>
<p><strong>Availability:</strong> ${
store.availability
}</p>
</div>
${
index < product.stores.length - 1
? '<div class="store-divider"></div>'
: ""
}
`
)
.join("")}
</div>
<button onclick="compareProduct('${product.name}')">Compare</button>
`;
searchResults.appendChild(productDiv);
});
}
// Function to compare prices and find the best deal
function compareProduct(productName) {
const product = mockProducts.find((p) => p.name === productName);
if (!product) return;
const availableStores = product.stores.filter(
(store) => store.availability !== "Out of Stock"
);
if (availableStores.length === 0) {
alert(`All stores are out of stock for ${product.name}.`);
return;
}
// Find the best deal
const bestDeal = availableStores.reduce(
(best, store) => (store.price < best.price ? store : best),
availableStores[0]
);
const message = `The best deal for ${product.name} is at ${bestDeal.name} in ${bestDeal.location} for $${bestDeal.price}.`;
alert(message);
}
// Function to handle email subscription
function subscribeEmail() {
const emailInput = document.getElementById("footerEmail").value;
if (emailInput) {
alert(`Subscribed successfully with email: ${emailInput}`);
document.getElementById("footerEmail").value = "";
} else {
alert("Please enter a valid email.");
}
}