Files
erahn-click/public/js/miscellaneous/navigation-trolley.js
T
2025-01-12 16:42:48 +08:00

67 lines
1.8 KiB
JavaScript
Vendored

async function fetchCartCount() {
try {
const response = await fetch('/showcarts-available');
if (!response.ok) {
throw new Error('Failed to fetch cart count');
}
const data = await response.json();
let totalcount = 0;
totalcount += (data.status_koop === 'PENDING') ? 1 : 0;
totalcount += (data.status_pkb === 'PENDING') ? 1 : 0;
const cartCount = totalcount || 0;
updateCartCount(cartCount);
} catch (error) {
console.error('Error fetching cart count:', error);
updateCartCount(0);
}
}
function updateCartCount(itemCount) {
const cartCountElement = document.getElementById('cart-count');
const ahrefRedirect = document.getElementById('a-redirect');
if (itemCount > 0) {
cartCountElement.textContent = itemCount;
cartCountElement.classList.remove('d-none');
} else {
cartCountElement.textContent = '';
cartCountElement.classList.add('d-none');
}
}
function redirectTrolley() {
const cartCountElement = document.getElementById('cart-count');
if(cartCountElement.textContent == 0){
return false;
}
let form = document.createElement('form');
form.method = 'POST';
form.action = routetrolley;
let csrfInput = document.createElement('input');
csrfInput.type = 'hidden';
csrfInput.name = '_token';
csrfInput.value = csrftoken;
form.appendChild(csrfInput);
let redirectInput = document.createElement('input');
redirectInput.type = 'hidden';
redirectInput.name = 'redirect_trolley';
redirectInput.value = 'trolley';
form.appendChild(redirectInput);
document.body.appendChild(form);
const loader = document.getElementById('load-arrahn');
loader.classList.remove('d-none');
form.submit();
}
fetchCartCount();