web/public/js/bag.js

142 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-11-17 16:36:00 +01:00
function add(product_id, price) {
var inputCount = document.getElementById(product_id);
inputCount.value++;
document.getElementById(product_id + '-price').innerHTML = 'Rp ' + price * inputCount.value;
2021-11-19 06:41:39 +01:00
set_total_price();
2021-11-17 16:36:00 +01:00
addToBag(product_id);
}
function minus(product_id, price) {
var inputCount = document.getElementById(product_id);
if (inputCount.value > 0) {
inputCount.value--;
document.getElementById(product_id + '-price').innerHTML = 'Rp ' + price * inputCount.value;
2021-11-19 06:41:39 +01:00
set_total_price();
2021-11-17 16:36:00 +01:00
removeFromBag(product_id);
}
}
function remove(product_id) {
var bag = JSON.parse(localStorage.getItem("bag"));
bag = bag.filter( x => {
if (x.product_id != product_id) {
return x;
}
location.reload();
});
localStorage.setItem("bag", JSON.stringify(bag));
}
function addToBag(product_id) {
if (localStorage.getItem("bag") === null) {
localStorage.setItem("bag", "[]");
}
var bag = JSON.parse(localStorage.getItem("bag"));
var filtered_bag = bag.filter( x => x.product_id == product_id);
if (filtered_bag.length === 0) {
bag.push({
product_id: product_id,
quantity: 1
});
2021-11-10 06:00:54 +01:00
} else {
2021-11-17 16:36:00 +01:00
filtered_bag[0].quantity++;
bag = bag.filter( x => {
if (x.product_id != product_id) {
return x;
}
});
bag.push(filtered_bag[0]);
2021-11-10 06:00:54 +01:00
}
2021-11-17 16:36:00 +01:00
localStorage.setItem("bag", JSON.stringify(bag));
2021-11-10 06:00:54 +01:00
}
2021-11-17 16:36:00 +01:00
function removeFromBag(product_id) {
if (localStorage.getItem("bag") === null) {
localStorage.setItem("bag", "[]");
2021-11-10 06:00:54 +01:00
} else {
2021-11-17 16:36:00 +01:00
var bag = JSON.parse(localStorage.getItem("bag"));
var filtered_bag = bag.filter( x => x.product_id == product_id);
if (filtered_bag.length > 0) {
filtered_bag[0].quantity--;
if (filtered_bag[0].quantity === 0) {
bag = bag.filter( x => {
if (x.product_id != product_id) {
return x;
}
location.reload();
});
} else {
bag = bag.filter( x => {
if (x.product_id != product_id) {
return x;
}
});
bag.push(filtered_bag[0]);
}
}
localStorage.setItem("bag", JSON.stringify(bag));
}
}
2021-11-19 06:41:39 +01:00
function set_total_price() {
let price = document.getElementsByClassName("price");
let total_price = 0;
for (let i = 0; i < price.length; i++) {
total_price += parseInt(price[i].innerHTML.substr(3));
}
document.getElementById("total-price").innerHTML = 'Rp ' + total_price;
}
2021-11-17 16:36:00 +01:00
function view_bag() {
var bag = JSON.parse(localStorage.getItem("bag"));
2021-11-19 06:41:39 +01:00
let total_price = 0;
2021-11-17 16:36:00 +01:00
var html = '';
for (var i = 0; i < bag.length; i++) {
2021-11-19 06:41:39 +01:00
let request = new XMLHttpRequest();
request.open('GET', `api/getProduct?id=${bag[i].product_id}`, false);
request.send(null);
if (request.status === 200) {
let data = JSON.parse(request.responseText);
total_price += parseInt(data[0].PRICE * bag[i].quantity);
2021-11-17 16:36:00 +01:00
html += `
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-sm-7 col-md-6 col-lg-4">
<img alt="placeholder image" class="img-fluid lazyload" src="img/${data[0].IMAGE}"
width="180px" height="180px" />
</div>
<div class="col-sm-5 col-md-6 col-lg-8">
<h2>${data[0].TITLE}</h2>
<h4>Size : ${data[0].SIZE}</h4>
<h6>Qty</h6>
<div style="display: flex;">
<span class="input-group-btn">
<button type="button" class="btn btn-default btn-number" style="color: white;"
2021-11-19 06:41:39 +01:00
onclick="minus('${bag[i].product_id}', '${data[0].PRICE}')"><i class="far fa-minus-square"></i></button>
2021-11-17 16:36:00 +01:00
</span>
2021-11-19 06:41:39 +01:00
<input class="box" type="text" disabled="disabled" value="${bag[i].quantity}"
class="form-control input-number" id="${bag[i].product_id}" />
2021-11-17 16:36:00 +01:00
<span class="input-group-btn">
<button type="button" class="btn btn-default btn-number" style="color: white;"
2021-11-19 06:41:39 +01:00
onclick="add('${bag[i].product_id}', '${data[0].PRICE}')"><i class="far fa-plus-square"></i></button>
2021-11-17 16:36:00 +01:00
</span>
</div>
<br>
2021-11-19 06:41:39 +01:00
<p id="${bag[i].product_id}-price" class="price">Rp ${data[0].PRICE * bag[i].quantity}</p>
<a href="javascript:;" onclick='return remove("${bag[i].product_id}")' style="color:red;"><i class="far fa-trash-alt" color="red"></i> <u>Remove</u></a>
2021-11-17 16:36:00 +01:00
<br><br>
</div>
</div>
</div>
</div>
`;
2021-11-19 06:41:39 +01:00
}
2021-11-10 06:00:54 +01:00
}
2021-11-19 06:41:39 +01:00
document.getElementById("view-bag").innerHTML = html;
document.getElementById("total-price").innerHTML = 'Rp ' + total_price;
2021-11-17 16:36:00 +01:00
}
view_bag();