Compare commits

...

3 Commits

Author SHA1 Message Date
Johannes
928c586e75 Merge branch 'randomize-last-two' of https://github.com/formbricks/formbricks into randomize-last-two 2025-03-13 14:49:29 +01:00
Johannes
fc066551b5 fix except last 2025-03-13 14:49:26 +01:00
Johannes
7b1c7f95de add setting 2025-03-13 13:45:17 +01:00
5 changed files with 40 additions and 8 deletions

View File

@@ -100,6 +100,11 @@ export const MatrixQuestionForm = ({
label: t("environments.surveys.edit.randomize_all_except_last"),
show: true,
},
exceptLastTwo: {
id: "exceptLastTwo",
label: t("environments.surveys.edit.randomize_all_except_last_two"),
show: true,
},
};
/// Auto animate
const [parent] = useAutoAnimate();

View File

@@ -70,6 +70,11 @@ export const MultipleChoiceQuestionForm = ({
label: t("environments.surveys.edit.randomize_all_except_last"),
show: true,
},
exceptLastTwo: {
id: "exceptLastTwo",
label: t("environments.surveys.edit.randomize_all_except_last_two"),
show: true,
},
};
const updateChoice = (choiceIdx: number, updatedAttributes: { label: TI18nString }) => {

View File

@@ -25,6 +25,7 @@ interface ShuffleOptionsTypes {
none?: ShuffleOptionType;
all?: ShuffleOptionType;
exceptLast?: ShuffleOptionType;
exceptLastTwo?: ShuffleOptionType;
}
interface ShuffleOptionSelectProps {

View File

@@ -34,6 +34,12 @@ export const getShuffledRowIndices = (n: number, shuffleOption: TShuffleOption):
shuffle(array);
array.push(lastElement);
}
} else if (shuffleOption === "exceptLastTwo") {
if (array.length >= 2) {
const lastTwo = array.splice(array.length - 2, 2);
shuffle(array);
array.push(...lastTwo);
}
}
return array;
};
@@ -45,22 +51,37 @@ export const getShuffledChoicesIds = (
const otherOption = choices.find((choice) => {
return choice.id === "other";
});
const shuffledChoices = otherOption ? [...choices.filter((choice) => choice.id !== "other")] : [...choices];
if (shuffleOption === "all") {
shuffle(shuffledChoices);
} else if (shuffleOption === "exceptLast") {
if (otherOption) {
}
if (shuffleOption === "exceptLast") {
const lastElement = shuffledChoices.pop();
if (lastElement) {
shuffle(shuffledChoices);
} else {
const lastElement = shuffledChoices.pop();
if (lastElement) {
shuffledChoices.push(lastElement);
}
} else if (shuffleOption === "exceptLastTwo") {
if (otherOption) {
if (shuffledChoices.length >= 1) {
// Keep the last element fixed (the one before "other")
const lastElement = shuffledChoices.pop();
shuffle(shuffledChoices);
shuffledChoices.push(lastElement);
if (lastElement) shuffledChoices.push(lastElement);
}
} else if (shuffledChoices.length >= 2) {
// No "other" option, keep last two elements fixed
const lastTwo = shuffledChoices.splice(shuffledChoices.length - 2, 2);
shuffle(shuffledChoices);
shuffledChoices.push(...lastTwo);
}
}
if (otherOption) shuffledChoices.push(otherOption);
if (otherOption) {
shuffledChoices.push(otherOption);
}
return shuffledChoices.map((choice) => choice.id);
};

View File

@@ -570,7 +570,7 @@ export const ZSurveyConsentQuestion = ZSurveyQuestionBase.extend({
export type TSurveyConsentQuestion = z.infer<typeof ZSurveyConsentQuestion>;
export const ZShuffleOption = z.enum(["none", "all", "exceptLast"]);
export const ZShuffleOption = z.enum(["none", "all", "exceptLast", "exceptLastTwo"]);
export type TShuffleOption = z.infer<typeof ZShuffleOption>;