Skip to content
Permalink
Browse files

feat(form): add support for static question (#169)

This implements support for
projectcaluma/caluma#396
  • Loading branch information...
czosel committed Apr 29, 2019
1 parent 2dca978 commit 17afa26e7a0e393691c2641c65353c2e37520985
@@ -8,7 +8,8 @@ const TYPES = [
"MULTIPLE_CHOICE",
"CHOICE",
"TABLE",
"FILE"
"FILE",
"STATIC"
];

export default Factory.extend({
@@ -58,6 +59,12 @@ export default Factory.extend({
rowForm: i => `subform-${i + 1}`
});
}
} else if (question.type === "STATIC") {
if (question.staticContent === undefined) {
question.update({
staticContent: i => `static-${i + 1}`
});
}
}
}
});
@@ -62,6 +62,9 @@ export default {
{
name: "FileQuestion"
},
{
name: "StaticQuestion"
},
{
name: "StringAnswer"
},
@@ -142,6 +145,9 @@ export default {
},
{
name: "FileQuestion"
},
{
name: "StaticQuestion"
}
]
},
@@ -0,0 +1,12 @@
import Component from "@ember/component";
import layout from "../../../templates/components/cf-field/input/static";

/**
* Input component for the static question type
*
* @class CfFieldInputTextComponent
* @argument {Field} field The field for this input type
*/
export default Component.extend({
layout
});
@@ -29,6 +29,7 @@ import saveChoiceQuestionMutation from "ember-caluma/gql/mutations/save-choice-q
import saveTableQuestionMutation from "ember-caluma/gql/mutations/save-table-question";
import saveFormQuestionMutation from "ember-caluma/gql/mutations/save-form-question";
import saveFileQuestionMutation from "ember-caluma/gql/mutations/save-file-question";
import saveStaticQuestionMutation from "ember-caluma/gql/mutations/save-static-question";

export const TYPES = {
TextQuestion: saveTextQuestionMutation,
@@ -39,7 +40,8 @@ export const TYPES = {
ChoiceQuestion: saveChoiceQuestionMutation,
TableQuestion: saveTableQuestionMutation,
FormQuestion: saveFormQuestionMutation,
FileQuestion: saveFileQuestionMutation
FileQuestion: saveFileQuestionMutation,
StaticQuestion: saveStaticQuestionMutation
};

export default Component.extend(ComponentQueryManager, {
@@ -167,6 +169,7 @@ export default Component.extend(ComponentQueryManager, {

_getIntegerQuestionInput(changeset) {
return {
isRequired: changeset.get("isRequired"),
minValue: parseInt(changeset.get("integerMinValue")),
maxValue: parseInt(changeset.get("integerMaxValue")),
placeholder: changeset.get("placeholder")
@@ -175,6 +178,7 @@ export default Component.extend(ComponentQueryManager, {

_getFloatQuestionInput(changeset) {
return {
isRequired: changeset.get("isRequired"),
minValue: parseFloat(changeset.get("floatMinValue")),
maxValue: parseFloat(changeset.get("floatMaxValue")),
placeholder: changeset.get("placeholder")
@@ -183,20 +187,23 @@ export default Component.extend(ComponentQueryManager, {

_getTextQuestionInput(changeset) {
return {
isRequired: changeset.get("isRequired"),
maxLength: parseInt(changeset.get("maxLength")),
placeholder: changeset.get("placeholder")
};
},

_getTextareaQuestionInput(changeset) {
return {
isRequired: changeset.get("isRequired"),
maxLength: parseInt(changeset.get("maxLength")),
placeholder: changeset.get("placeholder")
};
},

_getMultipleChoiceQuestionInput(changeset) {
return {
isRequired: changeset.get("isRequired"),
options: changeset.get("options.edges").map(({ node: { slug } }) => slug),
meta: JSON.stringify({
widgetOverride: changeset.get("widgetOverride"),
@@ -207,6 +214,7 @@ export default Component.extend(ComponentQueryManager, {

_getChoiceQuestionInput(changeset) {
return {
isRequired: changeset.get("isRequired"),
options: changeset.get("options.edges").map(({ node: { slug } }) => slug),
meta: JSON.stringify({
widgetOverride: changeset.get("widgetOverride"),
@@ -217,18 +225,28 @@ export default Component.extend(ComponentQueryManager, {

_getTableQuestionInput(changeset) {
return {
isRequired: changeset.get("isRequired"),
rowForm: changeset.get("rowForm")
};
},

_getFormQuestionInput(changeset) {
return {
isRequired: changeset.get("isRequired"),
subForm: changeset.get("subForm")
};
},

_getFileQuestionInput(/* changeset */) {
return {};
_getFileQuestionInput(changeset) {
return {
isRequired: changeset.get("isRequired")
};
},

_getStaticQuestionInput(changeset) {
return {
staticContent: changeset.get("staticContent")
};
},

saveOptions: task(function*(changeset) {
@@ -261,7 +279,6 @@ export default Component.extend(ComponentQueryManager, {
{
label: changeset.get("label"),
slug,
isRequired: changeset.get("isRequired"),
isHidden: changeset.get("isHidden"),
infoText: changeset.get("infoText"),
meta: JSON.stringify({
@@ -43,6 +43,9 @@ fragment SimpleQuestion on Question {
}
}
}
... on StaticQuestion {
staticContent
}
}

fragment FieldQuestion on Question {
@@ -0,0 +1,13 @@
#import "ember-caluma/gql/fragments/question-info"

mutation SaveStaticQuestion($input: SaveStaticQuestionInput!) {
saveStaticQuestion(input: $input) {
question {
...QuestionInfo
... on StaticQuestion {
staticContent
}
}
clientMutationId
}
}
@@ -53,6 +53,9 @@ query FormEditorQuestion($slug: String!) {
slug
}
}
... on StaticQuestion {
staticContent
}
}
}
}
@@ -27,7 +27,8 @@ const TYPE_MAP = {
ChoiceQuestion: "StringAnswer",
TableQuestion: "TableAnswer",
FormQuestion: "FormAnswer",
FileQuestion: "FileAnswer"
FileQuestion: "FileAnswer",
StaticQuestion: null
};

/**
@@ -81,26 +82,28 @@ export default EmberObject.extend({
})
);

const answer = Answer.create(
getOwner(this).ownerInjection(),
Object.assign(
this._answer || {
__typename,
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
})
)
}
: {}
)
);
const answer =
__typename &&
Answer.create(
getOwner(this).ownerInjection(),
Object.assign(
this._answer || {
__typename,
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
})
)
}
: {}
)
);

this.setProperties({
_errors: [],
@@ -67,6 +67,11 @@ export default class extends BaseMock {
return this.handleInterfaceType(...arguments);
}

@register("StaticQuestion")
handleStaticQuestion() {
return this.handleInterfaceType(...arguments);
}

@register("SaveTextQuestionPayload")
handleSaveTextQuestion(_, { input }) {
return this.handleSavePayload.fn.call(this, _, {
@@ -95,6 +100,13 @@ export default class extends BaseMock {
});
}

@register("SaveStaticQuestionPayload")
handleSaveStaticQuestion(_, { input }) {
return this.handleSavePayload.fn.call(this, _, {
input: { ...input, type: "STATIC" }
});
}

@register("SaveChoiceQuestionPayload")
handleSaveChoiceQuestion(_, { input }) {
const options = input.options.map(slug =>
@@ -144,7 +156,7 @@ export default class extends BaseMock {
}

@register("SaveTableQuestionPayload")
handleSaveTableFloatQuestion(_, { input }) {
handleSaveTableQuestion(_, { input }) {
return this.handleSavePayload.fn.call(this, _, {
input: { ...input, type: "TABLE" }
});

0 comments on commit 17afa26

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