Skip to content

Commit

Permalink
fix(marks): reset marks if request failed
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Dec 5, 2023
1 parent 9789ed1 commit 9ea5a1f
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions addon/services/marks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,25 @@ export default class MarksService extends Service {
marks = findAll(this, "mark");

/**
* Adds a mark to a document. Returns the added mark
* Adds a mark to a document.
*
* @param {Object} document The target document.
* @param {Object|String} mark Either e mark instance or a name.
* @returns {Object} The added mark
*/
@action async add(document, mark) {
const marks = await document.marks;
const marks = [...(await document.marks)];

if (marks.find((m) => m.id === mark.id)) {
return mark;
return;
}

marks.push(mark);
try {
document.marks = [...marks, mark];
await document.save();
} catch (error) {
document.marks = marks;
new ErrorHandler(this, error).notify("alexandria.errors.update");
}

return mark;
}

/**
Expand All @@ -40,10 +38,17 @@ export default class MarksService extends Service {
* @param {Object|String} mark Either e mark instance or a name.
*/
@action async remove(document, mark) {
document.marks = (await document.marks).filter((t) => t !== mark);
const marks = [...(await document.marks)];

if (!marks.find((m) => m.id === mark.id)) {
return;
}

try {
document.marks = marks.filter((m) => m !== mark);
await document.save();
} catch (error) {
document.marks = marks;
new ErrorHandler(this, error).notify("alexandria.errors.update");
}
}
Expand Down

0 comments on commit 9ea5a1f

Please sign in to comment.