diff --git a/web/cart.js b/web/cart.js index a647e40..8f5e20e 100644 --- a/web/cart.js +++ b/web/cart.js @@ -43,6 +43,7 @@ // 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'); + window.dtfContaAberta=!!account.customer; const c=account.customer; if(c && !cliente.mail){ cliente={name:c.name||'',cnpj:c.cnpj,zap:c.zap,mail:c.mail}; @@ -57,6 +58,9 @@ } pintaEntrega(); } + // The account's save button only exists after this answer. + window.dispatchEvent(new Event('dtf-cart-changed')); + if(typeof pintaPedido==='function') pintaPedido(); }catch(error){notice.textContent='Recuperação de carrinho indisponível; o pedido ainda pode ser feito nesta sessão.';} finally{restoring=false;} })(); diff --git a/web/checkout.js b/web/checkout.js index 37a333e..ba160e5 100644 --- a/web/checkout.js +++ b/web/checkout.js @@ -26,7 +26,8 @@ residue:item.residueAccepted||0, stroke:item.strokeClass||null,strokeChoice:item.strokeChoice||null, sheets:(item.partes||[]).map(part=>part.production), - files:(item.localFiles||[]).map(file=>({name:file.name,size:file.size,lastModified:file.lastModified})) + files:[...(item.localFiles||[]),...(item.savedFiles||[])] + .map(file=>({name:file.name,size:file.size,lastModified:file.lastModified})) })),treatment:ajusteSnapshot(),coupon:cupomCode()}); } // The quote this browser is paying, forgotten. clearDraft also empties the @@ -149,6 +150,7 @@ }).catch(()=>{}); window.dtfSessionReady=ready; window.dtfApi=api; + window.dtfMensagem=message; ready.catch(error => { status.textContent = error.message; }); // One message line under the steps. Its tone says what it is: a refusal or // a missing field is an error (red), a cart that changed a warning, a @@ -269,7 +271,9 @@ } } })(); - e.promise.then(()=>{e.done=true;if(window.dtfTimings)T.uploadEnd=performance.now();falhas.delete(k);pintaEnvio();}, + e.promise.then(id=>{e.id=id;e.done=true;if(window.dtfTimings)T.uploadEnd=performance.now();falhas.delete(k);pintaEnvio(); + // "Salvar em Minhas folhas" waits for the item's files: the cart repaints when one lands. + if(window.dtfContaAberta&&typeof pintaPedido==='function')pintaPedido();}, error=>{envios.delete(k);if(error?.name!=='AbortError'){falhas.set(k,{file,error}); // A refusal with a status already went to Sentry as an API error (DTF-WEB-15). if(!error?.status)window.dtfObservability?.uiError?.('Envio de arquivo falhou: '+(error?.message||'erro no envio'),'site');}pintaEnvio();}); @@ -279,11 +283,23 @@ } // Each file's upload as the product page shows it, under the uploader. window.dtfEnvio={ - enviar:file=>{if(!falhas.has(chaveArquivo(file)))enviar(file).catch(()=>{});}, + enviar:file=>{ + if(falhas.has(chaveArquivo(file)))return Promise.resolve(null); + const p=enviar(file); p.catch(()=>{}); return p; + }, estado:file=>{const k=chaveArquivo(file),e=envios.get(k),f=falhas.get(k); return f?{fase:'falhou',erro:f.error?.message||'erro no envio'} :!e?null:e.done?{fase:'pronto'}:e.sent>=file.size?{fase:'verificando'}:{fase:'enviando',enviado:e.sent};}, deNovo:file=>{falhas.delete(chaveArquivo(file));enviar(file).catch(()=>{});pintaEnvio();}, + // A file already on the server (a saved sheet opened for editing): its id + // is known, so a later enviar(file) resolves at once and sends nothing. + conhecido:(file,uploadId)=>{ + const k=chaveArquivo(file); + falhas.delete(k); + envios.set(k,{file,sent:file.size,done:true,id:uploadId,failed:null, + stop:new AbortController(),promise:Promise.resolve(uploadId)}); + }, + idDe:file=>{const e=envios.get(chaveArquivo(file));return e&&e.done?(e.id??null):null;}, // A file taken off the page, and in no cart item, stops going. manter:files=>{ const quero=new Set([...files,...pedido.flatMap(it=>it.localFiles||[]),...(itemAtual?.localFiles||[])].map(chaveArquivo)); @@ -495,7 +511,8 @@ // One item carries at most 200 files (app/core/models.py); a bigger cart // line is refused here, with the reason, never by the generic 422. const semCap=cart.flatMap(line=>line.partes?.length?line.partes:[line]); - if (semCap.some(item=>(item.localFiles||[]).length>200)) + const totalArquivos=item=>(item.localFiles||[]).length+(item.savedFiles||[]).length; + if (semCap.some(item=>totalArquivos(item)>200)) return message('Este item tem mais de 200 arquivos. Divida em dois itens.','error'); const initialCart=cartSnapshot(); busy = true; @@ -511,11 +528,17 @@ ? line.partes.map((part,i)=>({...line,...part,split:{group:'f'+index,part:i+1,of:line.partes.length}})) : [line]); for (const item of sheets) { - if (!item.localFiles?.length) throw new Error('Selecione novamente os arquivos deste item.'); + const locais=item.localFiles||[], salvos=item.savedFiles||[]; + if (!locais.length && !salvos.length) throw new Error('Selecione novamente os arquivos deste item.'); const uploads=[]; - for (const [index,file] of item.localFiles.entries()) { - uploads.push(await upload(file)); - enviaFoto(uploads[index], item.fotos?.[index]); + if (salvos.length) { + // A sheet kept in Minhas folhas: the server already has the files. + for (const saved of salvos) uploads.push(saved.upload_id); + } else { + for (const [index,file] of locais.entries()) { + uploads.push(await upload(file)); + enviaFoto(uploads[index], item.fotos?.[index]); + } } // Layout versions the server takes: 3 since 2026-10-21, 2 for a cart saved before. if (![2,3].includes(item.production?.version) || item.production.sources?.length!==uploads.length) diff --git a/web/site-cart.js b/web/site-cart.js index 44b181c..46618c8 100644 --- a/web/site-cart.js +++ b/web/site-cart.js @@ -190,6 +190,29 @@ function aplicaTratamento(aPagar){ check.checked=ajusteTudo;check.disabled=false;mostraAvisoTratamento(''); } } +// Minhas folhas: the files of an item, and whether they are all already on +// the server (or the item came back from Minhas folhas with its own ids). +function arquivosDoItem(it){ + return [...(it.localFiles||[]),...(it.partes||[]).flatMap(p=>p.localFiles||[])]; +} +function podeSalvarFolha(it){ + if((it.savedFiles||[]).length || (it.partes||[]).some(p=>(p.savedFiles||[]).length)) return true; + return arquivosDoItem(it).every(f=>!!window.dtfEnvio?.idDe(f)); +} +// The cart item as Minhas folhas keeps it: files become their upload ids, +// without the pictures and without the cart's own marks. +function itemParaSalvar(it){ + const copia=JSON.parse(JSON.stringify(it)); + const pares=[{orig:it,dest:copia}]; + (it.partes||[]).forEach((p,i)=>pares.push({orig:p,dest:copia.partes[i]})); + for(const {orig,dest} of pares){ + dest.savedFiles=(orig.savedFiles||[]).length ? orig.savedFiles : (orig.localFiles||[]).map(f=>({ + upload_id:window.dtfEnvio?.idDe(f),name:f.name,size:f.size,lastModified:f.lastModified,type:f.type + })); + delete dest.localFiles; delete dest.fotos; delete dest.marcado; + } + return copia; +} // O carrinho junta itens de produtos diferentes e cobra uma vez só. function pintaPedido(){ const aPagar = itensAPagar(); @@ -221,6 +244,9 @@ function pintaPedido(){ '