feat: Ability to startAt specific question (#2175)

Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com>
This commit is contained in:
Naitik Kapadia
2024-03-12 18:04:40 +05:30
committed by GitHub
parent 99da20f831
commit 6efb6d4e7b
@@ -50,11 +50,28 @@ export default function LinkSurvey({
const isPreview = searchParams?.get("preview") === "true";
const sourceParam = searchParams?.get("source");
const suId = searchParams?.get("suId");
const startAt = searchParams?.get("startAt");
const isStartAtValid = useMemo(() => {
if (!startAt) return false;
if (survey?.welcomeCard.enabled && startAt === "start") return true;
const isValid = survey?.questions.some((question) => question.id === startAt);
// To remove startAt query param from URL if it is not valid:
if (!isValid && typeof window !== "undefined") {
const url = new URL(window.location.href);
url.searchParams.delete("startAt");
window.history.replaceState({}, "", url.toString());
}
return isValid;
}, [survey, startAt]);
// pass in the responseId if the survey is a single use survey, ensures survey state is updated with the responseId
const [surveyState, setSurveyState] = useState(new SurveyState(survey.id, singleUseId, responseId, userId));
const [activeQuestionId, setActiveQuestionId] = useState<string>(
survey.welcomeCard.enabled ? "start" : survey?.questions[0]?.id
startAt && isStartAtValid ? startAt : survey.welcomeCard.enabled ? "start" : survey?.questions[0]?.id
);
const prefillResponseData: TResponseData | undefined = prefillAnswer