mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-30 10:19:51 -06:00
89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
import { useState } from "react";
|
|
import Modal from "./Modal";
|
|
import QuestionConditional from "./QuestionConditional";
|
|
import { TSurveyQuestion, TSurvey } from "@formbricks/types/surveys";
|
|
import ThankYouCard from "./ThankYouCard";
|
|
|
|
interface PreviewSurveyProps {
|
|
localSurvey?: TSurvey;
|
|
setActiveQuestionId: (id: string | null) => void;
|
|
activeQuestionId?: string | null;
|
|
questions: TSurveyQuestion[];
|
|
brandColor: string;
|
|
}
|
|
|
|
export default function PreviewSurvey({
|
|
localSurvey,
|
|
setActiveQuestionId,
|
|
activeQuestionId,
|
|
questions,
|
|
brandColor,
|
|
}: PreviewSurveyProps) {
|
|
const [isModalOpen, setIsModalOpen] = useState(true);
|
|
|
|
const gotoNextQuestion = () => {
|
|
const currentIndex = questions.findIndex((q) => q.id === activeQuestionId);
|
|
if (currentIndex < questions.length - 1) {
|
|
setActiveQuestionId(questions[currentIndex + 1].id);
|
|
} else {
|
|
if (localSurvey?.thankYouCard?.enabled) {
|
|
setActiveQuestionId("thank-you-card");
|
|
} else {
|
|
setIsModalOpen(false);
|
|
setTimeout(() => {
|
|
setActiveQuestionId(questions[0].id);
|
|
setIsModalOpen(true);
|
|
}, 500);
|
|
if (localSurvey?.thankYouCard?.enabled) {
|
|
setActiveQuestionId("thank-you-card");
|
|
} else {
|
|
setIsModalOpen(false);
|
|
setTimeout(() => {
|
|
setActiveQuestionId(questions[0].id);
|
|
setIsModalOpen(true);
|
|
}, 500);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
const resetPreview = () => {
|
|
setIsModalOpen(false);
|
|
setTimeout(() => {
|
|
setActiveQuestionId(questions[0].id);
|
|
setIsModalOpen(true);
|
|
}, 500);
|
|
};
|
|
|
|
if (!activeQuestionId) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Modal isOpen={isModalOpen} reset={resetPreview}>
|
|
{activeQuestionId == "thank-you-card" ? (
|
|
<ThankYouCard
|
|
brandColor={brandColor}
|
|
headline={localSurvey?.thankYouCard?.headline || ""}
|
|
subheader={localSurvey?.thankYouCard?.subheader || ""}
|
|
/>
|
|
) : (
|
|
questions.map(
|
|
(question, idx) =>
|
|
activeQuestionId === question.id && (
|
|
<QuestionConditional
|
|
key={question.id}
|
|
question={question}
|
|
brandColor={brandColor}
|
|
lastQuestion={idx === questions.length - 1}
|
|
onSubmit={gotoNextQuestion}
|
|
/>
|
|
)
|
|
)
|
|
)}
|
|
</Modal>
|
|
</>
|
|
);
|
|
}
|