Skip to content

Commit

Permalink
fix(table): fix answer handling of table questions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Metzener authored and fkm committed Jun 19, 2019
1 parent 47a111c commit 7923168
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 36 deletions.
26 changes: 6 additions & 20 deletions addon/components/cf-field/input/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}),
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion addon/gql/fragments/field-answer.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fragment SimpleAnswer on Answer {
fragment FieldAnswer on Answer {
...SimpleAnswer
... on TableAnswer {
value {
tableValue: value {
id
form {
slug
Expand Down
16 changes: 15 additions & 1 deletion addon/lib/answer.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion addon/lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 1 addition & 11 deletions addon/lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 }
)
);

Expand Down
2 changes: 1 addition & 1 deletion addon/templates/components/cf-field/input/table.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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|}}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/components/cf-field/input/table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module("Integration | Component | cf-field/input/table", function(hooks) {
this.set("field", {
id: "table-test",
answer: {
rowDocuments: [
value: [
{
fields: [
{
Expand Down

0 comments on commit 7923168

Please sign in to comment.