All checks were successful
Build and deploy / Validate source (push) Successful in 10s
Build and deploy / Integration suite on a real stack (push) Successful in 4m49s
Build and deploy / Secret scan and release gate (push) Successful in 8s
Build and deploy / Publish images (push) Successful in 1m15s
The Site asked only for CPF/CNPJ, WhatsApp and e-mail, so a person's Tiny contact was named after the recipient or the e-mail, and the WhatsApp notices the client's n8n sends from Tiny greeted "Olá, fulano@gmail.com". The cart's "Seus dados" and the signup now ask for a name, the account page can change it, and it names a CPF customer's Tiny contact (a CNPJ keeps the registry's razão social), the Kanban card and the team's alerts. Orders placed before have none, as the server still accepts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
4.5 KiB
JavaScript
64 lines
4.5 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=['fNome','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={name:c.name||'',cnpj:c.cnpj,zap:c.zap,mail:c.mail};
|
|
// The fields' own input handlers format them, as if typed.
|
|
for(const [id,key] of [['fNome','name'],['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;}
|
|
})();
|
|
})();
|