Add assets/js/sync.js

This commit is contained in:
2026-06-16 10:48:58 +02:00
parent 3883cc7eec
commit 7a980263f1
+58
View File
@@ -0,0 +1,58 @@
import { getDB } from './db.js';
export async function syncQueue() {
const db = getDB();
const tx =
db.transaction(
'sync_queue',
'readwrite'
);
const store =
tx.objectStore('sync_queue');
const getAll =
store.getAll();
getAll.onsuccess = async () => {
const queue =
getAll.result;
if (!queue.length) {
return;
}
try {
const res =
await fetch('/api/sync.php', {
method: 'POST',
headers: {
'Content-Type':
'application/json'
},
body:
JSON.stringify(queue)
});
const data =
await res.json();
if (data.ok) {
store.clear();
}
} catch (e) {
console.log(
'offline, sync later'
);
}
};
}