feat: enhance sync functionality with unsynced answer detection and UI updates
This commit is contained in:
+2
-1
@@ -4,7 +4,7 @@ import * as viewer from "./viewer.js";
|
|||||||
import * as interact from "./interact.js";
|
import * as interact from "./interact.js";
|
||||||
import * as config from "./config.js";
|
import * as config from "./config.js";
|
||||||
import { db, question_db } from "./db.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');
|
// const { v4: uuidv4 } = require('uuid');
|
||||||
|
|
||||||
log.setDefaultLevel("warn")
|
log.setDefaultLevel("warn")
|
||||||
@@ -207,6 +207,7 @@ cornerstoneTools.init();
|
|||||||
function putAnswer(answer) {
|
function putAnswer(answer) {
|
||||||
answer.time_answered = new Date().toISOString();
|
answer.time_answered = new Date().toISOString();
|
||||||
answer.synced = false;
|
answer.synced = false;
|
||||||
|
updateSyncUI("syncing");
|
||||||
db.answers.put(answer).then(() => {
|
db.answers.put(answer).then(() => {
|
||||||
triggerImmediateSync(exam_details, URLS);
|
triggerImmediateSync(exam_details, URLS);
|
||||||
});
|
});
|
||||||
|
|||||||
+20
-2
@@ -119,8 +119,26 @@ export function initSync(exam_details, URLS) {
|
|||||||
clearInterval(syncInterval);
|
clearInterval(syncInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set initial state
|
// Check if there are unsynced answers at startup
|
||||||
updateSyncUI("saved");
|
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
|
// Run periodic sync every 15 seconds
|
||||||
syncInterval = setInterval(() => {
|
syncInterval = setInterval(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user