Files
formbricks-formbricks/apps/web/app/lib/templates.ts
Dhruwang Jariwala f70cda6e11 refactor: removes old types (#1288)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2023-10-20 12:03:16 +00:00

26 lines
1.1 KiB
TypeScript

import { TSurveyQuestion } from "@formbricks/types/surveys";
import { TTemplate } from "@formbricks/types/templates";
export const replaceQuestionPresetPlaceholders = (question: TSurveyQuestion, product) => {
if (!question) return;
if (!product) return question;
const newQuestion = JSON.parse(JSON.stringify(question));
if (newQuestion.headline) {
newQuestion.headline = newQuestion.headline.replace("{{productName}}", product.name);
}
if (newQuestion.subheader) {
newQuestion.subheader = newQuestion.subheader?.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 = JSON.parse(JSON.stringify(template.preset));
preset.name = preset.name.replace("{{productName}}", product.name);
preset.questions = preset.questions.map((question) => {
return replaceQuestionPresetPlaceholders(question, product);
});
return { ...template, preset };
};