Skip to content

Commit

Permalink
Merge pull request #99 from indec-it/fix/showSubQuestions
Browse files Browse the repository at this point in the history
fix(subQuestions): fix subQuestions
  • Loading branch information
maximilianoforlenza authored Feb 18, 2024
2 parents e722568 + f38702f commit 23fa9e6
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 226 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": "3.1.0",
"version": "3.1.1",
"description": "Form builder",
"main": "index.js",
"private": false,
Expand Down
201 changes: 5 additions & 196 deletions src/components/FormBuilder/FormBuilder.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default {
const sections = [
{
name: 'S2',
label: 'Seccion 1',
label: 'INGRESOS',
questions: [
{
id: 1,
Expand Down Expand Up @@ -219,26 +219,7 @@ const sections = [
],
multiple: true,
favorite: false,
validations: [
{
id: 1,
rules: [
{
id: 1,
conditions: [
{
id: 1,
type: 'eq',
question: 'S2P4',
value: '',
section: 'S2'
}
]
}
],
message: {type: 'error', text: 'Debe completar el campo'}
}
],
validations: [],
navigation: [],
subQuestions: [],
metadata: {},
Expand Down Expand Up @@ -320,13 +301,12 @@ const sections = [
],
id: 2,
userVarName: 'Ingreso',
introduction: 'Seleccione aquellos Items para los cuales tuvo Ingresos',
navigation: []
introduction: 'Seleccione aquellos Items para los cuales tuvo Ingresos'
},
{
id: 1,
name: 'S1',
label: 'Seccion 2',
label: 'EGRESOS',
questions: [
{
id: 1,
Expand Down Expand Up @@ -609,178 +589,7 @@ const sections = [
id: 1
}
],
userVarName: 'Egresos',
navigation: [
{
id: 1,
rules: [
{
id: 1,
conditions: [
{
id: 1,
question: 'S2P1',
value: '1',
type: 'eq',
section: 'S2'
}
]
}
],
action: 'hide'
}
]
},
{
id: 4,
name: 'S4',
label: 'Seccion 3',
questions: [
{
id: 1,
label: 'Tipo de Transaccion',
name: 'S4P1',
number: '1',
type: 3,
options: [
{
id: 1,
name: 'S1P1O1',
subOptions: [
{
id: 1
}
],
label: '2.1 Manufatura sobre insumos fisicos',
value: '1',
needSpecification: false
},
{
id: 2,
needSpecification: false,
label: '2.1.1 Bienes enviados al exterior',
value: '2'
},
{
id: 3,
needSpecification: false,
label: '2.2 Mantenimiento y reparaciones',
value: '3'
}
],
multiple: false,
favorite: false,
validations: [],
navigation: [],
subQuestions: [],
metadata: {},
userVarName: 'Transaccion'
}
],
multiple: false,
favorite: false,
interruption: {
name: 'S1I1',
interruptible: false,
reason: '',
options: [
{
id: 1
}
]
},
headers: [
{
id: 1
}
],
userVarName: 'Egresos',
navigation: [
{
id: 1,
rules: [
{
id: 1,
conditions: [
{
id: 1,
question: 'S2P1',
value: '1',
type: 'eq',
section: 'S2'
}
]
}
],
action: 'hide'
}
]
},
{
id: 3,
name: 'S3',
label: 'Seccion 4',
questions: [
{
id: 1,
label: 'Tipo de Transaccion',
name: 'S3P1',
number: '1',
type: 3,
options: [
{
id: 1,
name: 'S1P1O1',
subOptions: [
{
id: 1
}
],
label: '2.1 Manufatura sobre insumos fisicos',
value: '1',
needSpecification: false
},
{
id: 2,
needSpecification: false,
label: '2.1.1 Bienes enviados al exterior',
value: '2'
},
{
id: 3,
needSpecification: false,
label: '2.2 Mantenimiento y reparaciones',
value: '3'
}
],
multiple: false,
favorite: false,
validations: [],
navigation: [],
subQuestions: [],
metadata: {},
userVarName: 'Transaccion'
}
],
multiple: false,
favorite: false,
interruption: {
name: 'S1I1',
interruptible: false,
reason: '',
options: [
{
id: 1
}
]
},
headers: [
{
id: 1
}
],
userVarName: 'Egresos',
navigation: []
userVarName: 'Egresos'
}
];

Expand Down
1 change: 1 addition & 0 deletions src/components/SubQuestions/SubQuestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function SubQuestions({values, subQuestions, name, ...props}) {
name: parentName,
specificationsPathName: name
});

return selectedQuestions.length > 0 ? (
<Stack spacing={2} sx={{width: '100%'}}>
{selectedQuestions.map(subQuestion => (
Expand Down
1 change: 1 addition & 0 deletions src/hooks/__tests__/useSubQuestions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ describe('useSubQuestions', () => {
{
id: 3,
optionId: 3,
show: true,
type: 1,
label: 'Monto pais',
name: 'S2P4SQ3',
Expand Down
18 changes: 6 additions & 12 deletions src/hooks/useSubQuestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ const useSubQuestions = ({subQuestions, value, name, specificationsPathName}) =>
const {sections, initialValues, section} = useForm();

useEffect(() => {
const allSubQuestions = subQuestions.map(subQuestion => {
const condition = getNavigation({
const allSubQuestions = subQuestions.map(subQuestion => ({
...subQuestion,
show: getNavigation({
navigation: subQuestion.navigation,
answers: value,
section,
initialValues,
sections,
questionName: name,
isSubQuestion: true
});
return {...subQuestion, show: condition.valid};
});
}).valid
}));
const hiddenSubQuestions = allSubQuestions.filter(subQuestion => !subQuestion.show);
const showSubQuestions = allSubQuestions.filter(subQuestion => subQuestion.show);
if (hiddenSubQuestions.length > 0) {
Expand All @@ -32,13 +32,7 @@ const useSubQuestions = ({subQuestions, value, name, specificationsPathName}) =>
...getSubQuestions(hiddenSubQuestions)
});
}
if (showSubQuestions.length > 0) {
showSubQuestions.forEach(subQuestion => {
// eslint-disable-next-line no-param-reassign
delete subQuestion.show;
});
setSelectedSubQuestions(showSubQuestions);
}
setSelectedSubQuestions(showSubQuestions);
}, [value?.[name]?.answer?.value]);

return {selectedQuestions};
Expand Down
47 changes: 47 additions & 0 deletions src/utils/__tests__/buildQuestions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,53 @@ describe('buildQuestions', () => {
});
});

describe('when the section has a Message question`s type', () => {
beforeEach(() => {
section = {
id: 1,
name: 'S1',
label: 'Sección 1',
questions: [
{
id: 1,
label: 'Select a date',
name: 'S1P1',
number: '1',
type: 9,
metadata: {},
options: [],
validations: [],
userVarName: 's1p1'
}
],
interruption: {
name: 'S1I1',
interruptible: false,
reason: '',
options: [
{
id: 1
}
]
},
headers: [],
userVarName: 'S1'
};
});

it('should return an array with `id` without `answer` section.question.name`', () => {
expect(buildQuestions(section)).toEqual({
S1: [
expect.objectContaining({
S1P1: {
id: 1
}
})
]
});
});
});

describe('when the section has a question with subQuestions', () => {
beforeEach(() => {
section = {
Expand Down
34 changes: 19 additions & 15 deletions src/utils/buildQuestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@ const buildQuestions = section => {
}
section.questions.forEach(question => {
const {id} = question;
values[section.name][question.name] = {id, answer: {value: getValue(question)}};
if (question.subQuestions && question.subQuestions.length > 0) {
values[section.name][question.name] = {
...values[section.name][question.name],
answer: {
...values[section.name][question.name].answer,
specifications: getSubQuestions(question.subQuestions)
}
};
}
if (question.multiple) {
values[section.name][question.name] = {
...values[section.name][question.name],
answer: [{id: 1, ...values[section.name][question.name].answer}]
};
if (question.type === questionTypes.MESSAGE) {
values[section.name][question.name] = {id};
} else {
values[section.name][question.name] = {id, answer: {value: getValue(question)}};
if (question.subQuestions && question.subQuestions.length > 0) {
values[section.name][question.name] = {
...values[section.name][question.name],
answer: {
...values[section.name][question.name].answer,
specifications: getSubQuestions(question.subQuestions)
}
};
}
if (question.multiple) {
values[section.name][question.name] = {
...values[section.name][question.name],
answer: [{id: 1, ...values[section.name][question.name].answer}]
};
}
}
});
values[section.name] = [values[section.name]];
Expand Down
Loading

0 comments on commit 23fa9e6

Please sign in to comment.