Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(delete): delete concurrently and clean up correctly #771

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions addon/components/document-delete-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,45 @@ export default class DocumentDeleteButtonComponent extends Component {
}
}

@task *delete() {
try {
if (this.args.docsToDelete) {
const docs = Array.isArray(this.args.docsToDelete)
? this.args.docsToDelete
: [this.args.docsToDelete]; // if the supplied argument is not an array we make it one

for (const doc of docs) {
yield doc.destroyRecord().catch((error) => {
doc.rollbackAttributes();
throw error;
});
this.documents.deselectDocument(doc);
}
}

if (this.args.onConfirm) {
this.args.onConfirm(this.args.docsToDelete);
}
delete = task(async () => {
if (!this.args.docsToDelete) {
return this.hideDialog();
}

const docs = Array.isArray(this.args.docsToDelete)
? this.args.docsToDelete
: [this.args.docsToDelete]; // if the supplied argument is not an array we make it one

if (this.args.onConfirm) {
this.args.onConfirm(docs);
}

const deletionStatus = await Promise.allSettled(
docs.map((doc) => {
return doc.destroyRecord();
}),
);

const success = [];
const rejected = [];
deletionStatus.forEach((element) => {
(element.status === "rejected" ? rejected : success).push(element);
});

success.forEach((_, index) => {
this.documents.deselectDocument(docs[index]);
});

rejected.forEach((error, index) => {
docs[index].rollbackAttributes();
new ErrorHandler(this, error).notify("alexandria.errors.delete-document");
});

if (!rejected.length) {
this.notification.success(
this.intl.t("alexandria.success.delete-document"),
);
} catch (error) {
new ErrorHandler(this, error).notify("alexandria.errors.delete-document");
} finally {
this.hideDialog();
}
}
this.hideDialog();
});
}
1 change: 1 addition & 0 deletions addon/services/alexandria-documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default class AlexandriaDocumentsService extends Service {
this.selectedDocuments.length === 0
? undefined
: this.selectedDocuments.map((d) => d.id);

this.router.transitionTo(this.router.currentRouteName, {
queryParams: {
document: docs,
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/components/document-card-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module("Integration | Component | document-card", function (hooks) {
});

test("delete file", async function (assert) {
const transitionTo = fake();
this.owner.lookup("service:router").transitionTo = transitionTo;

const destroy = fake();
this.document = {
id: 1,
Expand All @@ -36,6 +39,7 @@ module("Integration | Component | document-card", function (hooks) {
await click("[data-test-delete]");
await click("[data-test-delete-confirm]");
assert.ok(destroy.calledOnce, "destroyRecord was called once");
assert.ok(transitionTo.calledOnce, "transitionTo was called once");
});

test("thumbnail", async function (assert) {
Expand Down
70 changes: 63 additions & 7 deletions tests/integration/components/document-delete-button-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,88 @@ import { render, click } from "@ember/test-helpers";
import { setupRenderingTest } from "dummy/tests/helpers";
import { hbs } from "ember-cli-htmlbars";
import { module, test } from "qunit";
import { fake } from "sinon";

module("Integration | Component | document-delete-button", function (hooks) {
setupRenderingTest(hooks);

hooks.beforeEach(function () {
this.owner.lookup("service:router").transitionTo = fake();
});

test("delete document", async function (assert) {
this.document = {
id: 1,
title: "Test",
destroyRecord() {},
destroyRecord() {
assert.step("destroy test");
},
};

this.onCancel = () => assert.step("cancel");
this.onConfirm = () => assert.step("confirm");

await render(hbs`
<DocumentDeleteButton
@document={{this.document}}
@docsToDelete={{this.document}}
@onConfirm={{this.onConfirm}}
@onCancel={{this.onCancel}}
as |showDialog|
>
<button
{{on "click" showDialog}}
data-test-delete
type="button"
>
Delete
</button>
</DocumentDeleteButton>
`);

await click("[data-test-delete]");
await click("[data-test-delete-cancel]");

await click("[data-test-delete]");
await click("[data-test-delete-confirm]");

assert.verifySteps(["cancel", "confirm", "destroy test"]);
});

test("delete document multiple", async function (assert) {
this.documents = [
{
id: 1,
title: "Test",
destroyRecord() {
assert.step("destroy test");
},
},
{
id: 2,
title: "Bar",
destroyRecord() {
assert.step("destroy bar");
},
},
];

this.onCancel = () => assert.step("cancel");
this.onConfirm = () => assert.step("confirm");

await render(hbs`
<DocumentDeleteButton
@docsToDelete={{this.documents}}
@onConfirm={{this.onConfirm}}
@onCancel={{this.onCancel}}
as |showDialog|
>
<button
{{on "click" showDialog}}
data-test-delete
type="button"
>Delete</button>
{{on "click" showDialog}}
data-test-delete
type="button"
>
Delete
</button>
</DocumentDeleteButton>
`);

Expand All @@ -37,6 +93,6 @@ module("Integration | Component | document-delete-button", function (hooks) {
await click("[data-test-delete]");
await click("[data-test-delete-confirm]");

assert.verifySteps(["cancel", "confirm"]);
assert.verifySteps(["cancel", "confirm", "destroy test", "destroy bar"]);
});
});
5 changes: 5 additions & 0 deletions tests/integration/components/single-document-details-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ module("Integration | Component | single-document-details", function (hooks) {
});

test("delete document", async function (assert) {
const transitionTo = fake();
this.owner.lookup("service:router").transitionTo = transitionTo;

const destroy = fake();
this.selectedDocument = {
id: 1,
title: "Test",
destroyRecord: async () => destroy(),
};

await render(
hbs`<SingleDocumentDetails @document={{this.selectedDocument}}/>`,
);
Expand All @@ -65,6 +69,7 @@ module("Integration | Component | single-document-details", function (hooks) {
await click("[data-test-delete-confirm]");

assert.ok(destroy.calledOnce, "destroyRecord was called once");
assert.ok(transitionTo.calledOnce, "transitionTo was called once");
});

test("edit document title", async function (assert) {
Expand Down
Loading