-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdocument-delete-button.js
74 lines (59 loc) · 1.8 KB
/
document-delete-button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { task } from "ember-concurrency";
import { ErrorHandler } from "ember-alexandria/utils/error-handler";
export default class DocumentDeleteButtonComponent extends Component {
@service notification;
@service intl;
@service("alexandria-documents") documents;
@service router;
@tracked dialogVisible = false;
@action showDialog() {
this.dialogVisible = true;
}
@action hideDialog() {
this.dialogVisible = false;
}
@action cancel() {
this.hideDialog();
if (this.args.onCancel) {
this.args.onCancel();
}
}
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"),
);
}
this.hideDialog();
});
}