59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
/**
|
|
* Builds the pipe-delimited value used by question-chain button options.
|
|
* Format: questionNumber|type|answer value|answer text
|
|
*/
|
|
export function buildQuestionChainAnswerValue({
|
|
questionSequence,
|
|
nextQuestionSequence,
|
|
answerResult,
|
|
answerText,
|
|
}) {
|
|
if (nextQuestionSequence) {
|
|
return `${questionSequence}|nextQuestion|${nextQuestionSequence}|${answerText}`;
|
|
}
|
|
|
|
return `${questionSequence}|answer|${answerResult}|${answerText}`;
|
|
}
|
|
|
|
export function mapQuestionAnswersForChain(question) {
|
|
return question.answers.map((answer) => ({
|
|
buttonLabel: answer.answerText,
|
|
value: buildQuestionChainAnswerValue({
|
|
questionSequence: question.questionSequence,
|
|
nextQuestionSequence: answer.nextQuestionSequence,
|
|
answerResult: answer.answerResult,
|
|
answerText: answer.answerText,
|
|
}),
|
|
nextQuestionSequence: answer.nextQuestionSequence,
|
|
answerResult: answer.answerResult,
|
|
problemQuestionId: answer.problemQuestionId,
|
|
questionSequence: question.questionSequence,
|
|
questionType: answer.nextQuestionSequence ? "nextQuestion" : "answer",
|
|
}));
|
|
}
|
|
|
|
export function isTerminalQuestionChainAnswer(answerSelected) {
|
|
if (!answerSelected) {
|
|
return false;
|
|
}
|
|
|
|
const [, questionType, answerResult] = answerSelected.split("|");
|
|
|
|
return questionType?.toLowerCase() === "answer" && !!answerResult;
|
|
}
|
|
|
|
export function normalizeAnswerSelectedValue(answerSelected, answers) {
|
|
if (!answerSelected) {
|
|
return "";
|
|
}
|
|
|
|
if (answers.some((answer) => answer.value === answerSelected)) {
|
|
return answerSelected;
|
|
}
|
|
|
|
const matchedAnswer = answers.find(
|
|
(answer) => answer.value.toUpperCase() === String(answerSelected).toUpperCase()
|
|
);
|
|
|
|
return matchedAnswer?.value ?? answerSelected;
|
|
}
|