Skip to content

Commit

Permalink
[Upd #52] Refactored for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
LaChope committed Jan 31, 2022
1 parent 56efc32 commit f6bfe62
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
11 changes: 5 additions & 6 deletions src/components/Question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ export default class Question extends React.Component {
const question = this.props.question;
const subQuestion = question[Constants.HAS_SUBQUESTION];
const options = this.context.options;
const renderQuestion = this.renderQuestion(question);
const renderQuestion = this.questionComponent(question);
if (FormUtils.isHidden(question)) {
return null;
}
if (!FormUtils.isRelevant(question) &&
(this.context.options.debugMode || FormUtils.isQuestionMatchingStartingQuestionId(subQuestion, options.startingQuestionId))) {
(this.context.options.debugMode || JsonLdObjectUtils.checkId(subQuestion, options.startingQuestionId))) {
return (
<div className="show-irrelevant">
{renderQuestion}
Expand All @@ -141,7 +141,7 @@ export default class Question extends React.Component {
return renderQuestion;
}

renderQuestion(question) {
questionComponent(question) {
if (FormUtils.isAnswerable(question) && !FormUtils.isSection(question)) {
if (PRETTY_ANSWERABLE_LAYOUT) {
return (
Expand Down Expand Up @@ -268,7 +268,7 @@ export default class Question extends React.Component {
const startingQuestionId = this.context.options.startingQuestionId;
const subQuestion = question[Constants.HAS_SUBQUESTION]

if ((debugMode || FormUtils.isQuestionMatchingStartingQuestionId(subQuestion, startingQuestionId)) && !FormUtils.hasAnswer(question)) {
if ((debugMode || JsonLdObjectUtils.checkId(subQuestion, startingQuestionId)) && !FormUtils.hasAnswer(question)) {
return "show-irrelevant";
}
return "";
Expand Down Expand Up @@ -513,11 +513,10 @@ export default class Question extends React.Component {

for (let i = 0; i < subQuestions.length; i++) {
let question = subQuestions[i];
let questionId = question['@id'];
let component = this.context.mapComponent(question, Question);
let element = null;

if (debugMode || classname !== "show-irrelevant" || (!debugMode && questionId === startingQuestionId)) {
if (debugMode || classname !== "show-irrelevant" || (!debugMode && JsonLdObjectUtils.checkId(question, startingQuestionId))) {
element = React.createElement(component, {
key: 'sub-question-' + i,
question: question,
Expand Down
13 changes: 0 additions & 13 deletions src/util/FormUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,4 @@ export default class FormUtils {

return false;
}

/**
* Evaluates if question is equal to the starting question ID, which we want to jump to.
* @param question
* @param startingQuestionId
* @returns {boolean}
*/
static isQuestionMatchingStartingQuestionId(question, startingQuestionId) {
if (question === undefined) {
return;
}
return !!question.find(o => o['@id'] === startingQuestionId);
}
}
18 changes: 18 additions & 0 deletions src/util/JsonLdObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,22 @@ export default class JsonLdObjectUtils {
static orderByLocalizedLabels(data, intl) {
return data.sort(JsonLdObjectUtils.getCompareLocalizedLabelFunction(intl));
}

/**
* Evaluates if jsonLdObject is equal to id.
* @param jsonLdObject
* @param id
* @returns {boolean}
*/
static checkId(jsonLdObject, id) {
if (jsonLdObject === undefined) {
return;
}
if (jsonLdObject.constructor === Array) {
return !!jsonLdObject.find(o => o['@id'] === id);
}
if (jsonLdObject.constructor === Object) {
return jsonLdObject['@id'] === id;
}
}
}

0 comments on commit f6bfe62

Please sign in to comment.