diff --git a/addon/components/cf-field/input/table.js b/addon/components/cf-field/input/table.js index 8fafcd32b..fb689a6d3 100644 --- a/addon/components/cf-field/input/table.js +++ b/addon/components/cf-field/input/table.js @@ -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); diff --git a/addon/lib/answer.js b/addon/lib/answer.js index 22c58be41..c2bbd701f 100644 --- a/addon/lib/answer.js +++ b/addon/lib/answer.js @@ -38,6 +38,7 @@ export default EmberObject.extend({ "listValue", "fileValue", "dateValue", + "tableValue", { get() { return this.get(this._valueKey); diff --git a/addon/lib/document.js b/addon/lib/document.js index f21933f94..01b1db45a 100644 --- a/addon/lib/document.js +++ b/addon/lib/document.js @@ -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; } diff --git a/addon/lib/field.js b/addon/lib/field.js index 0b5eebbb0..75f8c5bf1 100644 --- a/addon/lib/field.js +++ b/addon/lib/field.js @@ -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; } diff --git a/addon/lib/question.js b/addon/lib/question.js index 1b291823a..b5a3f5d50 100644 --- a/addon/lib/question.js +++ b/addon/lib/question.js @@ -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 = diff --git a/tests/unit/lib/document-test.js b/tests/unit/lib/document-test.js index 2251e8cfc..74c535e54 100644 --- a/tests/unit/lib/document-test.js +++ b/tests/unit/lib/document-test.js @@ -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 + ); + } + }); });