Skip to content

Commit

Permalink
feat(jexl): support jexl referencing TableQuestions (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel authored May 13, 2019
1 parent 7ab76a7 commit 858d95e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
41 changes: 14 additions & 27 deletions addon/components/cf-field/input/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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 saveDocumentTableAnswerMutation from "ember-caluma/gql/mutations/save-document-table-answer";
import { inject as service } from "@ember/service";
import { ComponentQueryManager } from "ember-apollo-client";

Expand Down Expand Up @@ -52,19 +51,10 @@ export default Component.extend(ComponentQueryManager, {
doc => doc.id !== document.id
);

yield this.get("apollo").mutate({
mutation: saveDocumentTableAnswerMutation,
variables: {
input: {
question: this.get("field.question.slug"),
document: this.get("field.document.id"),
value: remainingDocuments.map(doc => doc.id)
}
}
});

// update client-side state
this.set("field.answer.rowDocuments", remainingDocuments);

yield this.onSave(remainingDocuments.map(doc => doc.id));
}),

save: task(function*() {
Expand All @@ -79,21 +69,12 @@ export default Component.extend(ComponentQueryManager, {

if (!rows.find(doc => doc.id === newDocument.id)) {
// add document to table
yield this.get("apollo").mutate({
mutation: saveDocumentTableAnswerMutation,
variables: {
input: {
question: this.get("field.question.slug"),
document: this.get("field.document.id"),
value: [
...this.getWithDefault("field.answer.rowDocuments", []).map(
doc => doc.id
),
newDocument.id
]
}
}
});
yield this.onSave([
...this.getWithDefault("field.answer.rowDocuments", []).map(
doc => doc.id
),
newDocument.id
]);

// update client-side state
this.set("field.answer.rowDocuments", [
Expand All @@ -103,6 +84,12 @@ export default Component.extend(ComponentQueryManager, {
this.get("notification").success(
this.get("intl").t("caluma.form.notification.table.add.success")
);
} else {
yield this.onSave([
...this.getWithDefault("field.answer.rowDocuments", []).map(
doc => doc.id
)
]);
}

this.set("showModal", false);
Expand Down
1 change: 1 addition & 0 deletions addon/lib/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default EmberObject.extend({
"listValue",
"fileValue",
"dateValue",
"tableValue",
{
get() {
return this.get(this._valueKey);
Expand Down
13 changes: 13 additions & 0 deletions addon/lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export default EmberObject.extend({
questionJexl.addTransform("answer", slugWithPath =>
this.findAnswer(slugWithPath)
);
questionJexl.addTransform("mapby", (arr, key) => {
return arr && arr.map ? arr.map(obj => obj[key]) : null;
});
questionJexl.addBinaryOp("intersects", 20, intersects);

return questionJexl;
Expand All @@ -89,6 +92,16 @@ export default EmberObject.extend({
field.question.__typename == "MultipleChoiceQuestion" ? [] : null;

if (field.answer.value && !field.question.hidden) {
if (field.question.__typename === "TableQuestion") {
return field.getWithDefault("answer.rowDocuments", []).map(doc =>
doc.fields.reduce((obj, field) => {
return {
...obj,
[field.question.slug]: field.answer.value
};
}, {})
);
}
return field.answer.value;
}

Expand Down
6 changes: 6 additions & 0 deletions addon/lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import saveDocumentStringAnswerMutation from "ember-caluma/gql/mutations/save-do
import saveDocumentListAnswerMutation from "ember-caluma/gql/mutations/save-document-list-answer";
import saveDocumentFileAnswerMutation from "ember-caluma/gql/mutations/save-document-file-answer";
import saveDocumentDateAnswerMutation from "ember-caluma/gql/mutations/save-document-date-answer";
import saveDocumentTableAnswerMutation from "ember-caluma/gql/mutations/save-document-table-answer";
import removeAnswerMutation from "ember-caluma/gql/mutations/remove-answer";

const TYPE_MAP = {
Expand Down Expand Up @@ -50,6 +51,7 @@ export default EmberObject.extend(Evented, {
saveDocumentListAnswerMutation,
saveDocumentFileAnswerMutation,
saveDocumentDateAnswerMutation,
saveDocumentTableAnswerMutation,

/**
* The Apollo GraphQL service for making requests
Expand Down Expand Up @@ -433,6 +435,10 @@ export default EmberObject.extend(Evented, {
});
},

_validateTableQuestion() {
return true;
},

_validateStaticQuestion() {
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions addon/lib/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default EmberObject.extend({
* Evaluate the question's hidden state.
*
* A question is hidden if
* * any of the `dependsOn` questions is hidden or empty, or
* * all of the `dependsOn` questions are hidden or empty, or
* * the JEXL expression evaluates to `true`
*
* @method hiddenTask.perform
Expand All @@ -65,8 +65,8 @@ export default EmberObject.extend({
this.dependsOn.every(
field =>
field.question.hidden ||
field.answer.value === null ||
field.answer.value === undefined
(field.question.__typename !== "TableQuestion" &&
(field.answer.value === null || field.answer.value === undefined))
);

hidden =
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/lib/document-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,24 @@ module("Unit | Library | document", function(hooks) {
assert.equal(await this.document.questionJexl.eval(expression), result);
}
});

test("question jexl mapby transform", async function(assert) {
const tests = [
[[{ foo: "bar" }, { foo: "baz" }], "value|mapby('foo')", ["bar", "baz"]],
[
[{ foo: "bar" }, { xy: "baz" }],
"value|mapby('foo')",
["bar", undefined]
],
[null, "value|mapby('foo')", null],
["astring", "value|mapby('foo')", null],
[{ foo: "bar" }, "value|mapby('foo')", null]
];
for (let [value, expression, result] of tests) {
assert.deepEqual(
await this.document.questionJexl.eval(expression, { value }),
result
);
}
});
});

0 comments on commit 858d95e

Please sign in to comment.