Skip to content

Commit

Permalink
Move mapping rules to respective component classes
Browse files Browse the repository at this point in the history
  • Loading branch information
holubv committed May 1, 2021
1 parent 375043b commit b31c840
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
29 changes: 4 additions & 25 deletions src/SmartComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,19 @@ export default class SmartComponents {
return [
{
component: AnswerableSectionComposite,
mapRule: q => FormUtils.isSection(q) && FormUtils.isAnswerable(q)
mapRule: AnswerableSectionComposite.mappingRule
},
{
component: CompositeQuestion,
mapRule: q => JsonLdUtils.getJsonAttValue(q, Constants.COMPOSITE_PATTERN)
mapRule: CompositeQuestion.mappingRule
},
{
component: QuestionWithAdvanced,
mapRule: q => {
return SmartComponents._hasAdvancedQuestion(q) && !FormUtils.isWizardStep(q);
}
mapRule: QuestionWithAdvanced.mappingRule
},
{
component: WizardStepWithAdvanced,
mapRule: q => {
return SmartComponents._hasAdvancedQuestion(q) && FormUtils.isWizardStep(q);
}
mapRule: WizardStepWithAdvanced.mappingRule
},
{
component: NullQuestion,
Expand All @@ -74,21 +70,4 @@ export default class SmartComponents {
];
}

static _hasAdvancedQuestion(q) {

if (!FormUtils.isSection(q) && !FormUtils.isAnswerable(q)) {
return false;
}
let subQuestions = q[SConstants.HAS_SUBQUESTION];
if (subQuestions && subQuestions.length) {
for (let subQuestion of subQuestions) {
if (JsonLdUtils.hasValue(subQuestion, Constants.SHOW_ADVANCED_QUESTION, true)) {
return true;
}
}
}
return false;

};

}
15 changes: 15 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Constants as SConstants} from "s-forms";
import JsonLdUtils from "jsonld-utils";

export default class Utils {

Expand Down Expand Up @@ -73,6 +74,20 @@ export default class Utils {
return false;
}

static hasSubQuestionWithValue(parent, property, value) {

let subQuestions = parent[SConstants.HAS_SUBQUESTION];
if (subQuestions && subQuestions.length) {
for (let subQuestion of subQuestions) {
if (JsonLdUtils.hasValue(subQuestion, property, value)) {
return true;
}
}
}

return false;
}

// static _findQuestion(question, id) {
//
// if (question['@id'] === id) {
Expand Down
4 changes: 3 additions & 1 deletion src/components/AnswerableSectionComposite.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import {QuestionWithAdvanced} from "../lib";

export default class AnswerableSectionComposite extends Question {

static mappingRule = q => FormUtils.isSection(q) && FormUtils.isAnswerable(q);

constructor(props) {
super(props);
}

_renderShowAdvanced() {
const question = this.props.question;

if (!SmartComponents._hasAdvancedQuestion(question)) {
if (!QuestionWithAdvanced.mappingRule(question)) {
return null;
}

Expand Down
2 changes: 2 additions & 0 deletions src/components/CompositeQuestion.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Constants from "../Constants";

export default class CompositeQuestion extends Question {

static mappingRule = q => JsonLdUtils.getJsonAttValue(q, Constants.COMPOSITE_PATTERN);

constructor(props) {
super(props);
}
Expand Down
14 changes: 14 additions & 0 deletions src/components/QuestionWithAdvanced.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@ import Constants from "../Constants";
import classNames from 'classnames';
import JsonldUtils from 'jsonld-utils';
import PropTypes from "prop-types";
import Utils from "../Utils";

export default class QuestionWithAdvanced extends Question {

static mappingRule = q => {

if (FormUtils.isWizardStep(q)) {
return false;
}

if (!FormUtils.isSection(q) && !FormUtils.isAnswerable(q)) {
return false;
}

return Utils.hasSubQuestionWithValue(q, Constants.SHOW_ADVANCED_QUESTION, true);
}

static findShowAdvancedQuestion(parent) {

let subQuestions = parent[SConstants.HAS_SUBQUESTION];
Expand Down
13 changes: 12 additions & 1 deletion src/components/WizardStepWithAdvanced.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import React from 'react';
import {WizardStep, Question, Constants as SConstants, HelpIcon} from 's-forms';
import {WizardStep, Question, Constants as SConstants, HelpIcon, FormUtils} from 's-forms';
import {Card, Form} from 'react-bootstrap';
import JsonLdUtils from 'jsonld-utils';
import QuestionWithAdvanced from "./QuestionWithAdvanced";
import Utils from "../Utils";
import Constants from "../Constants";


export default class WizardStepWithAdvanced extends WizardStep {

static mappingRule = q => {

if (!FormUtils.isWizardStep(q)) {
return false;
}

return Utils.hasSubQuestionWithValue(q, Constants.SHOW_ADVANCED_QUESTION, true);
}

_toggleAdvanced = (e) => {
e.stopPropagation();

Expand Down

0 comments on commit b31c840

Please sign in to comment.