feat: fill the delivery address from the CEP and quote freight as it is typed
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 2m21s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m47s
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 2m21s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m47s
A complete CEP now looks up its address on the server (ViaCEP, rate limited, keeping the Site's CSP to its own origin) and fills street, district, city and state, moving the cursor to the first field left, and quotes the freight without pressing Calcular. The CEP keeps its mask when the cart is restored. Freight and its delivery time appear in the order summary instead of a line under the CEP, the delivery option says it ships with Jadlog, and the address reminder is a hint rather than an error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -106,7 +106,7 @@ function pintaEntrega(){
|
||||
: entrega.cotado ? rs(entrega.valor) : 'a cotar';
|
||||
$('bPagar').disabled = !entrega.cotado || !clienteOk() || !enderecoOk() || !cartPodeEnviar();
|
||||
$('eEnd').textContent = entrega.tipo==='frete' && entrega.cotado && !enderecoOk()
|
||||
? 'Preencha o endereço de entrega para seguir.' : '';
|
||||
? 'Complete o endereço de entrega para ir ao pagamento.' : '';
|
||||
pintaPedido();
|
||||
}
|
||||
|
||||
@@ -158,10 +158,31 @@ document.querySelectorAll('.opE').forEach(b=>b.addEventListener('click',()=>{
|
||||
if(entrega.tipo==='frete') $('cepMsg').innerHTML='';
|
||||
pintaEntrega();
|
||||
}));
|
||||
function fmtCep(v){ v=String(v||'').replace(/\D/g,'').slice(0,8); return v.length>5 ? v.slice(0,5)+'-'+v.slice(5) : v; }
|
||||
// The address of a complete CEP fills the form; only the number is left to type.
|
||||
let cepBuscado='';
|
||||
async function buscaEndereco(cep){
|
||||
if(cep===cepBuscado || !window.dtfApi) return;
|
||||
cepBuscado=cep;
|
||||
try{
|
||||
const a=await window.dtfApi('/cep/'+cep);
|
||||
if(entrega.cep!==cep) return;
|
||||
for(const [id,chave,valor] of [['eRua','rua',a.street],['eBairro','bairro',a.district],
|
||||
['eCidade','cidade',a.city],['eUf','uf',a.state]]){
|
||||
if(!valor) continue;
|
||||
$(id).value=valor; entrega.end={...(entrega.end||{}),[chave]:valor};
|
||||
}
|
||||
pintaEntrega();
|
||||
const vazio=['eNome','eRua','eNum'].map($).find(el=>!el.value.trim());
|
||||
if(vazio && document.activeElement===$('cepIn')) vazio.focus();
|
||||
}catch(e){ /* sem consulta: o cliente preenche o endereço */ }
|
||||
}
|
||||
$('cepIn').addEventListener('input',e=>{
|
||||
const v=e.target.value.replace(/\D/g,'').slice(0,8);
|
||||
e.target.value = v.length>5 ? v.slice(0,5)+'-'+v.slice(5) : v;
|
||||
entrega.cep=v; entrega.cotado=false; entrega.valor=0; pintaEntrega();
|
||||
e.target.value = fmtCep(v);
|
||||
if(v===entrega.cep) return;
|
||||
entrega.cep=v; entrega.cotado=false; entrega.valor=0; entrega.dias=null; pintaEntrega();
|
||||
if(v.length===8){ buscaEndereco(v); if(window.dtfFreight) window.dtfFreight(); }
|
||||
});
|
||||
for(const [id,chave] of [['eNome','nome'],['eRua','rua'],['eNum','num'],['eComp','comp'],
|
||||
['eBairro','bairro'],['eCidade','cidade'],['eUf','uf']]){
|
||||
@@ -173,6 +194,7 @@ for(const [id,chave] of [['eNome','nome'],['eRua','rua'],['eNum','num'],['eComp'
|
||||
}
|
||||
$('bCep').addEventListener('click',async()=>{
|
||||
if(entrega.cep.length!==8){ $('cepMsg').innerHTML='CEP incompleto.'; return; }
|
||||
buscaEndereco(entrega.cep);
|
||||
if(window.dtfFreight){ await window.dtfFreight(); return; }
|
||||
// A cotação vem da transportadora. Enquanto a integração não existir, o pedido não
|
||||
// fecha por frete: o valor precisa estar fechado antes do Mercado Pago.
|
||||
|
||||
Reference in New Issue
Block a user