2024-08-04 10:29:43 +02:00
|
|
|
var all_products
|
|
|
|
|
|
2024-08-04 11:01:52 +02:00
|
|
|
let barcodeForm
|
|
|
|
|
|
2024-08-04 17:59:05 +02:00
|
|
|
const api_url = "http://127.0.0.1:8000"
|
|
|
|
|
|
|
|
|
|
var to_fill_product_index = 0
|
|
|
|
|
|
2024-08-05 13:14:24 +02:00
|
|
|
var total_price = 0
|
|
|
|
|
|
2024-08-04 10:19:24 +02:00
|
|
|
async function makeAPIRequest(request) {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(request);
|
|
|
|
|
const result = await response.json();
|
|
|
|
|
return result
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error:", error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadProducts() {
|
2024-08-04 17:59:05 +02:00
|
|
|
const loadProductRequest = new Request(api_url + "/products")
|
2024-08-04 10:29:43 +02:00
|
|
|
all_products = await makeAPIRequest(loadProductRequest)
|
|
|
|
|
console.log("Loaded products:", all_products)
|
2024-08-04 17:59:05 +02:00
|
|
|
|
2024-08-04 11:01:52 +02:00
|
|
|
barcodeForm = document.getElementById("barcode_form");
|
2024-08-04 17:59:05 +02:00
|
|
|
|
2024-08-04 11:01:52 +02:00
|
|
|
barcodeForm.addEventListener("submit", (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
const barcode = document.getElementById('barcode_input').value
|
|
|
|
|
var chosen_product
|
|
|
|
|
for (index = 0; index < all_products.length; index++) {
|
|
|
|
|
if (all_products[index].barcode == barcode) {
|
|
|
|
|
chosen_product = all_products[index]
|
|
|
|
|
}
|
2024-08-04 10:35:58 +02:00
|
|
|
}
|
2024-08-04 11:01:52 +02:00
|
|
|
|
|
|
|
|
barcode_input.value = ""
|
2024-08-04 17:59:05 +02:00
|
|
|
|
2024-08-05 17:03:10 +02:00
|
|
|
const product_placeholders = document.getElementsByClassName('product_col')
|
2024-08-04 17:59:05 +02:00
|
|
|
const chosen_product_placeholder = product_placeholders[to_fill_product_index]
|
2024-08-05 13:14:24 +02:00
|
|
|
const image_product = chosen_product_placeholder.getElementsByClassName('product_image')[0]
|
|
|
|
|
const price_product = chosen_product_placeholder.getElementsByClassName('product_price')[0]
|
|
|
|
|
const total_price_holder = document.getElementById("totalprice")
|
|
|
|
|
|
|
|
|
|
total_price += chosen_product.price
|
2024-08-04 17:59:05 +02:00
|
|
|
|
2024-08-05 17:03:10 +02:00
|
|
|
console.log(image_product)
|
|
|
|
|
|
|
|
|
|
image_product.setAttribute('src', api_url + "/icons/" + chosen_product.image_filename);
|
2024-08-04 17:59:05 +02:00
|
|
|
price_product.textContent = "\u20AC " + chosen_product.price
|
2024-08-05 13:14:24 +02:00
|
|
|
total_price_holder.textContent = "\u20AC " + total_price
|
2024-08-04 11:01:52 +02:00
|
|
|
|
2024-08-04 17:59:05 +02:00
|
|
|
to_fill_product_index += 1
|
2024-08-04 11:01:52 +02:00
|
|
|
});
|
2024-08-04 17:59:05 +02:00
|
|
|
|
2024-08-04 11:01:52 +02:00
|
|
|
}
|