Skip to content
Permalink
Browse files

feat(jexl): support jexl referencing TableQuestions (#229)

  • Loading branch information...
czosel committed May 13, 2019
1 parent 7ab76a7 commit 858d95e91ddfc9df90520c1aa5413f3ac948aa8d
@@ -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";

@@ -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*() {
@@ -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", [
@@ -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);
@@ -38,6 +38,7 @@ export default EmberObject.extend({
"listValue",
"fileValue",
"dateValue",
"tableValue",
{
get() {
return this.get(this._valueKey);
@@ -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;
@@ -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;
}

@@ -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 = {
@@ -50,6 +51,7 @@ export default EmberObject.extend(Evented, {
saveDocumentListAnswerMutation,
saveDocumentFileAnswerMutation,
saveDocumentDateAnswerMutation,
saveDocumentTableAnswerMutation,

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

_validateTableQuestion() {
return true;
},

_validateStaticQuestion() {
return true;
}
@@ -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
@@ -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 =
@@ -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.
You can’t perform that action at this time.