diff --git a/assets/js/sync.js b/assets/js/sync.js new file mode 100644 index 0000000..aa06a21 --- /dev/null +++ b/assets/js/sync.js @@ -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' + ); + } + }; +} \ No newline at end of file