Skip to content

Commit

Permalink
feat(form): add support for static question
Browse files Browse the repository at this point in the history
This implements support for
projectcaluma/caluma#396
  • Loading branch information
czosel committed Apr 28, 2019
1 parent a646579 commit 864af26
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 41 deletions.
9 changes: 8 additions & 1 deletion addon-mirage-support/factories/question.js
Expand Up @@ -8,7 +8,8 @@ const TYPES = [
"MULTIPLE_CHOICE",
"CHOICE",
"TABLE",
"FILE"
"FILE",
"STATIC"
];

export default Factory.extend({
Expand Down Expand Up @@ -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}`
});
}
}
}
});
6 changes: 6 additions & 0 deletions addon/-private/fragment-types.js
Expand Up @@ -62,6 +62,9 @@ export default {
{
name: "FileQuestion"
},
{
name: "StaticQuestion"
},
{
name: "StringAnswer"
},
Expand Down Expand Up @@ -142,6 +145,9 @@ export default {
},
{
name: "FileQuestion"
},
{
name: "StaticQuestion"
}
]
},
Expand Down
12 changes: 12 additions & 0 deletions addon/components/cf-field/input/static.js
@@ -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
});
25 changes: 21 additions & 4 deletions addon/components/cfb-form-editor/question.js
Expand Up @@ -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,
Expand All @@ -39,7 +40,8 @@ export const TYPES = {
ChoiceQuestion: saveChoiceQuestionMutation,
TableQuestion: saveTableQuestionMutation,
FormQuestion: saveFormQuestionMutation,
FileQuestion: saveFileQuestionMutation
FileQuestion: saveFileQuestionMutation,
StaticQuestion: saveStaticQuestionMutation
};

export default Component.extend(ComponentQueryManager, {
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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) {
Expand Down Expand Up @@ -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({
Expand Down
3 changes: 3 additions & 0 deletions addon/gql/fragments/field-question.graphql
Expand Up @@ -43,6 +43,9 @@ fragment SimpleQuestion on Question {
}
}
}
... on StaticQuestion {
staticContent
}
}

fragment FieldQuestion on Question {
Expand Down
13 changes: 13 additions & 0 deletions addon/gql/mutations/save-static-question.graphql
@@ -0,0 +1,13 @@
#import "ember-caluma/gql/fragments/question-info"

mutation SaveStaticQuestion($input: SaveStaticQuestionInput!) {
saveStaticQuestion(input: $input) {
question {
...QuestionInfo
... on StaticQuestion {
staticContent
}
}
clientMutationId
}
}
3 changes: 3 additions & 0 deletions addon/gql/queries/form-editor-question.graphql
Expand Up @@ -53,6 +53,9 @@ query FormEditorQuestion($slug: String!) {
slug
}
}
... on StaticQuestion {
staticContent
}
}
}
}
Expand Down
45 changes: 24 additions & 21 deletions addon/lib/field.js
Expand Up @@ -27,7 +27,8 @@ const TYPE_MAP = {
ChoiceQuestion: "StringAnswer",
TableQuestion: "TableAnswer",
FormQuestion: "FormAnswer",
FileQuestion: "FileAnswer"
FileQuestion: "FileAnswer",
StaticQuestion: null
};

/**
Expand Down Expand Up @@ -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: [],
Expand Down
14 changes: 13 additions & 1 deletion addon/mirage-graphql/mocks/question.js
Expand Up @@ -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, _, {
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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" }
});
Expand Down

0 comments on commit 864af26

Please sign in to comment.