Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
track time traveller update times to compare with draft update times,…
Browse files Browse the repository at this point in the history
… save notify about changes
  • Loading branch information
Travis1282 committed Dec 5, 2023
1 parent 93aaec6 commit 0f79079
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/sdk/lsf-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,24 +677,24 @@ export class LSFWrapper {
};

draftToast = (status) => {

if (status === 200 || status === 201) this.datamanager.invoke("toast", { message: "Draft saved successfully", type: "info" });
else if (status !== undefined) this.datamanager.invoke("toast", { message: "There was an error saving your draft", type: "error" });

}

needsDraftSave = (annotation) => annotation?.history?.hasChanges && new Date(annotation.history.lastAdditionTime) > new Date(annotation.draftSaved);

saveDraft = async (target = null) => {
const selected = target || this.lsf?.annotationStore?.selected;
const hasChanges = selected.history.hasChanges;
const submissionInProgress = selected?.submissionStarted;
const draftIsFresh = new Date(selected.draftSaved) > new Date() - selected.autosaveDelay;
const annotation = target || this.lsf?.annotationStore?.selected;
const hasChanges = this.needsDraftSave(annotation);
const submissionInProgress = annotation?.submissionStarted;

if (selected?.isDraftSaving || draftIsFresh) {
await when(() => !selected.isDraftSaving);
if (annotation?.isDraftSaving) {
await when(() => !annotation.isDraftSaving);
this.draftToast(200);
}
else if (hasChanges && selected && !submissionInProgress) {
const res = await selected?.saveDraftImmediatelyWithResults();
else if (hasChanges && annotation && !submissionInProgress) {
const res = await annotation?.saveDraftImmediatelyWithResults();
const status = res?.$meta?.status;

this.draftToast(status);
Expand All @@ -704,6 +704,8 @@ export class LSFWrapper {
onSubmitDraft = async (studio, annotation, params = {}) => {
const annotationDoesntExist = !annotation.pk;
const data = { body: this.prepareData(annotation, { draft: true }) }; // serializedAnnotation
const hasChanges = this.needsDraftSave(annotation);
const showToast = params?.useToast && hasChanges;

Object.assign(data.body, params);

Expand All @@ -712,7 +714,8 @@ export class LSFWrapper {
if (annotation.draftId > 0) {
// draft has been already created
const res = await this.datamanager.apiCall("updateDraft", { draftID: annotation.draftId }, data);


showToast && this.draftToast(res?.$meta?.status);
return res;

} else {
Expand All @@ -728,6 +731,8 @@ export class LSFWrapper {
);
}
response?.id && annotation.setDraftId(response?.id);
showToast && this.draftToast(response?.$meta?.status);

return response;
}
};
Expand Down

0 comments on commit 0f79079

Please sign in to comment.