From 253b0bc18357b5817dd3074dc218a498acafd84c Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 29 Jun 2026 11:49:45 +0100 Subject: [PATCH] feat: enhance sync functionality with unsynced answer detection and UI updates --- js/main.js | 3 ++- js/sync.js | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/js/main.js b/js/main.js index d02f433..def024e 100644 --- a/js/main.js +++ b/js/main.js @@ -4,7 +4,7 @@ import * as viewer from "./viewer.js"; import * as interact from "./interact.js"; import * as config from "./config.js"; import { db, question_db } from "./db.js"; -import { initSync, triggerImmediateSync } from "./sync.js"; +import { initSync, triggerImmediateSync, updateSyncUI } from "./sync.js"; // const { v4: uuidv4 } = require('uuid'); log.setDefaultLevel("warn") @@ -207,6 +207,7 @@ cornerstoneTools.init(); function putAnswer(answer) { answer.time_answered = new Date().toISOString(); answer.synced = false; + updateSyncUI("syncing"); db.answers.put(answer).then(() => { triggerImmediateSync(exam_details, URLS); }); diff --git a/js/sync.js b/js/sync.js index f6ab107..5393164 100644 --- a/js/sync.js +++ b/js/sync.js @@ -119,8 +119,26 @@ export function initSync(exam_details, URLS) { clearInterval(syncInterval); } - // Set initial state - updateSyncUI("saved"); + // Check if there are unsynced answers at startup + db.answers + .where({ + aid: exam_details.aid, + cid: exam_details.cid, + eid: exam_details.eid, + }) + .filter(ans => ans.synced === false) + .toArray() + .then(unsynced => { + if (unsynced && unsynced.length > 0) { + updateSyncUI("failed"); // Unsynced answers exist; they aren't accepted by server yet + performSync(exam_details, URLS); + } else { + updateSyncUI("saved"); + } + }) + .catch(() => { + updateSyncUI("saved"); + }); // Run periodic sync every 15 seconds syncInterval = setInterval(() => {