diff --git a/packages/surveys/src/components/general/Survey.tsx b/packages/surveys/src/components/general/Survey.tsx index 3f1c4a7472..ae752ace7d 100644 --- a/packages/surveys/src/components/general/Survey.tsx +++ b/packages/surveys/src/components/general/Survey.tsx @@ -4,7 +4,7 @@ import { ResponseErrorComponent } from "@/components/general/ResponseErrorCompon import { AutoCloseWrapper } from "@/components/wrappers/AutoCloseWrapper"; import { evaluateCondition } from "@/lib/logicEvaluator"; import { cn } from "@/lib/utils"; -import { useEffect, useRef, useState } from "preact/hooks"; +import { useEffect, useMemo, useRef, useState } from "preact/hooks"; import { formatDateWithOrdinal, isValidDateString } from "@formbricks/lib/utils/datetime"; import { extractFallbackValue, extractId, extractRecallInfo } from "@formbricks/lib/utils/recall"; @@ -42,7 +42,15 @@ export function Survey({ const [ttc, setTtc] = useState({}); const currentQuestionIndex = survey.questions.findIndex((q) => q.id === questionId); - const currentQuestion = survey.questions[currentQuestionIndex]; + const currentQuestion = useMemo(() => { + if (questionId === "end" && !survey.thankYouCard.enabled) { + const newHistory = [...history]; + const prevQuestionId = newHistory.pop(); + return survey.questions.find((q) => q.id === prevQuestionId); + } else { + return survey.questions.find((q) => q.id === questionId); + } + }, [questionId, survey]); const contentRef = useRef(null); const showProgressBar = !survey.styling?.hideProgressBar; @@ -79,8 +87,8 @@ export function Survey({ } }); - let currIdx = currentQuestionIndex; - let currQues = currentQuestion; + let currIdxTemp = currentQuestionIndex; + let currQuesTemp = currentQuestion; function getNextQuestionId(data: TResponseData, isFromPrefilling: Boolean = false): string { const questions = survey.questions; @@ -90,13 +98,13 @@ export function Survey({ if (!isFromPrefilling) { return questions[0]?.id || "end"; } else { - currIdx = 0; - currQues = questions[0]; + currIdxTemp = 0; + currQuesTemp = questions[0]; } } - if (currIdx === -1) throw new Error("Question not found"); - if (currQues?.logic && currQues?.logic.length > 0) { - for (let logic of currQues.logic) { + if (currIdxTemp === -1) throw new Error("Question not found"); + if (currQuesTemp?.logic && currQuesTemp?.logic.length > 0 && currentQuestion) { + for (let logic of currQuesTemp.logic) { if (!logic.destination) continue; if ( currentQuestion.type === "multipleChoiceSingle" || @@ -115,7 +123,7 @@ export function Survey({ } } } - return questions[currIdx + 1]?.id || "end"; + return questions[currIdxTemp + 1]?.id || "end"; } const onChange = (responseDataUpdate: TResponseData) => { @@ -180,12 +188,13 @@ export function Survey({ setHistory(newHistory); } else { // otherwise go back to previous question in array - prevQuestionId = survey.questions[currIdx - 1]?.id; + prevQuestionId = survey.questions[currIdxTemp - 1]?.id; } if (!prevQuestionId) throw new Error("Question not found"); setQuestionId(prevQuestionId); onActiveQuestionChange(prevQuestionId); }; + function getCardContent() { if (showError) { return ( @@ -225,13 +234,12 @@ export function Survey({ /> ); } else { - const currQues = survey.questions.find((q) => q.id === questionId); return ( - currQues && ( + currentQuestion && ( ) ); diff --git a/packages/surveys/src/components/general/ThankYouCard.tsx b/packages/surveys/src/components/general/ThankYouCard.tsx index d042248729..731f8ba3fa 100644 --- a/packages/surveys/src/components/general/ThankYouCard.tsx +++ b/packages/surveys/src/components/general/ThankYouCard.tsx @@ -39,25 +39,28 @@ export default function ThankYouCard({ return (
- {imageUrl && } - -
- - - -
- - + {imageUrl ? ( + + ) : ( +
+
+ + + +
+ +
+ )}