Skip to content

Commit

Permalink
[Upd #279] Split constants and vocabulary (model) into two distinct f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
LaChope committed Apr 9, 2024
1 parent 4b80939 commit fb8d67b
Show file tree
Hide file tree
Showing 30 changed files with 591 additions and 573 deletions.
2 changes: 1 addition & 1 deletion .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Constants from "../src/constants/Constants";
import Constants from "../src/constants/Constants.js";
import IntlContextProvider from "../src/contexts/IntlContextProvider";
import { FormGenContextProvider } from "../src/contexts/FormGenContext";
import { ConfigurationContextProvider } from "../src/contexts/ConfigurationContext";
Expand Down
28 changes: 14 additions & 14 deletions src/components/Answer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SelectAnswer from "./answer/SelectAnswer";
import FormUtils from "../util/FormUtils";
import Utils from "../util/Utils";
import TypeaheadAnswer from "./answer/TypeaheadAnswer";
import Constants from "../constants/Constants";
import Vocabulary from "../constants/Vocabulary.js";
import { FormGenContext } from "../contexts/FormGenContext";
import { ConfigurationContext } from "../contexts/ConfigurationContext";
import QuestionStatic from "./QuestionStatic.jsx";
Expand All @@ -36,25 +36,25 @@ const Answer = (props) => {

const _setValue = (change, value) => {
if (value === null) {
change[Constants.HAS_OBJECT_VALUE] = null;
change[Constants.HAS_DATA_VALUE] = null;
change[Vocabulary.HAS_OBJECT_VALUE] = null;
change[Vocabulary.HAS_DATA_VALUE] = null;
} else if (
props.answer[Constants.HAS_OBJECT_VALUE] ||
props.answer[Vocabulary.HAS_OBJECT_VALUE] ||
FormUtils.isTypeahead(props.question)
) {
change[Constants.HAS_OBJECT_VALUE] = {
change[Vocabulary.HAS_OBJECT_VALUE] = {
"@id": value,
};
} else {
change[Constants.HAS_DATA_VALUE] = {
change[Vocabulary.HAS_DATA_VALUE] = {
"@value": value,
};
}
};

const _hasOptions = (item) => {
return (
item[Constants.HAS_OPTION] && item[Constants.HAS_OPTION].length !== 0
item[Vocabulary.HAS_OPTION] && item[Vocabulary.HAS_OPTION].length !== 0
);
};

Expand Down Expand Up @@ -177,7 +177,7 @@ const Answer = (props) => {

const _getLabel = (question) => {
const label = JsonldUtils.getLocalized(
question[Constants.RDFS_LABEL],
question[Vocabulary.RDFS_LABEL],
options.intl
);

Expand All @@ -200,17 +200,17 @@ const Answer = (props) => {
classname: "",
message: null,
});
if (question[Constants.HAS_VALID_ANSWER] === false) {
if (question[Vocabulary.HAS_VALID_ANSWER] === false) {
if (
question[Constants.HAS_VALIDATION_SEVERITY] ===
Constants.VALIDATION_SEVERITY.WARNING
question[Vocabulary.HAS_VALIDATION_SEVERITY] ===
Vocabulary.VALIDATION_SEVERITY.WARNING
) {
setValidation({
severity: "warning",
classname: "is-warning",
message: (
<FormText className="is-warning">
{question[Constants.HAS_VALIDATION_MESSAGE]}
{question[Vocabulary.HAS_VALIDATION_MESSAGE]}
</FormText>
),
});
Expand All @@ -220,7 +220,7 @@ const Answer = (props) => {
classname: "is-invalid",
message: (
<FormText className="is-invalid">
{question[Constants.HAS_VALIDATION_MESSAGE]}
{question[Vocabulary.HAS_VALIDATION_MESSAGE]}
</FormText>
),
});
Expand All @@ -233,7 +233,7 @@ const Answer = (props) => {

const label = _getLabel(question);
const title = JsonldUtils.getLocalized(
question[Constants.RDFS_COMMENT],
question[Vocabulary.RDFS_COMMENT],
options.intl
);
let component;
Expand Down
4 changes: 2 additions & 2 deletions src/components/MediaContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import Constants from "../constants/Constants";
import Vocabulary from "../constants/Vocabulary";

interface Props {
question: object;
Expand All @@ -8,7 +8,7 @@ interface Props {
const MediaContent = ({ question }: Props) => {
const renderMedia = () => {
// @ts-ignore
const mediaContent = question[Constants.HAS_MEDIA_CONTENT];
const mediaContent = question[Vocabulary.HAS_MEDIA_CONTENT];

if (mediaContent) {
if (Array.isArray(mediaContent)) {
Expand Down
9 changes: 5 additions & 4 deletions src/components/PrefixIcon.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
OverlayTrigger,
Popover,
} from "react-bootstrap";
import Constants from "../constants/Constants";
import Vocabulary from "../constants/Vocabulary.js";

class PrefixIcon extends React.Component {
constructor(props) {
Expand All @@ -22,13 +22,14 @@ class PrefixIcon extends React.Component {
<ListGroup>
{this.state.prefixes
.sort((l, r) => {
const res = l[Constants.PREFIX] < r[Constants.PREFIX];
const res = l[Vocabulary.PREFIX] < r[Vocabulary.PREFIX];
if (res) return -1;
return 1;
})
.map((p) => (
<ListGroupItem key={p[Constants.PREFIX]}>
<strong>{p[Constants.PREFIX]}</strong>: {p[Constants.NAMESPACE]}
<ListGroupItem key={p[Vocabulary.PREFIX]}>
<strong>{p[Vocabulary.PREFIX]}</strong>:{" "}
{p[Vocabulary.NAMESPACE]}
</ListGroupItem>
))}
</ListGroup>
Expand Down
68 changes: 34 additions & 34 deletions src/components/Question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Card, Accordion } from "react-bootstrap";
import * as JsonLdUtils from "jsonld-utils";
import PropTypes from "prop-types";
import Answer from "./Answer";
import Constants from "../constants/Constants";
import Vocabulary from "../constants/Vocabulary.js";
import FormUtils from "../util/FormUtils";
import JsonLdObjectMap from "../util/JsonLdObjectMap";
import QuestionAnswerProcessor from "../model/QuestionAnswerProcessor";
Expand Down Expand Up @@ -43,7 +43,7 @@ export default class Question extends React.Component {
componentDidUpdate() {
const question = this.props.question;
const startingQuestionId = this.context.options.startingQuestionId;
const subQuestions = question[Constants.HAS_SUBQUESTION];
const subQuestions = question[Vocabulary.HAS_SUBQUESTION];
const isSubQuestionStartingQuestionId = subQuestions.find(
(o) => o["@id"] === startingQuestionId
);
Expand Down Expand Up @@ -71,21 +71,21 @@ export default class Question extends React.Component {
this.setState({ expanded: expanded });
}

this._handleChange(Constants.HAS_ANSWER, answerIndex, change);
this._handleChange(Vocabulary.HAS_ANSWER, answerIndex, change);
};

handleSubQuestionChange = (subQuestionIndex, change) => {
this._handleChange(Constants.HAS_SUBQUESTION, subQuestionIndex, change);
this._handleChange(Vocabulary.HAS_SUBQUESTION, subQuestionIndex, change);
};

handleCommentChange = (commentIndex, change) => {
this._handleChange(Constants.HAS_COMMENT, commentIndex, change);
this._handleChange(Vocabulary.HAS_COMMENT, commentIndex, change);
};

_handleChange(att, valueIndex, newValue) {
let newState = { ...this.props.question };
newState[att][valueIndex] = newValue;
if (att === Constants.HAS_ANSWER) {
if (att === Vocabulary.HAS_ANSWER) {
const result = this.state.validator(newValue);
newState = { ...newState, ...result };
}
Expand Down Expand Up @@ -320,7 +320,7 @@ export default class Question extends React.Component {
getShowIrrelevantClassname(question) {
const debugMode = this.context.options.debugMode;
const startingQuestionId = this.context.options.startingQuestionId;
const subQuestion = question[Constants.HAS_SUBQUESTION];
const subQuestion = question[Vocabulary.HAS_SUBQUESTION];

if (
(debugMode ||
Expand Down Expand Up @@ -378,27 +378,27 @@ export default class Question extends React.Component {

_getAnswers() {
const question = this.props.question;
if (!question[Constants.HAS_ANSWER]) {
question[Constants.HAS_ANSWER] = [];
if (!question[Vocabulary.HAS_ANSWER]) {
question[Vocabulary.HAS_ANSWER] = [];
}
if (!Array.isArray(question[Constants.HAS_ANSWER])) {
question[Constants.HAS_ANSWER] = [question[Constants.HAS_ANSWER]];
if (!Array.isArray(question[Vocabulary.HAS_ANSWER])) {
question[Vocabulary.HAS_ANSWER] = [question[Vocabulary.HAS_ANSWER]];
}
if (question[Constants.HAS_ANSWER].length === 0) {
if (question[Vocabulary.HAS_ANSWER].length === 0) {
if (FormUtils.isSection(question) && !FormUtils.isAnswerable(question)) {
question[Constants.HAS_ANSWER] = [];
question[Vocabulary.HAS_ANSWER] = [];
} else {
question[Constants.HAS_ANSWER] = [
question[Vocabulary.HAS_ANSWER] = [
QuestionAnswerProcessor.generateAnswer(question),
];
}
}
return question[Constants.HAS_ANSWER];
return question[Vocabulary.HAS_ANSWER];
}

_getAnswerWidthStyle() {
const length = Number(
this.props.question[Constants.HAS_INITIAL_INPUT_LENGTH]
this.props.question[Vocabulary.HAS_INITIAL_INPUT_LENGTH]
);
if (!length) {
return {};
Expand All @@ -414,9 +414,9 @@ export default class Question extends React.Component {
static _getAnswerClass(question, isTextarea) {
let columns = isTextarea
? "col-12"
: Constants.GENERATED_ROW_SIZE === 1
: Vocabulary.GENERATED_ROW_SIZE === 1
? "col-6"
: "col-" + Constants.COLUMN_COUNT / Constants.GENERATED_ROW_SIZE;
: "col-" + Vocabulary.COLUMN_COUNT / Vocabulary.GENERATED_ROW_SIZE;

return columns;
}
Expand All @@ -434,8 +434,8 @@ export default class Question extends React.Component {
if (
JsonLdUtils.hasValue(
question,
Constants.LAYOUT_CLASS,
Constants.LAYOUT.EMPHASISE_ON_RELEVANT
Vocabulary.LAYOUT_CLASS,
Vocabulary.LAYOUT.EMPHASISE_ON_RELEVANT
)
) {
return "emphasise-on-relevant";
Expand Down Expand Up @@ -464,10 +464,10 @@ export default class Question extends React.Component {

_renderPrefixes() {
const question = this.props.question;
return question[Constants.HAS_DECLARED_PREFIX] &&
question[Constants.HAS_DECLARED_PREFIX].length ? (
return question[Vocabulary.HAS_DECLARED_PREFIX] &&
question[Vocabulary.HAS_DECLARED_PREFIX].length ? (
<PrefixIcon
prefixes={question[Constants.HAS_DECLARED_PREFIX]}
prefixes={question[Vocabulary.HAS_DECLARED_PREFIX]}
iconClass={"help-icon-checkbox"}
>
<InfoCircle />
Expand All @@ -477,8 +477,8 @@ export default class Question extends React.Component {

_renderUnits() {
const question = this.props.question;
return question[Constants.HAS_UNIT] ? (
<div className="has-unit-label">{question[Constants.HAS_UNIT]}</div>
return question[Vocabulary.HAS_UNIT] ? (
<div className="has-unit-label">{question[Vocabulary.HAS_UNIT]}</div>
) : null;
}

Expand Down Expand Up @@ -512,28 +512,28 @@ export default class Question extends React.Component {

_getSubQuestions() {
const question = this.props.question;
if (!question[Constants.HAS_SUBQUESTION]) {
question[Constants.HAS_SUBQUESTION] = [];
if (!question[Vocabulary.HAS_SUBQUESTION]) {
question[Vocabulary.HAS_SUBQUESTION] = [];
}
if (!Array.isArray(question[Constants.HAS_SUBQUESTION])) {
question[Constants.HAS_SUBQUESTION] = [
question[Constants.HAS_SUBQUESTION],
if (!Array.isArray(question[Vocabulary.HAS_SUBQUESTION])) {
question[Vocabulary.HAS_SUBQUESTION] = [
question[Vocabulary.HAS_SUBQUESTION],
];
}

// sort by label
JsonLdObjectUtils.orderByLocalizedLabels(
question[Constants.HAS_SUBQUESTION],
question[Vocabulary.HAS_SUBQUESTION],
this.context.options.intl
);

// sort by property
JsonLdObjectUtils.orderPreservingToplogicalSort(
question[Constants.HAS_SUBQUESTION],
Constants.HAS_PRECEDING_QUESTION
question[Vocabulary.HAS_SUBQUESTION],
Vocabulary.HAS_PRECEDING_QUESTION
);

return question[Constants.HAS_SUBQUESTION];
return question[Vocabulary.HAS_SUBQUESTION];
}

_getFirstAnswerValue() {
Expand Down
Loading

0 comments on commit fb8d67b

Please sign in to comment.