-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
279 lines (233 loc) · 9.22 KB
/
scripts.js
File metadata and controls
279 lines (233 loc) · 9.22 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Global variable to hold products
let products = []; // This is the list that all functions can use
const apiUrl = "https://hez5ohf737.execute-api.us-east-1.amazonaws.com/test"; // Replace with your API Gateway endpoint
// Function to toggle between dark and light modes
function toggleDarkMode() {
const body = document.body;
if (body.classList.contains("dark-mode")) {
body.classList.remove("dark-mode");
body.classList.add("light-mode");
} else {
body.classList.remove("light-mode");
body.classList.add("dark-mode");
}
}
// Function to handle product form submission (create or update)
async function handleProductSubmit(event) {
event.preventDefault();
// Reset the error/success message field
setNotification(""); // Clear existing message
const productId = document.getElementById("product-id").value;
const createdAt = document.getElementById("created-at").value; // Fetch `created_at`
const productName = document.getElementById("product-name").value;
const productDescription = document.getElementById("product-description").value;
const productPrice = parseFloat(document.getElementById("product-price").value);
const productStock = parseInt(document.getElementById("product-stock").value);
const product = {
name: productName,
description: productDescription,
price: productPrice,
stock: productStock,
};
if (productId) {
// Update product
product.created_at = createdAt;
const success = await updateProduct(productId, product);
if (success) {
setNotification("Product updated successfully");
} else {
setNotification("Failed to update product");
}
} else {
// Create new product
const success = await createProduct(product);
if (success) {
setNotification("Product created successfully");
} else {
setNotification("Failed to create product");
}
}
// Reset the form and reload products
document.getElementById("product-form").reset();
document.getElementById("product-id").value = "";
loadProducts(); // Reload product list after update
}
// Function to create a new product
async function createProduct(product) {
const response = await fetch(`${apiUrl}/products`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(product),
});
return response.ok; // Returns true if successful, false otherwise
}
// Function to update an existing product with productId included in the body
async function updateProduct(productId, product) {
// Include productId in the product object
const createdAt = document.getElementById("created-at").value;
const productWithId = {
...product,
id: productId,
created_at: createdAt,
};
const response = await fetch(`${apiUrl}/products`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(productWithId), // Ensure productId is part of the body
});
return response.ok; // Returns true if successful, false otherwise
}
// Function to delete a product
async function deleteProduct(productId) {
const product = products.find((p) => p.id === productId);
if (product && product.created_at) {
const response = await fetch(`${apiUrl}/products?id=${productId}&created_at=${product.created_at}`, {
method: "DELETE",
});
if (!response.ok) {
setNotification("Failed to delete product.");
} else {
setNotification("Product deleted successfully.");
}
loadProducts(); // Reload the product list after deletion
} else {
setNotification("Product deletion failed: created_at is undefined.");
}
}
// Function to load all products from the API
async function loadProducts() {
const response = await fetch(`${apiUrl}/products`, {
method: "GET"
});
if (response.ok) {
products = await response.json(); // Populate the global products list
displayProducts(products); // Display the products in the table
} else {
setNotification("Failed to load products");
}
}
// Function to display products in the table
function displayProducts(products) {
const productList = document.getElementById("product-list").getElementsByTagName("tbody")[0];
productList.innerHTML = ""; // Clear existing rows
products.forEach((product) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${product.name}</td>
<td>${product.description}</td>
<td>${product.price}</td>
<td>${product.stock}</td>
<td>
<button onclick="editProduct('${product.id}')">Edit</button>
<button onclick="deleteProduct('${product.id}')">Delete</button>
</td>
`;
productList.appendChild(row);
});
}
// Function to edit a product (pre-fill form)
function editProduct(productId) {
const product = products.find((p) => p.id === productId); // Find the correct product in the global products list
if (product) {
document.getElementById("product-id").value = productId;
document.getElementById("created-at").value = product.created_at || "Unknown";
document.getElementById("product-name").value = product.name;
document.getElementById("product-description").value = product.description;
document.getElementById("product-price").value = product.price;
document.getElementById("product-stock").value = product.stock;
}
}
// Function to set the error or success message
function setNotification(message) {
document.getElementById("notification").innerText = message;
}
// Function to apply filters and search
async function applyFilters() {
const searchQuery = document.getElementById("search-input").value; // Get search input
const minPrice = parseFloat(document.getElementById("min-price").value); // Get min price
const maxPrice = parseFloat(document.getElementById("max-price").value); // Get max price
// Create query parameters for filtering
const queryParams = new URLSearchParams();
if (searchQuery) queryParams.append("search", searchQuery);
if (!isNaN(minPrice)) queryParams.append("minPrice", minPrice);
if (!isNaN(maxPrice)) queryParams.append("maxPrice", maxPrice);
// Fetch filtered product list from the backend
const response = await fetch(`${apiUrl}/products?${queryParams.toString()}`);
if (response.ok) {
const products = await response.json(); // Parse the JSON response
displayProducts(products); // Function to display products on the frontend
} else {
console.error("Failed to fetch products:", response.statusText);
}
}
function clearFilters() {
// Reset search and filtering fields to default values
document.getElementById("search-input").value = ""; // Clear search input
document.getElementById("min-price").value = ""; // Clear min price
document.getElementById("max-price").value = ""; // Clear max price
// Reload product list without filters
applyFilters(); // Re-apply filters with default (empty) values
loadProducts(); // Ensure products are loaded when the page is loaded
}
// Load products on page load
window.onload = function () {
loadProducts(); // Ensure products are loaded when the page is loaded
};
// Function to parse CSV content into a JSON array
function parseCSV(csvContent) {
const lines = csvContent.split("\n"); // Split CSV content into lines
const headers = lines[0].split(","); // Get the headers from the first line
const data = []; // Array to hold parsed data
// Iterate through the remaining lines to create product objects
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
if (line.trim() === "") continue; // Skip empty lines
const values = line.split(","); // Split the line into values
const product = {}; // Create a product object
// Assign values to product object based on headers
for (let j = 0; j < headers.length; j++) {
product[headers[j].trim()] = values[j].trim(); // Trim spaces
}
data.push(product); // Add the product to the data array
}
return data; // Return the JSON array of products
}
// Function to upload CSV data for bulk input
async function uploadCSV() {
const fileInput = document.getElementById("csv-file");
if (fileInput.files.length === 0) {
setNotification("Please select a CSV file to upload."); // No file selected
return;
}
const file = fileInput.files[0]; // Get the selected file
// Read the CSV file as text
const reader = new FileReader();
reader.onload = async (event) => {
const csvData = event.target.result; // Get the CSV content
// Parse CSV content into a JSON array
const jsonData = parseCSV(csvData);
try {
// Make a POST request to the Lambda function with the JSON array
const response = await fetch(`${apiUrl}/products`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(jsonData), // Send parsed JSON data
});
if (response.ok) {
setNotification("Bulk upload successful!"); // Display success message
loadProducts();
} else {
setNotification("Bulk upload failed."); // Display error message
}
} catch (error) {
setNotification("An error occurred during the upload."); // Handle exceptions
}
};
reader.readAsText(file); // Read the CSV file
}