From a1e2fddd5c6d5857b09cb1035e7480218575adfa Mon Sep 17 00:00:00 2001 From: Piyush Gupta <56182734+gupta-piyush19@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:02:02 +0530 Subject: [PATCH] fix: logic jump (#4501) Co-authored-by: Anshuman Pandey <54475686+pandeymangg@users.noreply.github.com> --- .../edit/components/LogicEditorActions.tsx | 2 +- .../edit/components/LogicEditorConditions.tsx | 7 ++++++- .../surveys/[surveyId]/edit/lib/utils.tsx | 8 ++++--- packages/types/surveys/types.ts | 21 ++++++++++++------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/components/LogicEditorActions.tsx b/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/components/LogicEditorActions.tsx index a3ecfd6f3e3..4cd4c888f3d 100644 --- a/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/components/LogicEditorActions.tsx +++ b/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/components/LogicEditorActions.tsx @@ -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; diff --git a/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/components/LogicEditorConditions.tsx b/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/components/LogicEditorConditions.tsx index 6931d82eb46..99cf7429e8e 100644 --- a/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/components/LogicEditorConditions.tsx +++ b/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/components/LogicEditorConditions.tsx @@ -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", diff --git a/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/lib/utils.tsx b/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/lib/utils.tsx index 0ca57e779bf..dccb837cafb 100644 --- a/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/lib/utils.tsx +++ b/apps/web/app/(app)/(survey-editor)/environments/[environmentId]/surveys/[surveyId]/edit/lib/utils.tsx @@ -202,6 +202,7 @@ export const getConditionOperatorOptions = ( export const getMatchValueProps = ( condition: TSingleCondition, localSurvey: TSurvey, + questionIdx: number, t: (key: string) => string ): { show?: boolean; @@ -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 ?? []; @@ -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); @@ -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 { diff --git a/packages/types/surveys/types.ts b/packages/types/surveys/types.ts index f4e499c0c29..77923f126ba 100644 --- a/packages/types/surveys/types.ts +++ b/packages/types/surveys/types.ts @@ -1368,6 +1368,11 @@ const isInvalidOperatorsForQuestionType = ( isInvalidOperator = true; } break; + case TSurveyQuestionTypeEnum.ContactInfo: + if (!["isSubmitted", "isSkipped"].includes(operator)) { + isInvalidOperator = true; + } + break; default: isInvalidOperator = true; } @@ -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") { @@ -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 || @@ -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 { @@ -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); @@ -2640,7 +2647,7 @@ export const ZSurveyFilterCriteria = z.object({ export type TSurveyFilterCriteria = z.infer; -const ZSurveyFilters = z.object({ +export const ZSurveyFilters = z.object({ name: z.string(), createdBy: z.array(z.enum(["you", "others"])), status: z.array(ZSurveyStatus), @@ -2650,14 +2657,14 @@ const ZSurveyFilters = z.object({ export type TSurveyFilters = z.infer; -const ZFilterOption = z.object({ +export const ZFilterOption = z.object({ label: z.string(), value: z.string(), }); export type TFilterOption = z.infer; -const ZSortOption = z.object({ +export const ZSortOption = z.object({ label: z.string(), value: z.enum(["createdAt", "updatedAt", "name", "relevance"]), });