Skip to content

Commit

Permalink
Finish assessment warning (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
dqnykamp authored Oct 29, 2023
1 parent c00bdbf commit 9b2fa5b
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 119 deletions.
188 changes: 111 additions & 77 deletions packages/doenetml-worker/src/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ export default class Core {
this.errorWarnings.errors.push(...res.errors);
this.errorWarnings.warnings.push(...res.warnings);

this.failedToSavePageState = false;
this.failedToSaveCreditForItem = false;

// console.log(`serialized components at the beginning`)
// console.log(deepClone(serializedComponents));

Expand Down Expand Up @@ -455,7 +458,6 @@ export default class Core {
this.saveSubmissions({
pageCreditAchieved:
await this.document.stateValues.creditAchieved,
suppressToast: true,
});
}

Expand Down Expand Up @@ -10810,7 +10812,6 @@ export default class Core {
canSkipUpdatingRenderer = false,
skipRendererUpdate = false,
sourceInformation = {},
suppressToast = false, // temporary
}) {
if (warnings && warnings.length > 0) {
this.errorWarnings.warnings.push(...warnings);
Expand Down Expand Up @@ -10985,7 +10986,7 @@ export default class Core {
// so don't record the submission to the attempt tables
// (the event will still get recorded)
if (this.itemNumber > 0) {
this.saveSubmissions({ pageCreditAchieved, suppressToast });
this.saveSubmissions({ pageCreditAchieved });
}
}

Expand Down Expand Up @@ -11180,14 +11181,6 @@ export default class Core {
// console.log(">>>>resp from record event", resp.data)
} catch (e) {
console.error(`Error saving event: ${e.message}`);
// postMessage({
// messageType: "sendToast",
// coreId: this.coreId,
// args: {
// message: `Error saving event: ${e.message}`,
// alertType: "error"
// }
// })
}
}

Expand Down Expand Up @@ -12707,6 +12700,26 @@ export default class Core {
}
}

async saveImmediately() {
if (this.savePageStateTimeoutID) {
// if in debounce to save page to local storage
// then immediate save to local storage
// and override timeout to save to database
clearTimeout(this.savePageStateTimeoutID);
await this.saveState(true);
} else {
// else override timeout to save any pending changes to database
await this.saveChangesToDatabase(true);
}

postMessage({
messageType: "saveImmediatelyResult",
success: !(
this.failedToSavePageState || this.failedToSaveCreditForItem
),
});
}

async saveState(overrideThrottle = false) {
this.savePageStateTimeoutID = null;

Expand Down Expand Up @@ -12788,10 +12801,10 @@ export default class Core {
}, 60000);

// TODO: find out how to test if not online
// and send this toast if not online:
// and send this alert if not online:

// postMessage({
// messageType: "sendToast",
// messageType: "sendAlert",
// coreId: this.coreId,
// args: {
// message: "You're not connected to the internet. Changes are not saved. ",
Expand All @@ -12808,45 +12821,59 @@ export default class Core {
);
} catch (e) {
postMessage({
messageType: "sendToast",
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"Error synchronizing data. Changes not saved to the server.",
alertType: "error",
id: "dataError",
},
});

this.failedToSavePageState = true;

return;
}

console.log("result from saving to database:", resp.data);

if (resp.status === null) {
postMessage({
messageType: "sendToast",
messageType: "sendAlert",
coreId: this.coreId,
args: {
message: `Error synchronizing data. Changes not saved to the server. Are you connected to the internet?`,
alertType: "error",
id: "dataError",
},
});

this.failedToSavePageState = true;

return;
}

let data = resp.data;

if (!data.success) {
postMessage({
messageType: "sendToast",
messageType: "sendAlert",
coreId: this.coreId,
args: {
message: data.message,
alertType: "error",
id: "dataError",
},
});

this.failedToSavePageState = true;

return;
}

this.failedToSavePageState = false;

this.serverSaveId = data.saveId;

if (this.flags.allowLocalState) {
Expand Down Expand Up @@ -12910,7 +12937,7 @@ export default class Core {
// console.log(">>>>recordContentInteraction data",data)
}

saveSubmissions({ pageCreditAchieved, suppressToast = false }) {
saveSubmissions({ pageCreditAchieved }) {
if (!this.flags.allowSaveSubmissions) {
return;
}
Expand All @@ -12922,7 +12949,7 @@ export default class Core {
itemNumber: this.itemNumber,
};

console.log("payload for save credit for item", payload);
// console.log("payload for save credit for item", payload);

axios
.post(this.apiURLs.saveCreditForItem, payload)
Expand All @@ -12931,22 +12958,28 @@ export default class Core {

if (resp.status === null) {
postMessage({
messageType: "sendToast",
messageType: "sendAlert",
coreId: this.coreId,
args: {
message: `Credit not saved due to error. Are you connected to the internet?`,
alertType: "error",
id: "creditDataError",
},
});

this.failedToSaveCreditForItem = true;
} else if (!resp.data.success) {
postMessage({
messageType: "sendToast",
messageType: "sendAlert",
coreId: this.coreId,
args: {
message: `Credit not saved due to error: ${resp.data.message}`,
alertType: "error",
id: "creditDataError",
},
});

this.failedToSaveCreditForItem = true;
} else {
let data = resp.data;

Expand All @@ -12966,81 +12999,85 @@ export default class Core {
},
});

this.failedToSaveCreditForItem = false;

//TODO: need type warning (red but doesn't hang around)
if (data.viewedSolution) {
if (!suppressToast) {
postMessage({
messageType: "sendToast",
coreId: this.coreId,
args: {
message:
"No credit awarded since solution was viewed.",
alertType: "info",
},
});
}
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"No credit awarded since solution was viewed.",
alertType: "info",
id: "solutionViewed",
},
});
}
if (data.timeExpired) {
if (!suppressToast) {
postMessage({
messageType: "sendToast",
coreId: this.coreId,
args: {
message:
"No credit awarded since the time allowed has expired.",
alertType: "info",
},
});
}
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"No credit awarded since the time allowed has expired.",
alertType: "info",
id: "timeExpired",
},
});
}
if (data.pastDueDate) {
if (!suppressToast) {
postMessage({
messageType: "sendToast",
coreId: this.coreId,
args: {
message:
"No credit awarded since the due date has passed.",
alertType: "info",
},
});
}
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"No credit awarded since the due date has passed.",
alertType: "info",
id: "pastDue",
},
});
}
if (data.exceededAttemptsAllowed) {
if (!suppressToast) {
postMessage({
messageType: "sendToast",
coreId: this.coreId,
args: {
message:
"No credit awarded since no more attempts are allowed.",
alertType: "info",
},
});
}
postMessage({
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"No credit awarded since no more attempts are allowed.",
alertType: "info",
id: "noMoreAttempts",
},
});
}
if (data.databaseError) {
postMessage({
messageType: "sendToast",
messageType: "sendAlert",
coreId: this.coreId,
args: {
message:
"Credit not saved due to database error.",
alertType: "error",
id: "creditDataError",
},
});

this.failedToSaveCreditForItem = true;
}
}
})
.catch((e) => {
postMessage({
messageType: "sendToast",
messageType: "sendAlert",
coreId: this.coreId,
args: {
message: `Credit not saved due to error: ${e.message}`,
alertType: "error",
id: "creditDataError",
},
});

this.failedToSaveCreditForItem = true;
});
}

Expand Down Expand Up @@ -13173,11 +13210,12 @@ export default class Core {
if (resp.status === null) {
let message = `Cannot show solution due to error. Are you connected to the internet?`;
postMessage({
messageType: "sendToast",
messageType: "sendAlert",
coreId: this.coreId,
args: {
message,
alertType: "error",
id: "solutionDataError",
},
});
return {
Expand Down Expand Up @@ -13206,11 +13244,12 @@ export default class Core {
let message = `Cannot show solution due to error.`;

postMessage({
messageType: "sendToast",
messageType: "sendAlert",
coreId: this.coreId,
args: {
message,
alertType: "error",
id: "solutionDataError",
},
});

Expand Down Expand Up @@ -13320,15 +13359,10 @@ export default class Core {
}
}

if (this.savePageStateTimeoutID) {
// if in debounce to save page to local storage
// then immediate save to local storage
// and override timeout to save to database
clearTimeout(this.savePageStateTimeoutID);
await this.saveState(true);
} else {
// else override timeout to save any pending changes to database
await this.saveChangesToDatabase(true);
await this.saveImmediately();

if (this.failedToSavePageState || this.failedToSaveCreditForItem) {
throw Error("Terminating core failed due to failure to save data.");
}
}

Expand Down
Loading

0 comments on commit 9b2fa5b

Please sign in to comment.