toddler_shop_frontend/static/scripts/main.js

155 lines
5.5 KiB
JavaScript
Raw Normal View History

2024-08-04 10:29:43 +02:00
var all_products
2024-08-04 11:01:52 +02:00
let barcodeForm
2024-09-04 11:35:18 +02:00
const api_url = "https://api.toddlershop.yarnecoppens.com"
2024-08-04 17:59:05 +02:00
var to_fill_product_index = 0
2024-08-05 13:14:24 +02:00
var total_price = 0
async function makeAPIRequest(request) {
try {
const response = await fetch(request);
const result = await response.json();
return result
} catch (error) {
console.error("Error:", error);
}
}
2024-08-24 13:21:02 +02:00
async function loadCash(price) {
const cashAmountRequest = new Request(api_url + '/price_to_cash/' + price)
const cashAmount = await makeAPIRequest(cashAmountRequest)
const cash_bills_row = document.getElementById('cash_bills')
for (var bill_type in cashAmount){
bill_amount = cashAmount[bill_type]
console.log(bill_type, bill_amount)
for (var x = 0; x < bill_amount; x++){
const new_column = document.createElement('div')
new_column.classList.add('col')
2024-09-04 10:08:06 +02:00
const bill_image = document.createElement('img')
2024-09-04 11:23:25 +02:00
bill_image.classList.add('cash_image')
2024-09-04 10:08:06 +02:00
bill_image.src = api_url + '/icons/' + bill_type
new_column.appendChild(bill_image)
2024-08-24 13:21:02 +02:00
cash_bills_row.appendChild(new_column)
}
}
}
2024-09-04 11:52:18 +02:00
async function indexOnLoad(){
2024-09-04 11:59:45 +02:00
document.body.addEventListener('click', focusBarcodeInput, true);
2024-09-04 11:52:18 +02:00
loadProducts();
document.querySelector("input[autofocus]").addEventListener("blur", function() {
var t = this;
t.focus();
setTimeout(function() { // Some browsers won't let you do it until
t.focus(); // after the blur has completed
}, 100);
});
}
2024-09-04 11:59:45 +02:00
function focusBarcodeInput(){
document.getElementById('barcode_input').focus()
}
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();
2024-08-05 18:05:41 +02:00
if (to_fill_product_index < 5) {
2024-08-04 11:01:52 +02:00
2024-08-05 18:05:41 +02:00
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 11:01:52 +02:00
}
2024-08-05 18:05:41 +02:00
barcode_input.value = ""
const product_placeholders = document.getElementsByClassName('product_col')
const chosen_product_placeholder = product_placeholders[to_fill_product_index]
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
image_product.setAttribute('src', api_url + "/icons/" + chosen_product.image_filename);
2024-09-04 11:23:25 +02:00
image_product.style.visibility = 'visible';
2024-08-05 18:05:41 +02:00
price_product.textContent = "\u20AC " + chosen_product.price
total_price_holder.textContent = "\u20AC " + total_price
2024-08-04 17:59:05 +02:00
2024-08-05 18:05:41 +02:00
to_fill_product_index += 1
}else{
barcode_input.value = ""
}
2024-08-04 11:01:52 +02:00
});
2024-08-04 17:59:05 +02:00
2024-08-04 11:01:52 +02:00
}
2024-09-04 10:08:06 +02:00
2024-09-04 11:23:25 +02:00
function removeProduct(element_to_remove){
const product_row = document.getElementById('product_row')
const products = product_row.children
let found_removed_product = false
for (var i = 0; i < products.length; i++) {
var product = products[i];
if (product.getElementsByClassName('product_image')[0] == element_to_remove){
found_removed_product = true
const total_price_holder = document.getElementById("totalprice")
total_price -= product.getElementsByClassName('product_price')[0].textContent.replace('€ ','')
console.log(product.getElementsByClassName('product_price')[0].textContent.replace('€ ',''))
total_price_holder.textContent = "\u20AC " + total_price
const price_holder = element_to_remove.parentNode.getElementsByClassName('product_price')[0]
price_holder.innerHTML = ''
}
if (found_removed_product){
if (i < to_fill_product_index - 1){
const next_product = products[i+1]
const current_price_holder = product.getElementsByClassName('product_price')[0]
const next_price_holder = next_product.getElementsByClassName('product_price')[0]
product.getElementsByClassName('product_image')[0].src = next_product.getElementsByClassName('product_image')[0].src
current_price_holder.innerHTML = next_price_holder.innerHTML
}else{
product.getElementsByClassName('product_image')[0].style.visibility = 'hidden';
const current_price_holder = product.getElementsByClassName('product_price')[0]
current_price_holder.innerHTML = ''
}
}
}
to_fill_product_index -= 1
}
function payCash() {
2024-09-04 10:08:06 +02:00
const total_cost = document.getElementById('totalprice').innerHTML.replace('€ ','')
2024-09-04 11:23:25 +02:00
window.location.href = "/pay_cash/" + total_cost
2024-09-04 10:08:06 +02:00
}
function payCard() {
const total_cost = document.getElementById('totalprice').innerHTML.replace('€ ','')
window.location.href = "/pay_card/" + total_cost
}
2024-09-04 11:23:25 +02:00
function toMain() {
window.location.href = "/";
2024-09-04 10:08:06 +02:00
}