-
Notifications
You must be signed in to change notification settings - Fork 26
Question Dumper
Maurits edited this page Apr 19, 2021
·
2 revisions
The following code can dump our questions..
src/signals/incident/definitions/question-dumper.js:
const parseQuestion = (question, values = []) => {
question.forEach(metaValue => {
if (typeof metaValue === 'string') {
values.push(metaValue);
} else if (metaValue.props.items) {
parseQuestion(metaValue.props.items, values);
} else {
values.push(metaValue.props.children);
}
});
return values;
};
export const questionsToMarkdown = questions => {
let lines = [];
Object.entries(questions).forEach(([categoryName, options]) => {
lines.push(`# Categorie: ${categoryName}`);
Object.entries(options).forEach(([questionId, question]) => {
if (questionId.startsWith('$field_0') || questionId.startsWith('custom_text')) return;
// Remove duplicate questions
if (lines.some(line => (line).includes(question.meta.label))) return;
if (question.meta.subtitle) {
lines.push(`Q: **${question.meta.label}${question?.options?.validators.includes('required') ? ' (niet verplicht)' : ''}** (${question.meta.subtitle})`);
} else if (question.meta.label) {
lines.push(`Q: **${question.meta.label}${question?.options?.validators.includes('required') ? ' (niet verplicht)' : ''}**`);
if (question.meta.subtitle) lines.push(question.meta.subtitle);
} else if (Array.isArray(question.meta.value)) {
lines = lines.concat(parseQuestion(question.meta.value));
} else {
lines.push(question.meta.value);
}
if (question.meta.values) Object.values(question.meta.values).forEach((answer, aIndex) => { lines.push(`A ${aIndex + 1}: ${answer}`); });
lines.push('');
});
});
return lines;
};
export const questionsToJSON = questions => {
const result = {};
Object.entries(questions).forEach(([categoryName, options]) => {
result[categoryName] = {};
Object.entries(options.controls).forEach(([questionId, question]) => {
if (questionId.startsWith('$field_0') || questionId.startsWith('custom_text')) return;
const category = {
subtitle: question.meta.subtitle,
label: question.meta.label,
shortLabel: question.meta.shortLabel,
values: question.meta.values,
};
if (question.meta.values) {
category.answers = question.meta.values;
} else if (Array.isArray(question.meta.value)) {
category.answers = [];
category.answers = category.answers.concat(parseQuestion(question.meta.value));
} else {
category.value = question.meta.value;
}
result[categoryName][questionId] = category;
});
});
return result;
};
Enable it by pasting the following code after the last import statement
in: src/signals/incident/definitions/wizard-step-2-vulaan.js:
import { questionsToJSON } from './question-dumper';
const allQuestions = {
afval,
civieleConstructies,
overlastBedrijvenEnHoreca,
overlastInDeOpenbareRuimte,
overlastOpHetWater,
overlastVanDieren,
overlastPersonenEnGroepen,
wegenVerkeerStraatmeubilair,
wonen,
};
console.log(JSON.stringify(questionsToJSON(allQuestions), null, 2));
// ************************************************************************************