Files
formbricks/apps/web/components/shared/Survey.tsx
T
Matti Nannt 0a08d7df68 chore: Introduce unified survey UI package @formbricks/surveys (#698)
* add vite survey package

* add renderSurvey method

* add all survey components

* First working version of renderSurvey

* integrate survey package into survey preview

* add survey modal functionality to formbricks-surveys

* fix build errors and add new template types

* add response queue

* add simple formbricks-js integration

* add local state management for surveys

* add local storage to multiple choice and open text questions

* add local state to other question types, layout fixes

* Fix modal close button, clean js package code

* add new calculate progress function

* fix progressbar on thankyou card

* fix churn survey branching in demo product

* use tsup to bundle @formbricks/js

* update survey positioning in link surveys

* fix preview reset button in link survey

* change logic for progress bar

* update progressbar logic

* update spacing

* add conditional autofocus / disable for iframe

* add userId to link survey

* integrated email verification

* moved token verification and reading to server component

* ran pnpm format

* added question prefilling

* ran pnpm format

* Moved question prefilling logic to Link Survey

* Refactor types

* centralize survey package props, fix build errors

* fix userId in link survey

* fix survey closed message

* add redirect on complete, fix bugs

* smaller bugfixes

* smaller bugfixes

* fix bugs

* fix build errors

* remove logs

---------

Co-authored-by: Johannes <johannes@formbricks.com>
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
2023-09-10 14:06:55 +09:00

128 lines
2.9 KiB
TypeScript

import { renderSurveyInline, renderSurveyModal } from "@formbricks/surveys";
import { Survey } from "@formbricks/types/surveys";
import { TResponseData, TResponseUpdate } from "@formbricks/types/v1/responses";
import { TSurvey } from "@formbricks/types/v1/surveys";
import { useEffect, useMemo } from "react";
const createContainerId = () => `formbricks-survey-container`;
interface SurveyProps {
survey: TSurvey | Survey;
brandColor: string;
formbricksSignature: boolean;
activeQuestionId?: string;
onDisplay?: () => void;
onResponse?: (response: TResponseUpdate) => void;
onFinished?: () => void;
onActiveQuestionChange?: (questionId: string) => void;
onClose?: () => void;
autoFocus?: boolean;
prefillResponseData?: TResponseData;
isRedirectDisabled?: boolean;
}
interface SurveyModalProps extends SurveyProps {
placement?: "topRight" | "bottomRight" | "bottomLeft" | "topLeft" | "center";
clickOutside?: boolean;
darkOverlay?: boolean;
highlightBorderColor?: string | null;
}
export const SurveyInline = ({
survey,
brandColor,
formbricksSignature,
activeQuestionId,
onDisplay = () => {},
onResponse = () => {},
onActiveQuestionChange = () => {},
onClose = () => {},
autoFocus,
prefillResponseData,
isRedirectDisabled,
}: SurveyProps) => {
const containerId = useMemo(() => createContainerId(), []);
useEffect(() => {
renderSurveyInline({
survey,
brandColor,
formbricksSignature,
containerId,
onDisplay,
onResponse,
onClose,
activeQuestionId,
onActiveQuestionChange,
autoFocus,
prefillResponseData,
isRedirectDisabled,
});
}, [
activeQuestionId,
brandColor,
containerId,
formbricksSignature,
onActiveQuestionChange,
onClose,
onDisplay,
onResponse,
survey,
autoFocus,
prefillResponseData,
isRedirectDisabled,
]);
return <div id={containerId} className="h-full w-full" />;
};
export const SurveyModal = ({
survey,
brandColor,
formbricksSignature,
activeQuestionId,
placement = "bottomRight",
clickOutside = false,
darkOverlay = false,
highlightBorderColor = null,
onDisplay = () => {},
onResponse = () => {},
onActiveQuestionChange = () => {},
onClose = () => {},
autoFocus,
isRedirectDisabled,
}: SurveyModalProps) => {
useEffect(() => {
renderSurveyModal({
survey,
brandColor,
formbricksSignature,
placement,
clickOutside,
darkOverlay,
highlightBorderColor,
onDisplay,
onResponse,
onClose,
activeQuestionId,
onActiveQuestionChange,
autoFocus,
isRedirectDisabled,
});
}, [
activeQuestionId,
brandColor,
clickOutside,
darkOverlay,
formbricksSignature,
highlightBorderColor,
onActiveQuestionChange,
onClose,
onDisplay,
onResponse,
placement,
survey,
autoFocus,
isRedirectDisabled,
]);
return <div id="formbricks-survey"></div>;
};