Files
formbricks/apps/web/app/lib/templates.ts
Anshuman Pandey 647a05f469 fix: survey editor validation (#2749)
Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com>
Co-authored-by: Johannes <johannes@formbricks.com>
2024-07-09 14:33:09 +00:00

38 lines
1.4 KiB
TypeScript

import { getLocalizedValue } from "@formbricks/lib/i18n/utils";
import { structuredClone } from "@formbricks/lib/pollyfills/structuredClone";
import { TProduct } from "@formbricks/types/product";
import { TSurveyQuestion } from "@formbricks/types/surveys/types";
import { TTemplate } from "@formbricks/types/templates";
export const replaceQuestionPresetPlaceholders = (
question: TSurveyQuestion,
product: TProduct
): TSurveyQuestion => {
if (!product) return question;
const newQuestion = structuredClone(question);
const defaultLanguageCode = "default";
if (newQuestion.headline) {
newQuestion.headline[defaultLanguageCode] = getLocalizedValue(
newQuestion.headline,
defaultLanguageCode
).replace("{{productName}}", product.name);
}
if (newQuestion.subheader) {
newQuestion.subheader[defaultLanguageCode] = getLocalizedValue(
newQuestion.subheader,
defaultLanguageCode
)?.replace("{{productName}}", product.name);
}
return newQuestion;
};
// replace all occurences of productName with the actual product name in the current template
export const replacePresetPlaceholders = (template: TTemplate, product: any) => {
const preset = structuredClone(template.preset);
preset.name = preset.name.replace("{{productName}}", product.name);
preset.questions = preset.questions.map((question) => {
return replaceQuestionPresetPlaceholders(question, product);
});
return { ...template, preset };
};