Skip to content

Commit

Permalink
[upd] Remove option to generate wizard from question
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim committed Sep 27, 2020
1 parent d758072 commit d6a9387
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/components/SForms.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const SForms = forwardRef((props, ref) => {
useEffect(() => {
const buildWizard = async () => {
const intl = props.options.intl;
const [wizardProperties, structure] = await WizardGenerator.createWizard(props.form, props.formData, null, intl);
const [wizardProperties, structure] = await WizardGenerator.createWizard(props.form, intl);

if (wizardProperties.steps.length > 0) {
wizardProperties.steps[0].visited = true;
Expand Down Expand Up @@ -49,7 +49,6 @@ const SForms = forwardRef((props, ref) => {

SForms.propTypes = {
form: PropTypes.object.isRequired,
formData: PropTypes.object,
options: PropTypes.object.isRequired,
components: PropTypes.object,
componentsOptions: PropTypes.object,
Expand Down
29 changes: 1 addition & 28 deletions src/model/DefaultFormGenerator.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
import * as Constants from '../constants/Constants';

export default class DefaultFormGenerator {
/**
* Generates default form for the wizard framework.
*
* The form consists of a single step, which contains one text area for the description.
*
* @param rootQuestion Optional, contains root question with data for the default form template
*/
static generateForm(rootQuestion) {
static generateForm() {
let formTemplate = require('./defaultForm');
// Deep copy of the form template to prevent modifications
formTemplate = JSON.parse(JSON.stringify(formTemplate));
if (!rootQuestion) {
return formTemplate;
}
const form = formTemplate['@graph'][0];
const formStep = form[Constants.HAS_SUBQUESTION][0];
const stepQuestion = formStep[Constants.HAS_SUBQUESTION][0];
const questionAnswer = stepQuestion[Constants.HAS_ANSWER];

const step = rootQuestion.subQuestions ? rootQuestion.subQuestions[0] : null;
form['@id'] = rootQuestion.uri;
if (!step) {
return formTemplate;
}
formStep['@id'] = step.uri;
const question = step.subQuestions ? step.subQuestions[0] : null;
if (!question) {
return formTemplate;
}
stepQuestion['@id'] = question.uri;
const answer = question.answers ? question.answers[0] : {};
if (answer) {
questionAnswer['@id'] = answer.uri;
questionAnswer[Constants.HAS_DATA_VALUE] = answer.textValue;
}
return formTemplate;
}
}
25 changes: 12 additions & 13 deletions src/model/WizardGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export default class WizardGenerator {
/**
* Generates a default, one-step wizard.
*
* @param data Optional, data for which the wizard should be generated (i.e. the root question)
* @param title Optional, title of the wizard
* @param intl Preferred language of questions
*
* @return Wizard steps definitions (an array of one element in this case) and form data
*/
static createDefaultWizard(data, title, intl) {
const defaultFormData = DefaultFormGenerator.generateForm(data);
static createDefaultWizard(intl) {
const defaultFormData = DefaultFormGenerator.generateForm();

const [steps, form] = WizardGenerator._constructWizardSteps(defaultFormData, intl);

Expand All @@ -25,12 +25,13 @@ export default class WizardGenerator {

/**
* Generates wizard steps from the specified data-enriched template.
*
* @param structure The wizard structure in JSON-LD
* @param data Optional, data for which the wizard will be generated (i.e. the root question)
* @param title Optional, wizard title
* @param intl Preferred language of questions
*
* @return Promise with generated wizard step definitions and form data
*/
static createWizard(structure, data, title, intl) {
static createWizard(structure, intl) {
return new Promise((resolve) =>
jsonld.flatten(structure, {}, null, (err, structure) => {
let wizardProperties;
Expand All @@ -42,15 +43,13 @@ export default class WizardGenerator {
const [steps, rootForm] = WizardGenerator._constructWizardSteps(structure, intl);
form = rootForm;
wizardProperties = {
steps,
title
steps
};
} catch (e) {
const [steps, rootForm] = WizardGenerator.createDefaultWizard(data, title);
const [steps, rootForm] = WizardGenerator.createDefaultWizard(intl);
form = rootForm;
wizardProperties = {
steps,
title
steps
};
}
return resolve([wizardProperties, form]);
Expand Down Expand Up @@ -79,7 +78,7 @@ export default class WizardGenerator {

if (!formElements) {
Logger.error('Could not find any questions in the received data.');
throw 'Questions in the form';
throw 'No questions in the form';
}

stepQuestions = formElements.filter((item) => {
Expand Down
1 change: 0 additions & 1 deletion types/s-forms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export interface SComponentsOptions {

export interface SFormsProps {
form: object;
formData?: object;
options?: SOptions;
components?: SComponents;
componentsOptions?: SComponentsOptions;
Expand Down

0 comments on commit d6a9387

Please sign in to comment.