Skip to content

Commit d032cf0

Browse files
committed
fix(delete): delete concurrently and clean up correctly
fixes bug when deleting multiple the last one keeps the side panel open, eventhough it does not exist anymore
1 parent 94e2406 commit d032cf0

File tree

3 files changed

+94
-23
lines changed

3 files changed

+94
-23
lines changed

addon/components/document-delete-button.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,31 @@ export default class DocumentDeleteButtonComponent extends Component {
3030
}
3131
}
3232

33-
@task *delete() {
33+
delete = task(async () => {
3434
try {
3535
if (this.args.docsToDelete) {
3636
const docs = Array.isArray(this.args.docsToDelete)
3737
? this.args.docsToDelete
3838
: [this.args.docsToDelete]; // if the supplied argument is not an array we make it one
3939

40-
for (const doc of docs) {
41-
yield doc.destroyRecord().catch((error) => {
42-
doc.rollbackAttributes();
43-
throw error;
44-
});
45-
this.documents.deselectDocument(doc);
40+
if (this.args.onConfirm) {
41+
this.args.onConfirm(docs);
4642
}
47-
}
4843

49-
if (this.args.onConfirm) {
50-
this.args.onConfirm(this.args.docsToDelete);
44+
const deletionStatus = await Promise.allSettled(
45+
docs.map((doc) => {
46+
return doc.destroyRecord();
47+
}),
48+
);
49+
50+
deletionStatus.forEach(({ status, reason }, index) => {
51+
if (status === "rejected") {
52+
docs[index].rollbackAttributes();
53+
throw reason;
54+
}
55+
56+
this.documents.deselectDocument(docs[index]);
57+
});
5158
}
5259

5360
this.notification.success(
@@ -58,5 +65,5 @@ export default class DocumentDeleteButtonComponent extends Component {
5865
} finally {
5966
this.hideDialog();
6067
}
61-
}
68+
});
6269
}

addon/services/alexandria-documents.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { action } from "@ember/object";
22
import Service, { inject as service } from "@ember/service";
3+
import { macroCondition, isTesting } from "@embroider/macros";
34
import { tracked } from "@glimmer/tracking";
45
import { task } from "ember-concurrency";
56
import { saveAs } from "file-saver";
@@ -38,11 +39,23 @@ export default class AlexandriaDocumentsService extends Service {
3839
this.selectedDocuments.length === 0
3940
? undefined
4041
: this.selectedDocuments.map((d) => d.id);
41-
this.router.transitionTo(this.router.currentRouteName, {
42-
queryParams: {
43-
document: docs,
44-
},
45-
});
42+
43+
if (macroCondition(isTesting())) {
44+
// there is no router for integration tests
45+
if (this.router.currentRouteName) {
46+
this.router.transitionTo(this.router.currentRouteName, {
47+
queryParams: {
48+
document: docs,
49+
},
50+
});
51+
}
52+
} else {
53+
this.router.transitionTo(this.router.currentRouteName, {
54+
queryParams: {
55+
document: docs,
56+
},
57+
});
58+
}
4659
}
4760

4861
/**

tests/integration/components/document-delete-button-test.js

+58-7
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@ module("Integration | Component | document-delete-button", function (hooks) {
1010
this.document = {
1111
id: 1,
1212
title: "Test",
13-
destroyRecord() {},
13+
destroyRecord() {
14+
assert.step("destroy test");
15+
},
1416
};
1517

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

1921
await render(hbs`
2022
<DocumentDeleteButton
21-
@document={{this.document}}
23+
@docsToDelete={{this.document}}
2224
@onConfirm={{this.onConfirm}}
2325
@onCancel={{this.onCancel}}
2426
as |showDialog|
2527
>
2628
<button
27-
{{on "click" showDialog}}
28-
data-test-delete
29-
type="button"
30-
>Delete</button>
29+
{{on "click" showDialog}}
30+
data-test-delete
31+
type="button"
32+
>
33+
Delete
34+
</button>
3135
</DocumentDeleteButton>
3236
`);
3337

@@ -37,6 +41,53 @@ module("Integration | Component | document-delete-button", function (hooks) {
3741
await click("[data-test-delete]");
3842
await click("[data-test-delete-confirm]");
3943

40-
assert.verifySteps(["cancel", "confirm"]);
44+
assert.verifySteps(["cancel", "confirm", "destroy test"]);
45+
});
46+
47+
test("delete document multiple", async function (assert) {
48+
this.documents = [
49+
{
50+
id: 1,
51+
title: "Test",
52+
destroyRecord() {
53+
assert.step("destroy test");
54+
},
55+
},
56+
{
57+
id: 2,
58+
title: "Bar",
59+
destroyRecord() {
60+
assert.step("destroy bar");
61+
},
62+
},
63+
];
64+
65+
this.onCancel = () => assert.step("cancel");
66+
this.onConfirm = () => assert.step("confirm");
67+
68+
await render(hbs`
69+
<DocumentDeleteButton
70+
@docsToDelete={{this.documents}}
71+
@onConfirm={{this.onConfirm}}
72+
@onCancel={{this.onCancel}}
73+
as |showDialog|
74+
>
75+
<button
76+
{{on "click" showDialog}}
77+
data-test-delete
78+
type="button"
79+
>
80+
Delete
81+
</button>
82+
</DocumentDeleteButton>
83+
`);
84+
85+
await click("[data-test-delete]");
86+
await click("[data-test-delete-cancel]");
87+
88+
await click("[data-test-delete]");
89+
await click("[data-test-delete-confirm]");
90+
91+
assert.verifySteps(["cancel", "confirm", "destroy test", "destroy bar"]);
4192
});
4293
});

0 commit comments

Comments
 (0)