Skip to content

Commit

Permalink
fix(form): fix cf-field-value for choice and multiple choice questions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Metzener authored and anehx committed Nov 28, 2019
1 parent f7315f5 commit 2afabb9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
27 changes: 5 additions & 22 deletions addon/components/cf-field-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,27 @@ import moment from "moment";
import Component from "@ember/component";
import layout from "../templates/components/cf-field-value";
import { computed } from "@ember/object";
import { camelize } from "@ember/string";
import { queryManager } from "ember-apollo-client";
import getFileAnswerInfoQuery from "ember-caluma/gql/queries/get-fileanswer-info";

function getOptionKey(questionType) {
return `${camelize(questionType.replace(/Question$/, ""))}Options`;
}

export default Component.extend({
layout,

apollo: queryManager(),

tagName: "span",

value: computed("field.answer.value", function() {
const field = this.get("field");
const options = field.question[getOptionKey(field.question.__typename)];
value: computed("field.{selected,answer.value}", function() {
const field = this.field;

switch (field.question.__typename) {
case "ChoiceQuestion":
case "DynamicChoiceQuestion": {
const option = options.edges.find(
edge => edge.node.slug === field.answer.value
);
return { label: option ? option.node.label : field.answer.value };
return field.selected;
}
case "MultipleChoiceQuestion":
case "DynamicMultipleChoiceQuestion": {
const answerValue = field.answer.value || [];
const selectedOptions = options.edges.filter(edge =>
answerValue.includes(edge.node.slug)
);
return {
label:
selectedOptions && selectedOptions.length
? selectedOptions.map(edge => edge.node.label).join(", ")
: answerValue.join(", ")
};
return { label: field.selected.map(({ label }) => label).join(", ") };
}
case "FileQuestion": {
const answerValue = field.answer.value;
Expand All @@ -61,6 +43,7 @@ export default Component.extend({
};
}
}),

actions: {
async download(fileAnswerId) {
const { downloadUrl } = await this.apollo.watchQuery(
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/components/cf-field-value-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module("Integration | Component | cf-field-value", function(hooks) {

test("it renders multiple choice questions", async function(assert) {
this.set("field", {
selected: [
{ slug: "option-a", label: "A" },
{ slug: "option-b", label: "B" }
],
question: {
__typename: "MultipleChoiceQuestion",
multipleChoiceOptions: {
Expand Down Expand Up @@ -45,6 +49,7 @@ module("Integration | Component | cf-field-value", function(hooks) {

test("it renders choice questions", async function(assert) {
this.set("field", {
selected: { slug: "option-c", label: "C" },
question: {
__typename: "ChoiceQuestion",
choiceOptions: {
Expand Down

0 comments on commit 2afabb9

Please sign in to comment.