Skip to content
Permalink
Browse files

fix(table): fix answer handling of table questions

  • Loading branch information...
anehx authored and fkm committed Jun 18, 2019
1 parent 47a111c commit 792316836d5de7dfc019c7e6dc3172be2af952f7
@@ -64,12 +64,12 @@ export default Component.extend(ComponentQueryManager, {
}).drop(),

deleteRow: task(function*(document) {
const remainingDocuments = this.get("field.answer.rowDocuments").filter(
const remainingDocuments = (this.get("field.answer.value") || []).filter(
doc => doc.id !== document.id
);

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

yield this.onSave(remainingDocuments.map(doc => doc.id));
}),
@@ -82,31 +82,17 @@ export default Component.extend(ComponentQueryManager, {
return;
}

const rows = this.getWithDefault("field.answer.rowDocuments", []);
const rows = this.get("field.answer.value") || [];

if (!rows.find(doc => doc.id === newDocument.id)) {
// add document to table
yield this.onSave([
...this.getWithDefault("field.answer.rowDocuments", []).map(
doc => doc.id
),
newDocument.id
]);

// update client-side state
this.set("field.answer.rowDocuments", [
...(this.get("field.answer.rowDocuments") || []),
newDocument
]);
yield this.onSave([...rows.map(doc => doc.id), newDocument.id]);

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
)
]);
yield this.onSave([...rows.map(doc => doc.id)]);
}

this.set("showModal", false);
@@ -33,7 +33,7 @@ fragment SimpleAnswer on Answer {
fragment FieldAnswer on Answer {
...SimpleAnswer
... on TableAnswer {
value {
tableValue: value {
id
form {
slug
@@ -1,13 +1,16 @@
import EmberObject, { computed } from "@ember/object";
import { camelize } from "@ember/string";
import { next } from "@ember/runloop";
import { inject as service } from "@ember/service";

/**
* Object which represents an answer in context of a field
*
* @class Answer
*/
export default EmberObject.extend({
documentStore: service(),

/**
* The name of the property in which the value is stored. This depends on the
* type of the answer.
@@ -41,7 +44,18 @@ export default EmberObject.extend({
"tableValue",
{
get() {
return this.get(this._valueKey);
const value = this.get(this._valueKey);

if (this.__typename === "TableAnswer") {
return (
value &&
value.map(raw =>
this.documentStore.find(raw, { parentDocument: this.document })
)
);
}

return value;
},
set(_, value) {
if (this._valueKey) {
@@ -140,7 +140,7 @@ export default EmberObject.extend({

if (field.answer.value && !field.question.hidden) {
if (field.question.__typename === "TableQuestion") {
return field.getWithDefault("answer.rowDocuments", []).map(doc =>
return (field.get("answer.value") || []).map(doc =>
doc.fields.reduce((obj, field) => {
return {
...obj,
@@ -11,7 +11,6 @@ import Evented, { on } from "@ember/object/evented";

import Answer from "ember-caluma/lib/answer";
import Question from "ember-caluma/lib/question";
import Document from "ember-caluma/lib/document";
import { decodeId } from "ember-caluma/helpers/decode-id";

import saveDocumentFloatAnswerMutation from "ember-caluma/gql/mutations/save-document-float-answer";
@@ -102,16 +101,7 @@ export default EmberObject.extend(Evented, {
question: { slug: this._question.slug },
[camelize(__typename.replace(/Answer$/, "Value"))]: null
},
{ document: this.document, field: this },
this._answer && Array.isArray(this._answer.value)
? {
rowDocuments: this._answer.value.map(document =>
Document.create(getOwner(this).ownerInjection(), {
raw: document
})
)
}
: {}
{ document: this.document, field: this }
)
);

@@ -9,7 +9,7 @@
</tr>
</thead>
<tbody>
{{#each field.answer.rowDocuments as |document|}}
{{#each field.answer.value as |document|}}
<tr>
{{#if field.question.meta.columnsToDisplay.length}}
{{#each document.fields as |answerField|}}
@@ -10,7 +10,7 @@ module("Integration | Component | cf-field/input/table", function(hooks) {
this.set("field", {
id: "table-test",
answer: {
rowDocuments: [
value: [
{
fields: [
{

0 comments on commit 7923168

Please sign in to comment.
You can’t perform that action at this time.