This commit is contained in:
Joakim
2026-11-01 22:06:14 +01:00
parent f247cd69bf
commit 9ae513a1b8
7 changed files with 872 additions and 14 deletions

View File

@@ -1,4 +1,5 @@
const state={products:[],quantities:{},paymentTimer:null,pollTimer:null,thankTimer:null,pollSince:0};
const state={products:[],quantities:{},paymentTimer:null,pollTimer:null,thankTimer:null,pollSince:0,thankYouSeconds:3};
const debug=(...args)=>console.log('[FIKAKASSA]',new Date().toISOString(),...args);
// Kioskläge: blockera webbläsarens vanliga touch-, markerings- och kontextmenyer.
document.addEventListener('contextmenu',e=>e.preventDefault());
@@ -137,9 +138,58 @@ function renderProducts(){
}
function changeQty(id,d){state.quantities[id]=Math.max(0,(state.quantities[id]||0)+d);renderProducts()}
function clearTimers(){if(state.paymentTimer)clearTimeout(state.paymentTimer);if(state.pollTimer)clearTimeout(state.pollTimer);if(state.thankTimer)clearTimeout(state.thankTimer);state.paymentTimer=state.pollTimer=state.thankTimer=null}
function resetShop(){clearTimers();state.quantities={};state.pollSince=Date.now()/1000;paymentView.classList.add('hidden');thanksView.classList.add('hidden');shopView.classList.remove('hidden');renderProducts()}
async function startPayment(){const items=state.products.map(p=>({id:p.id,quantity:state.quantities[p.id]||0})).filter(x=>x.quantity>0);if(!items.length)return;const r=await fetch('/api/order',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({items})});if(!r.ok)return;const order=await r.json();summary.innerHTML=order.items.map(i=>`<div class="summary-row"><strong>${i.quantity} ×</strong><span>${i.name}</span></div>`).join('');paymentTotal.textContent=money(order.total);posterAmount.textContent=`Betala ${money(order.total)}`;posterNumber.textContent=formatSwishNumber(order.swish_number);posterName.textContent=order.swish_name||'Joakim Svensson';try{await prepareQr(order.id);}catch(e){console.error(e);return;}shopView.classList.add('hidden');thanksView.classList.add('hidden');paymentView.classList.remove('hidden');timeoutBar.style.transition='none';timeoutBar.style.transform='scaleX(1)';requestAnimationFrame(()=>requestAnimationFrame(()=>{timeoutBar.style.transition='transform 30s linear';timeoutBar.style.transform='scaleX(0)'}));state.pollSince=Date.now()/1000;pollPayment();state.paymentTimer=setTimeout(resetShop,30000)}
async function pollPayment(){try{const r=await fetch(`/api/payment/event?after=${state.pollSince}`,{cache:'no-store'});if(r.ok){const e=await r.json();if(e.event==='payment'){state.pollSince=e.received_at;showThanks();return}}}catch(e){}if(!paymentView.classList.contains('hidden'))state.pollTimer=setTimeout(pollPayment,700)}
function showThanks(){clearTimers();paymentView.classList.add('hidden');shopView.classList.add('hidden');thanksView.classList.remove('hidden');state.thankTimer=setTimeout(resetShop,3000)}
function resetShop(){
debug('resetShop()');
clearTimers();state.quantities={};state.pollSince=Date.now()/1000;
paymentView.classList.add('hidden');thanksView.classList.add('hidden');shopView.classList.remove('hidden');renderProducts()
}
async function startPayment(){
const items=state.products.map(p=>({id:p.id,quantity:state.quantities[p.id]||0})).filter(x=>x.quantity>0);
if(!items.length)return;
debug('startPayment()',items);
const r=await fetch('/api/order',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({items})});
if(!r.ok){debug('POST /api/order failed',r.status);return}
const order=await r.json();
debug('order created',order.id,'total=',order.total,'created_at=',order.created_at);
summary.innerHTML=order.items.map(i=>`<div class="summary-row"><strong>${i.quantity} ×</strong><span>${i.name}</span></div>`).join('');
paymentTotal.textContent=money(order.total);posterAmount.textContent=`Betala ${money(order.total)}`;
posterNumber.textContent=formatSwishNumber(order.swish_number);posterName.textContent=order.swish_name||'Joakim Svensson';
try{await prepareQr(order.id)}catch(e){debug('QR failed',e);console.error(e);return}
shopView.classList.add('hidden');thanksView.classList.add('hidden');paymentView.classList.remove('hidden');
timeoutBar.style.transition='none';timeoutBar.style.transform='scaleX(1)';
requestAnimationFrame(()=>requestAnimationFrame(()=>{timeoutBar.style.transition='transform 30s linear';timeoutBar.style.transform='scaleX(0)'}));
// Use order.created_at rather than "now". A fast payment can arrive while the QR is loading.
state.pollSince=Math.max(0,Number(order.created_at)-0.01);
debug('payment polling started, after=',state.pollSince);
pollPayment();
state.paymentTimer=setTimeout(()=>{debug('payment timeout');resetShop()},30000)
}
async function pollPayment(){
try{
const after=state.pollSince;
const r=await fetch(`/api/payment/event?after=${after}`,{cache:'no-store'});
if(r.ok){
const e=await r.json();
debug('payment event poll',e);
if(e.event==='payment'){
state.pollSince=e.received_at;
showThanks();
return
}
}else debug('payment event HTTP',r.status);
}catch(e){debug('payment poll error',e)}
if(!paymentView.classList.contains('hidden'))state.pollTimer=setTimeout(pollPayment,700)
}
function showThanks(){
debug('showThanks(): payment accepted, switching to thanks screen');
clearTimers();
paymentView.classList.add('hidden');shopView.classList.add('hidden');thanksView.classList.remove('hidden');
debug('thanks timer started',state.thankYouSeconds*1000,'ms');
state.thankTimer=setTimeout(()=>{debug('thanks timer expired');resetShop()},state.thankYouSeconds*1000)
}
nextCustomer.onclick=resetShop;cancelPurchase.onclick=resetShop;
Promise.all([fetch('/api/products').then(r=>r.json()),fetch('/api/shop').then(r=>r.json())]).then(([pd,sd])=>{state.products=pd.products;applyGridConfig(sd.grid);state.products.forEach(p=>state.quantities[p.id]=0);renderProducts()});
Promise.all([fetch('/api/products').then(r=>r.json()),fetch('/api/shop').then(r=>r.json())]).then(([pd,sd])=>{
state.products=pd.products;applyGridConfig(sd.grid);state.thankYouSeconds=Number(sd.thank_you_seconds)||3;
debug('shop config loaded',sd);
state.products.forEach(p=>state.quantities[p.id]=0);renderProducts()
}).catch(e=>debug('initial load failed',e));