Skip to content

Commit

Permalink
fix: logic jump (formbricks#4501)
Browse files Browse the repository at this point in the history
Co-authored-by: Anshuman Pandey <54475686+pandeymangg@users.noreply.github.com>
  • Loading branch information
gupta-piyush19 and pandeymangg authored Dec 19, 2024
1 parent 74b770a commit a1e2fdd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export function LogicEditorActions({
placeholder: "Value",
type: localSurvey.variables.find((v) => v.id === action.variableId)?.type || "text",
}}
groupedOptions={getActionValueOptions(action.variableId, localSurvey, t)}
groupedOptions={getActionValueOptions(action.variableId, localSurvey, questionIdx, t)}
onChangeValue={(val, option, fromInput) => {
const fieldType = option?.meta?.type as TActionVariableValueType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ export function LogicEditorConditions({

const conditionValueOptions = getConditionValueOptions(localSurvey, questionIdx, t);
const conditionOperatorOptions = getConditionOperatorOptions(condition, localSurvey);
const { show, options, showInput = false, inputType } = getMatchValueProps(condition, localSurvey, t);
const {
show,
options,
showInput = false,
inputType,
} = getMatchValueProps(condition, localSurvey, questionIdx, t);

const allowMultiSelect = [
"equalsOneOf",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export const getConditionOperatorOptions = (
export const getMatchValueProps = (
condition: TSingleCondition,
localSurvey: TSurvey,
questionIdx: number,
t: (key: string) => string
): {
show?: boolean;
Expand All @@ -223,7 +224,7 @@ export const getMatchValueProps = (
return { show: false, options: [] };
}

let questions = localSurvey.questions ?? [];
let questions = localSurvey.questions.filter((_, idx) => idx <= questionIdx);
let variables = localSurvey.variables ?? [];
let hiddenFields = localSurvey.hiddenFields?.fieldIds ?? [];

Expand Down Expand Up @@ -774,7 +775,7 @@ export const getActionTargetOptions = (
currQuestionIdx: number,
t: (key: string) => string
): TComboboxOption[] => {
let questions = localSurvey.questions.filter((_, idx) => idx !== currQuestionIdx);
let questions = localSurvey.questions.filter((_, idx) => idx > currQuestionIdx);

if (action.objective === "requireAnswer") {
questions = questions.filter((question) => !question.required);
Expand Down Expand Up @@ -863,11 +864,12 @@ export const getActionOperatorOptions = (
export const getActionValueOptions = (
variableId: string,
localSurvey: TSurvey,
questionIdx: number,
t: (key: string) => string
): TComboboxGroupedOption[] => {
const hiddenFields = localSurvey.hiddenFields?.fieldIds ?? [];
let variables = localSurvey.variables ?? [];
const questions = localSurvey.questions;
const questions = localSurvey.questions.filter((_, idx) => idx <= questionIdx);

const hiddenFieldsOptions = hiddenFields.map((field) => {
return {
Expand Down
21 changes: 14 additions & 7 deletions packages/types/surveys/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,11 @@ const isInvalidOperatorsForQuestionType = (
isInvalidOperator = true;
}
break;
case TSurveyQuestionTypeEnum.ContactInfo:
if (!["isSubmitted", "isSkipped"].includes(operator)) {
isInvalidOperator = true;
}
break;
default:
isInvalidOperator = true;
}
Expand Down Expand Up @@ -2005,7 +2010,9 @@ const validateActions = (
logicIndex: number,
actions: TSurveyLogicAction[]
): z.ZodIssue[] => {
const questionIds = survey.questions.map((q) => q.id);
const previousQuestions = survey.questions.filter((_, idx) => idx <= questionIndex);
const nextQuestions = survey.questions.filter((_, idx) => idx >= questionIndex);
const nextQuestionsIds = nextQuestions.map((q) => q.id);

const actionIssues: (z.ZodIssue | undefined)[] = actions.map((action) => {
if (action.objective === "calculate") {
Expand Down Expand Up @@ -2076,7 +2083,7 @@ const validateActions = (
if (action.value.type === "question") {
const allowedQuestions = [TSurveyQuestionTypeEnum.Rating, TSurveyQuestionTypeEnum.NPS];

const selectedQuestion = survey.questions.find((q) => q.id === action.value.value);
const selectedQuestion = previousQuestions.find((q) => q.id === action.value.value);

if (
!selectedQuestion ||
Expand All @@ -2095,7 +2102,7 @@ const validateActions = (
const endingIds = survey.endings.map((ending) => ending.id);

const possibleQuestionIds =
action.objective === "jumpToQuestion" ? [...questionIds, ...endingIds] : questionIds;
action.objective === "jumpToQuestion" ? [...nextQuestionsIds, ...endingIds] : nextQuestionsIds;

if (!possibleQuestionIds.includes(action.target)) {
return {
Expand All @@ -2106,7 +2113,7 @@ const validateActions = (
}

if (action.objective === "requireAnswer") {
const optionalQuestionIds = survey.questions
const optionalQuestionIds = nextQuestions
.filter((question) => !question.required)
.map((question) => question.id);

Expand Down Expand Up @@ -2640,7 +2647,7 @@ export const ZSurveyFilterCriteria = z.object({

export type TSurveyFilterCriteria = z.infer<typeof ZSurveyFilterCriteria>;

const ZSurveyFilters = z.object({
export const ZSurveyFilters = z.object({
name: z.string(),
createdBy: z.array(z.enum(["you", "others"])),
status: z.array(ZSurveyStatus),
Expand All @@ -2650,14 +2657,14 @@ const ZSurveyFilters = z.object({

export type TSurveyFilters = z.infer<typeof ZSurveyFilters>;

const ZFilterOption = z.object({
export const ZFilterOption = z.object({
label: z.string(),
value: z.string(),
});

export type TFilterOption = z.infer<typeof ZFilterOption>;

const ZSortOption = z.object({
export const ZSortOption = z.object({
label: z.string(),
value: z.enum(["createdAt", "updatedAt", "name", "relevance"]),
});
Expand Down

0 comments on commit a1e2fdd

Please sign in to comment.