Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle Parent Conditional Change for Survey #6217

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 54 additions & 41 deletions app/assets/javascripts/surveys/modules/Survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,13 @@ const Survey = {
answerGroup[answerId] = val;
}
} else if (value !== '0') { // Multi-Select (Checkbox)
if (typeof answerGroup[answerId] !== 'undefined') {
answerGroup[answerId][answerKey].push('0');
answerGroup[answerId][answerKey].push(value);
} else {
answerText[answerKey] = ['0', value];
answerGroup[answerId] = answerText;
}
if (typeof answerGroup[answerId] !== 'undefined') {
answerGroup[answerId][answerKey].push('0');
answerGroup[answerId][answerKey].push(value);
} else {
answerText[answerKey] = ['0', value];
answerGroup[answerId] = answerText;
}
}
} else {
_postData[name] = value;
Expand Down Expand Up @@ -358,7 +358,7 @@ const Survey = {

// Validate Checkbox
if ($block.find('[data-required-checkbox]').length
&& $block.find('input[type="checkbox"]:checked').length === 0) {
&& $block.find('input[type="checkbox"]:checked').length === 0) {
validation = false;
}

Expand Down Expand Up @@ -627,53 +627,66 @@ const Survey = {
},

handleParentConditionalChange(value, conditionalGroup, $parent) {
let { currentAnswers } = conditionalGroup;
// Get a reference to the current answers (or initialize it if not yet set)
if (!conditionalGroup.currentAnswers) {
conditionalGroup.currentAnswers = [];
}

let conditional;
// let resetQuestions = false;
const answersToRemove = [];

// Find answers that were previously selected but are no longer selected
if (Array.isArray(value)) {
// Check if empty
if (value.length === 0 && currentAnswers) {
conditionalGroup.currentAnswers = [];
}

// Check if conditional was present and is no longer
currentAnswers.forEach((a) => {
if (value.indexOf(a) === -1) {
const index = currentAnswers.indexOf(a);
if (currentAnswers.length === 1) {
currentAnswers = [];
} else {
currentAnswers = currentAnswers.slice(index, index + 1);
}
// For multi-select questions
conditionalGroup.currentAnswers.forEach((prevAnswer) => {
if (value.indexOf(prevAnswer) === -1) {
// This answer was selected before but not anymore
answersToRemove.push(prevAnswer);
}
});
// Check if value matches a conditional question
value.forEach((v) => {
if (conditionalGroup[v] !== undefined
&& currentAnswers.indexOf(v) === -1) {
conditional = conditionalGroup[v];
currentAnswers.push(v);
conditionalGroup.currentAnswers = currentAnswers;

// Add newly selected answers
value.forEach((newAnswer) => {
if (conditionalGroup[newAnswer] !== undefined
&& conditionalGroup.currentAnswers.indexOf(newAnswer) === -1) {
conditionalGroup.currentAnswers.push(newAnswer);
conditional = conditionalGroup[newAnswer];
}
});

if (currentAnswers.length === 0) {
} else {
// For single-select questions
if (conditionalGroup.currentAnswers.length > 0
&& conditionalGroup.currentAnswers[0] !== value) {
// Previous answer is different from current answer
answersToRemove.push(...conditionalGroup.currentAnswers);
conditionalGroup.currentAnswers = [];
}
} else {
conditional = conditionalGroup[value];

if (conditionalGroup[value] !== undefined
&& conditionalGroup.currentAnswers.indexOf(value) === -1) {
conditionalGroup.currentAnswers = [value];
conditional = conditionalGroup[value];
}
}

this.resetConditionalGroupChildren(conditionalGroup);
// Reset only the questions that are no longer relevant
answersToRemove.forEach((answer) => {
if (conditionalGroup[answer]) {
// Find and reset just this conditional question
this.resetConditionalQuestion($(conditionalGroup[answer]));

if (typeof conditional !== 'undefined' && conditional !== null) {
// Remove this answer from currentAnswers array
const index = conditionalGroup.currentAnswers.indexOf(answer);
if (index !== -1) {
conditionalGroup.currentAnswers.splice(index, 1);
}
}
});

// Activate the new conditional question if one exists
if (conditional) {
this.activateConditionalQuestion($(conditional), $parent);
}

// this.indexBlocks();

// $parent.find('.survey__next.hidden').removeClass('hidden');
},

conditionalPresenceListeners(id, question) {
Expand Down
Loading