All checks were successful
Build and deploy / Validate source (push) Successful in 6s
Build and deploy / Integration suite on a real stack (push) Successful in 3m7s
Build and deploy / Secret scan and release gate (push) Successful in 10s
Build and deploy / Publish images (push) Successful in 1m6s
The order page becomes the customer's area, one page at four addresses: - /conta/entrar: sign in or create an account, side by side. "Esqueci minha senha" points to the Dropstar WhatsApp until e-mail can be sent. - /conta: the counts of orders waiting for payment, in production, in correction and finished, and the latest one. - /conta/pedidos: every order and the cart waiting for payment in one list, newest first, filtered by group, order number and period, ten per page. The cart shows "Aguardando pagamento" and its "Pagar" goes to the PIX page when a PIX code is open. An order opens in place with its progress, items, delivery, history, files and the correction form. A guest sees the orders paid in this browser. - /conta/dados: WhatsApp and a saved delivery address (the CNPJ is locked), e-mail and password changes, both confirmed with the current password; a password change signs the other devices out. The cart fills in the account's details and saved address. New API routes for the details, and the order list takes filters and pages and returns the counts; only the newest unpaid quote whose files still exist is listed. The Site's "Minha conta" and "Ver meus pedidos" point to the new addresses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
4.4 KiB
JavaScript
64 lines
4.4 KiB
JavaScript
/* Browser-local cart recovery. Files are stored only for an unfinished cart, for 24h.
|
|
Only the items are kept: the customer's details and address are typed again
|
|
after a reload or a closed tab, so nothing personal stays in the browser. */
|
|
(() => {
|
|
// A browser may put back what was typed before the reload without telling
|
|
// the page, which then shows values it does not know and cannot pay with.
|
|
const CAMPOS=['fCnpj','fZap','fMail','cepIn','eNome','eRua','eNum','eComp','eBairro','eCidade','eUf'];
|
|
for(const id of CAMPOS)if($(id))$(id).value='';
|
|
let database,scope,timer,restoring=true;
|
|
let signedOut=false;
|
|
window.addEventListener('dtf-private-data-cleared',()=>{signedOut=true;clearTimeout(timer);pedido=[];itemAtual=null;folhas=[];artes=[];});
|
|
const notice=document.createElement('p');notice.className='carrAviso';
|
|
document.getElementById('carr').prepend(notice);
|
|
function transaction(mode,fn){return new Promise((resolve,reject)=>{const tx=database.transaction('cart',mode);const request=fn(tx.objectStore('cart'));let result;request.onsuccess=()=>result=request.result;tx.oncomplete=()=>resolve(result);tx.onerror=()=>reject(tx.error);tx.onabort=()=>reject(tx.error);});}
|
|
async function save(){
|
|
if(signedOut||restoring||!database||!scope)return;
|
|
const items=[...pedido,...(itemAtual?[itemAtual]:[])];
|
|
try{
|
|
if(!items.length){await transaction('readwrite',store=>store.delete(scope));return;}
|
|
await transaction('readwrite',store=>store.put({items,expires:Date.now()+86400000},scope));
|
|
notice.textContent='';
|
|
}catch(error){notice.textContent='Não foi possível salvar o carrinho neste navegador. Mantenha esta página aberta até enviar o pedido.';}
|
|
}
|
|
window.dtfClearCart=async()=>{clearTimeout(timer);if(database&&scope)await transaction('readwrite',store=>store.delete(scope));notice.textContent='';};
|
|
window.addEventListener('dtf-cart-changed',()=>{clearTimeout(timer);timer=setTimeout(save,400);});
|
|
window.addEventListener('pagehide',()=>{clearTimeout(timer);save();});
|
|
setInterval(async()=>{
|
|
if(!database||signedOut)return;
|
|
try{const keys=await transaction('readonly',s=>s.getAllKeys());for(const key of keys){const value=await transaction('readonly',s=>s.get(key));if(value&&value.expires<Date.now())await transaction('readwrite',s=>s.delete(key));}}catch{/* Browser may close storage during navigation. */}
|
|
},60000);
|
|
(async()=>{
|
|
try{
|
|
scope=(await window.dtfSessionReady).cart_scope;
|
|
database=await new Promise((resolve,reject)=>{const req=indexedDB.open('dtf-local-cart',1);req.onupgradeneeded=()=>req.result.createObjectStore('cart');req.onsuccess=()=>resolve(req.result);req.onerror=()=>reject(req.error);});
|
|
const records=await transaction('readonly',store=>store.getAllKeys());
|
|
for(const key of records){const value=await transaction('readonly',store=>store.get(key));if(value.expires<Date.now())await transaction('readwrite',store=>store.delete(key));}
|
|
const saved=await transaction('readonly',store=>store.get(scope));
|
|
if(saved && !pedido.length && !itemAtual){
|
|
pedido=saved.items;
|
|
$('atual').style.display='none';pintaEntrega();
|
|
notice.textContent='Carrinho recuperado. Remova e adicione novamente um item se precisar alterar sua montagem.';
|
|
}
|
|
// A signed-in customer's details come from the account, with the saved
|
|
// delivery address, whether or not a cart was recovered.
|
|
const account=await window.dtfApi('/account/me');
|
|
const c=account.customer;
|
|
if(c && !cliente.mail){
|
|
cliente={cnpj:c.cnpj,zap:c.zap,mail:c.mail};
|
|
// The fields' own input handlers format them, as if typed.
|
|
for(const [id,key] of [['fCnpj','cnpj'],['fZap','zap'],['fMail','mail']]){$(id).value=cliente[key];$(id).dispatchEvent(new Event('input'));}
|
|
const a=c.address;
|
|
if(a && !entrega.cep){
|
|
entrega.cep=a.postal_code;
|
|
entrega.end={nome:a.recipient,rua:a.street,num:a.number,comp:a.complement||'',bairro:a.district,cidade:a.city,uf:a.state};
|
|
$('cepIn').value=a.postal_code.replace(/^(\d{5})(\d{3})$/,'$1-$2');
|
|
for(const [id,key] of [['eNome','nome'],['eRua','rua'],['eNum','num'],['eComp','comp'],['eBairro','bairro'],['eCidade','cidade'],['eUf','uf']])$(id).value=entrega.end[key];
|
|
}
|
|
pintaEntrega();
|
|
}
|
|
}catch(error){notice.textContent='Recuperação de carrinho indisponível; o pedido ainda pode ser feito nesta sessão.';}
|
|
finally{restoring=false;}
|
|
})();
|
|
})();
|