Исправлены вкладки: синхронизация фоном без двойного рендера

This commit is contained in:
Dauren777 2026-06-10 09:46:31 +00:00
parent ca4fbf2549
commit 55f8906b77

View File

@ -99,7 +99,6 @@ function saveA(d){localStorage.setItem("pab_audits",JSON.stringify(d))}
function saveU(d){localStorage.setItem("pab_users",JSON.stringify(d))}
// Supabase sync — фоном, не ломает вкладки
function sbSyncAll(){return Promise.all([sbPullUsers(),sbPullAudits()])}
function sbPullUsers(){return fetch(SBU+"/rest/v1/users?select=*",{headers:{"apikey":SBK,"Authorization":"Bearer "+SBK}}).then(function(r){return r.json()}).then(function(d){var um=getU();d.forEach(function(x){if(!um[x.login])um[x.login]={pass:x.pass,name:x.name,email:x.email,role:x.role,freq:x.freq,branch:x.branch,dept:x.dept,region:x.region,oblast:x.oblast,city:x.city}});saveU(um)}).catch(function(){})}
function sbPullAudits(){return fetch(SBU+"/rest/v1/audits?select=*&order=created_at.desc",{headers:{"apikey":SBK,"Authorization":"Bearer "+SBK}}).then(function(r){return r.json()}).then(function(d){var am=[],ca=getA();d.forEach(function(x){am.push({id:x.id,number:x.number,date:x.date,location:x.location,region:x.region,workType:x.work_type,workerCount:x.worker_count,observer:x.observer,observerRole:x.observer_role,overallSafe:x.overall_safe,categories:x.categories,totalViolations:x.total_violations,dialogue:x.dialogue,photos:x.photos,docs:x.docs,createdBy:x.created_by,createdAt:x.created_at})});ca.forEach(function(a){var found=false;am.forEach(function(b){if(b.id===a.id)found=true});if(!found)am.push(a)});saveA(am)}).catch(function(){})}
function sbPushAudit(e){fetch(SBU+"/rest/v1/audits",{method:"POST",headers:{"apikey":SBK,"Authorization":"Bearer "+SBK,"Content-Type":"application/json","Prefer":"resolution=merge-duplicates"},body:JSON.stringify({id:e.id,number:e.number,date:e.date,location:e.location,region:e.region,work_type:e.workType,worker_count:e.workerCount,observer:e.observer,observer_role:e.observerRole,overall_safe:e.overallSafe,categories:e.categories,total_violations:e.totalViolations,dialogue:e.dialogue,photos:e.photos,docs:e.docs,created_by:e.createdBy,created_at:e.createdAt})}).catch(function(){})}
@ -129,12 +128,9 @@ function showPanel(n){
["NA","MS","DB","VL","HS"].forEach(function(id){document.getElementById("pn"+id).classList.remove("ac")});
document.getElementById("pn"+n).classList.add("ac");
document.querySelectorAll("nav a").forEach(function(a){a.classList.toggle("ac",a.getAttribute("onclick").indexOf("'"+n+"'")>=0)});
// Сразу рендерим из localStorage (быстро)
if(n==="MS")rMS();if(n==="DB")rDB();if(n==="VL")rVL();if(n==="HS")rHS();
// Потом синхронизируемся с Supabase и обновляем
sbSyncAll().then(function(){
if(n==="MS")rMS();if(n==="DB")rDB();if(n==="VL")rVL();if(n==="HS")rHS();
});
// Фоновая синхронизация с Supabase
sbPullUsers();sbPullAudits();
}
function doLogout(){sessionStorage.removeItem("pab_user");location.href="index.html"}
@ -249,7 +245,7 @@ function importData(){
input.click();
}
rHS();sbSyncAll();
rHS();sbPullUsers();sbPullAudits();
</script>
</body>
</html>