-
Notifications
You must be signed in to change notification settings - Fork 22
/
table.js
106 lines (88 loc) · 2.92 KB
/
table.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import Component from "@ember/component";
import layout from "../../../templates/components/cf-field/input/table";
import { task, all } from "ember-concurrency";
import saveDocumentMutation from "ember-caluma/gql/mutations/save-document";
import { inject as service } from "@ember/service";
import { ComponentQueryManager } from "ember-apollo-client";
import { computed } from "@ember/object";
import { getOwner } from "@ember/application";
import Document from "ember-caluma/lib/document";
import { parseDocument } from "ember-caluma/lib/parsers";
export default Component.extend(ComponentQueryManager, {
layout,
notification: service(),
intl: service(),
showModal: false,
documentToEdit: null,
columnHeaders: computed(
"field.question.{rowForm.questions.edges.@each,meta.columnsToDisplay.[]}",
function() {
if (this.get("field.question.meta.columnsToDisplay.length")) {
return this.get("field.question.rowForm.questions.edges").filter(n =>
this.get("field.question.meta.columnsToDisplay").includes(n.node.slug)
);
}
return this.get("field.question.rowForm.questions.edges").slice(0, 4);
}
),
addRow: task(function*() {
const raw = yield this.get("apollo").mutate(
{
mutation: saveDocumentMutation,
variables: {
input: { form: this.get("field.question.rowForm.slug") }
}
},
"saveDocument.document"
);
const newDocument = Document.create(getOwner(this).ownerInjection(), {
raw: parseDocument(raw)
});
this.setProperties({
documentToEdit: newDocument,
showModal: true
});
}).drop(),
deleteRow: task(function*(document) {
if (!this.field.answer.value) return;
const remainingDocuments = this.field.answer.value.filter(
doc => doc.id !== document.id
);
yield this.onSave(remainingDocuments);
}),
save: task(function*() {
try {
const newDocument = this.get("documentToEdit");
yield all(newDocument.fields.map(f => f.validate.perform()));
if (newDocument.fields.some(field => field.isInvalid)) {
return;
}
const rows = this.get("field.answer.value") || [];
if (!rows.find(doc => doc.uuid === newDocument.uuid)) {
// add document to table
yield this.onSave([...rows, newDocument]);
this.get("notification").success(
this.get("intl").t("caluma.form.notification.table.add.success")
);
} else {
// TODO: delete dangling document
yield this.onSave([...rows]);
}
this.set("showModal", false);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
this.get("notification").danger(
this.get("intl").t("caluma.form.notification.table.add.error")
);
}
}),
actions: {
editRow(document) {
this.setProperties({
documentToEdit: document,
showModal: true
});
}
}
});