Skip to content

Commit

Permalink
Merge pull request #90 from indec-it/feat/transformSection
Browse files Browse the repository at this point in the history
feat: transform section to create subQuestions
  • Loading branch information
maximilianoforlenza authored Jan 8, 2024
2 parents 3fc811c + 94bc24f commit f50d35e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@indec/form-builder",
"version": "2.4.7",
"version": "2.5.0",
"description": "Form builder",
"main": "index.js",
"private": false,
Expand Down
9 changes: 5 additions & 4 deletions src/components/FormBuilder/FormBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function FormBuilder({section, isLastSection, page, onSubmit, onPrevious, compon
addNewSection,
handleOpenModal,
handleShowSurvey,
setOpenModal
setOpenModal,
transformedSection
} = useFormBuilder({isReadOnly, section, initialValues});
return (
<Formik
Expand Down Expand Up @@ -62,18 +63,18 @@ function FormBuilder({section, isLastSection, page, onSubmit, onPrevious, compon
onEdit={() => handleShowSurvey(currentSection.id, false)}
onDelete={() => handleOpenModal(modals.CONFIRM_DELETE_SECTION_MODAL, currentSection.id)}
sectionsLength={values[section.name].length}
section={section}
section={transformedSection}
values={currentSection}
isReadOnly={isReadOnly}
isValid={validateSchema.isValidSync({[section.name]: values?.[section.name]})}
/>
)}
{showSurvey === currentSection.id && (
<Box sx={{backgroundColor: '#fff', boxShadow: 2, p: 2}}>
<Box sx={{boxShadow: 2, p: 2}}>
<QuestionBuilder
values={currentSection}
index={index}
section={section}
section={transformedSection}
disabled={readOnlyMode}
warnings={warnings}
/>
Expand Down
1 change: 0 additions & 1 deletion src/components/FormBuilder/SectionHeader/SectionHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function SectionHeader({section, sectionsLength, onView, onEdit, onDelete, value
<Box
sx={{
display: 'flex',
backgroundColor: '#fff',
boxShadow: 2,
p: 2,
justifyContent: 'space-between',
Expand Down
84 changes: 80 additions & 4 deletions src/components/FormBuilder/useFormBuilder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {useState} from 'react';
import {useState, useMemo} from 'react';

import questionTypes from '@/constants/questionTypes';
import modals from '@/constants/modals';
import validationTypes from '@/constants/validationTypes';
import useSectionInitialValues from '@/hooks/useSectionInitialValues';
import buildQuestions from '@/utils/buildQuestions';
import getSchemas from '@/utils/getSchemas';
Expand All @@ -11,8 +13,6 @@ const useFormBuilder = ({isReadOnly, section, initialValues}) => {
const [showSurvey, setShowSurvey] = useState();
const [selectedSectionId, setSelectedSelectionId] = useState();
const [openModal, setOpenModal] = useState();
const {initialValues: formInitialValues} = useSectionInitialValues(initialValues, section);
const {errorSchema: validateSchema, warningSchema} = getSchemas({section});

const handleShowSurvey = (sectionId, readOnly) => {
setShowSurvey(sectionId);
Expand Down Expand Up @@ -40,6 +40,81 @@ const useFormBuilder = ({isReadOnly, section, initialValues}) => {
setValues(newValues);
};

const transformedSection = useMemo(() => {
const questions = section.questions.map(question => {
if (
[questionTypes.DROPDOWN, questionTypes.RADIO, questionTypes.CHECKBOX].includes(question.type) &&
question.allOptionsNeedSpecification
) {
return {
...question,
subQuestions: question.options.map((option, index) => ({
id: index + 1,
type: question.metadata.specification.type,
optionId: option.id,
label: question.metadata.specification.label,
name: `S${section.id}P${question.id}SQ${index + 1}`,
userVarName: `${question.userVarName}_${option.value}`,
validations: [
{
id: 1,
rules: [
{
id: 1,
conditions: [
{
id: 1,
section: 'S2',
question: question.name,
value: '',
type:
question.type === questionTypes.CHECKBOX ? validationTypes.NOT_INCLUDES : validationTypes.NOT_EQUAL
},
{
id: 2,
section: 'S2',
question: `S${section.id}P${question.id}SQ${index + 1}`,
value: '',
type: validationTypes.EQUAL
}
]
}
],
message: {text: 'Debe completar el campo', type: 'error'}
}
],
navigation: [
{
id: 1,
rules: [
{
id: 1,
conditions: [
{
id: 1,
section: 'S2',
question: question.name,
value: option.value,
type:
question.type === questionTypes.CHECKBOX ? validationTypes.NOT_INCLUDES : validationTypes.NOT_EQUAL
}
]
}
],
action: 'hide'
}
]
}))
};
}
return question;
});
return {...section, questions};
}, [section]);

const {initialValues: formInitialValues} = useSectionInitialValues(initialValues, transformedSection);
const {errorSchema: validateSchema, warningSchema} = getSchemas({section: transformedSection});

return {
readOnlyMode,
showSurvey,
Expand All @@ -52,7 +127,8 @@ const useFormBuilder = ({isReadOnly, section, initialValues}) => {
addNewSection,
handleOpenModal,
handleShowSurvey,
setOpenModal
setOpenModal,
transformedSection
};
};

Expand Down

0 comments on commit f50d35e

Please sign in to comment.