Skip to content

Commit

Permalink
fix handleParentConditionalChange
Browse files Browse the repository at this point in the history
  • Loading branch information
JiyaGupta-cs committed Feb 25, 2025
1 parent 34d4669 commit 20beeb7
Showing 1 changed file with 54 additions and 41 deletions.
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 @@ -625,53 +625,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

0 comments on commit 20beeb7

Please sign in to comment.