From 1b3fa43a23e5ac22cbf09d4e51ee646309d9c827 Mon Sep 17 00:00:00 2001 From: subnixr Date: Sun, 26 Mar 2017 21:17:07 +0200 Subject: [PATCH] Clean little code --- static/js/sell.js | 56 ++++++++++++++++++++++++++++++++++++++++++ templates/sell.html | 60 ++------------------------------------------- 2 files changed, 58 insertions(+), 58 deletions(-) create mode 100644 static/js/sell.js diff --git a/static/js/sell.js b/static/js/sell.js new file mode 100644 index 0000000..be4cea7 --- /dev/null +++ b/static/js/sell.js @@ -0,0 +1,56 @@ +function updateTotal(amount) { + total_el = document.getElementById('total') + total = parseFloat(total_el.innerText) + total += amount + total_el.innerText = total.toFixed(2) +} + +function renderBasketItem(uid, name) { + return '' +} + +function addProduct(uid) { + // This is the hidden input element inside the form. We'll use this DOM + // element to keep informations about the product, such as the name, + // the price and the value inside the 'data-' tag. + form_el = document.getElementById('prod_' + uid) + basket = document.getElementById('basket') + basket_el = document.getElementById('basketitem_' + uid) + + form_el.value = parseInt(form_el.value) + 1 + + // If there is not a button for the given product inside the basket + // div we'll create it. + if (basket_el === null) { + basket.innerHTML += renderBasketItem(uid, form_el.dataset.name) + } else { + // Otherwise we'll just update it. + console.log(form_el.value) + new_text = form_el.value + ' x ' + form_el.dataset.name + basket_el.innerText = new_text + } + + updateTotal(parseFloat(form_el.dataset.price)) +} + +function delProduct(uid) { + form_el = document.getElementById('prod_' + uid) + basket = document.getElementById('basket') + basket_el = document.getElementById('basketitem_' + uid) + + form_el.value = parseInt(form_el.value) - 1 + + if (form_el.value == 0) { + basket.removeChild(basket_el) + } else { + new_text = form_el.value + ' x ' + form_el.dataset.name + basket_el.innerText = new_text + } + + updateTotal(parseFloat(-form_el.dataset.price)) +} + + diff --git a/templates/sell.html b/templates/sell.html index a87cf0f..8b8c66f 100644 --- a/templates/sell.html +++ b/templates/sell.html @@ -27,66 +27,10 @@ + {% endblock %}