Skip to content

Commit

Permalink
[Fix #62] Refactor for readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
LaChope committed Nov 9, 2021
1 parent 730bfd6 commit 6bda35a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/components/Answer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import TypeaheadAnswer from './answer/TypeaheadAnswer';
import Constants from '../constants/Constants';
import { FormGenContext } from '../contexts/FormGenContext';
import { ConfigurationContext } from '../contexts/ConfigurationContext';
import Question from "./Question";

const Answer = (props) => {
const formGenContext = React.useContext(FormGenContext);
Expand Down Expand Up @@ -138,7 +139,7 @@ const Answer = (props) => {
return (
<div className="question-header">
{label}
{props.icons}
{Question.renderIcons(props.question, options, props.onCommentChange)}
</div>
);
}
Expand Down Expand Up @@ -177,7 +178,7 @@ Answer.propTypes = {
onChange: PropTypes.func.isRequired,
onCommentChange: PropTypes.func.isRequired,
index: PropTypes.number,
icons: PropTypes.array
icons: PropTypes.object
};

export default Answer;
51 changes: 26 additions & 25 deletions src/components/Question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ export default class Question extends React.Component {
question={question}
onChange={this.onAnswerChange}
onCommentChange={this.onCommentChange}
icons={Question.renderIcons(question, options, this.onCommentChange)}
/>
</div>
{this._renderUnits()}
Expand Down Expand Up @@ -313,42 +312,44 @@ export default class Question extends React.Component {
);
}

static renderQuestionHelp(question, options) {
const icons = options.icons;
let questionHelpIcon;

for (let i = 0; i < icons.length; i++) {
if (icons[i].id === Constants.ICONS.QUESTION_HELP) {
questionHelpIcon = icons[i];
}
}

if (!questionHelpIcon || questionHelpIcon.behavior === Constants.ICON_BEHAVIOR.ENABLE) {
if (question[Constants.HELP_DESCRIPTION]) {
static evaluateIcon(icon, question, options, onCommentChange) {
if (icon && icon.behavior === Constants.ICON_BEHAVIOR.ENABLE) {
if (icon.id === Constants.ICONS.QUESTION_HELP && question[Constants.HELP_DESCRIPTION]) {
return <HelpIcon
text={JsonLdUtils.getLocalized(question[Constants.HELP_DESCRIPTION], options.intl)}
absolutePosition={false}/>
}

if (icon.id === Constants.ICONS.QUESTION_COMMENTS) {
return <QuestionCommentIcon
question={question}
onChange={onCommentChange}/>
}
}
return null;
}

static renderQuestionComments = (question, options, onChange) => {
const icons = options.icons
let questionCommentsIcon;
static getIconFromIconList = (iconList, iconName) => {
let icon;

for (let i = 0; i < icons.length; i++) {
if (icons[i].id === Constants.ICONS.QUESTION_COMMENTS) {
questionCommentsIcon = icons[i];
for (let i = 0; i < iconList.length; i++) {
if (iconList[i].id === iconName) {
icon = iconList[i];
}
}
return icon;
}

if (questionCommentsIcon && questionCommentsIcon.behavior === Constants.ICON_BEHAVIOR.ENABLE) {
return <QuestionCommentIcon
question={question}
onChange={onChange}/>
}
return null;
static renderQuestionHelp(question, options) {
const icons = options.icons;
const questionHelpIcon = this.getIconFromIconList(icons, Constants.ICONS.QUESTION_HELP)
return this.evaluateIcon(questionHelpIcon, question, options);
}

static renderQuestionComments = (question, options, onCommentChange) => {
const icons = options.icons;
const questionCommentsIcon = this.getIconFromIconList(icons, Constants.ICONS.QUESTION_COMMENTS)
return this.evaluateIcon(questionCommentsIcon, question, options, onCommentChange);
}

static renderIcons(question, options, onCommentChange) {
Expand Down

0 comments on commit 6bda35a

Please sign in to comment.