-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
178 lines (161 loc) · 5.41 KB
/
script.js
File metadata and controls
178 lines (161 loc) · 5.41 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
// static/script.js
const state = {
allProducts: [],
filteredProducts: [],
cart: [],
selectedCategory: 'All',
};
const API_BASE_URL = window.location.origin;
// DOM
const categoryFilterEl = document.getElementById('category-filter');
const productListEl = document.getElementById('product-list');
const cartItemsEl = document.getElementById('cart-items');
const cartTotalEl = document.getElementById('cart-total');
const checkoutButtonEl = document.getElementById('checkout-button');
const customerIdInputEl = document.getElementById('customer-id');
const messageBoxEl = document.getElementById('message-box');
const emptyCartMessageEl = document.getElementById('empty-cart-message');
// Render filter buttons
function renderCategoryFilter() {
categoryFilterEl.innerHTML = '';
const categories = ['All', ...new Set(state.allProducts.map(p => p.Category))].sort();
categories.forEach(category => {
const btn = document.createElement('button');
btn.className = 'filter-btn';
btn.textContent = category;
btn.dataset.category = category;
if (category === state.selectedCategory) btn.classList.add('active');
categoryFilterEl.appendChild(btn);
});
}
// Render product cards
function renderProducts() {
state.filteredProducts = state.selectedCategory === 'All'
? state.allProducts
: state.allProducts.filter(p => p.Category === state.selectedCategory);
productListEl.innerHTML = '';
if (state.filteredProducts.length === 0) {
productListEl.innerHTML = `<p class="empty-message">No products found in this category.</p>`;
}
state.filteredProducts.forEach(p => {
const el = document.createElement('div');
el.className = 'product-card';
el.innerHTML = `
<div class="product-info">
<h3>${p.Name}</h3>
<p>Category: ${p.Category}</p>
<p>Stock: ${p.StockQty}</p>
<p class="product-price">$${Number(p.Price).toFixed(2)}</p>
</div>
<button class="add-to-cart-btn" data-id="${p.ProductID}">Add to Cart</button>
`;
productListEl.appendChild(el);
});
}
// Render cart
function renderCart() {
cartItemsEl.innerHTML = '';
let total = 0;
if (state.cart.length === 0) {
emptyCartMessageEl.classList.remove('hidden');
} else {
emptyCartMessageEl.classList.add('hidden');
}
state.cart.forEach(item => {
const row = document.createElement('div');
row.className = 'cart-item';
row.innerHTML = `
<div class="cart-item-details">
<h4>${item.name}</h4>
<p>${item.quantity} x $${item.price.toFixed(2)}</p>
</div>
<button class="remove-btn" data-id="${item.id}">X</button>
`;
cartItemsEl.appendChild(row);
total += item.price * item.quantity;
});
cartTotalEl.textContent = `$${total.toFixed(2)}`;
checkoutButtonEl.disabled = state.cart.length === 0;
}
// Helpers
function showMessage(text, ok) {
messageBoxEl.textContent = text;
messageBoxEl.classList.remove('hidden','success','error');
messageBoxEl.classList.add(ok ? 'success' : 'error');
setTimeout(() => messageBoxEl.classList.add('hidden'), 4000);
}
// Events
productListEl.addEventListener('click', (e) => {
const btn = e.target.closest('.add-to-cart-btn');
if (!btn) return;
const id = parseInt(btn.dataset.id);
const p = state.allProducts.find(x => x.ProductID === id);
if (!p) return;
const exist = state.cart.find(x => x.id === id);
if (exist) exist.quantity += 1;
else state.cart.push({ id: p.ProductID, name: p.Name, price: Number(p.Price), quantity: 1 });
renderCart();
});
cartItemsEl.addEventListener('click', (e) => {
const btn = e.target.closest('.remove-btn');
if (!btn) return;
const id = parseInt(btn.dataset.id);
state.cart = state.cart.filter(x => x.id !== id);
renderCart();
});
categoryFilterEl.addEventListener('click', (e) => {
const btn = e.target.closest('.filter-btn');
if (!btn) return;
document.querySelector('.filter-btn.active')?.classList.remove('active');
btn.classList.add('active');
state.selectedCategory = btn.dataset.category;
renderProducts();
});
checkoutButtonEl.addEventListener('click', async () => {
const customerId = parseInt(customerIdInputEl.value || '0');
if (!customerId || customerId <= 0) {
showMessage("Please enter a valid Customer ID.", false);
return;
}
if (state.cart.length === 0) {
showMessage("Your cart is empty.", false);
return;
}
const payload = {
customer_id: customerId,
cart: state.cart.map(i => ({ id: i.id, quantity: i.quantity, price: i.price }))
};
try {
const res = await fetch(`${API_BASE_URL}/api/place_order`, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
const data = await res.json();
if (data.success) {
showMessage(data.message, true);
state.cart = [];
customerIdInputEl.value = '';
renderCart();
} else {
showMessage(data.message || "Order failed.", false);
}
} catch (err) {
console.error(err);
showMessage("Network error. Is the Flask server running?", false);
}
});
// Initial load
async function fetchProducts() {
try {
const res = await fetch(`${API_BASE_URL}/api/products`);
state.allProducts = await res.json();
renderCategoryFilter();
renderProducts();
} catch (err) {
productListEl.innerHTML = `<p class="error-message">Failed to load products.</p>`;
}
}
document.addEventListener('DOMContentLoaded', () => {
fetchProducts();
renderCart();
});