Files
formbricks-formbricks/packages/ui/QuestionFormInput/utils.ts
Dhruwang Jariwala 5b78487b94 feat: recall from hidden fields and attributes (#2601)
Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com>
Co-authored-by: Johannes <johannes@formbricks.com>
Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2024-05-27 15:28:43 +00:00

129 lines
3.9 KiB
TypeScript

import { createI18nString } from "@formbricks/lib/i18n/utils";
import { isLabelValidForAllLanguages } from "@formbricks/lib/i18n/utils";
import {
TI18nString,
TSurvey,
TSurveyMatrixQuestion,
TSurveyMultipleChoiceQuestion,
TSurveyQuestion,
} from "@formbricks/types/surveys";
// Function to get index for choice /rowLabel /columnLabel
export const getIndex = (id: string, isChoice: boolean) => {
if (!isChoice) return null;
const parts = id.split("-");
if (parts.length > 1) {
return parseInt(parts[1], 10);
}
return null;
};
export const getChoiceLabel = (
question: TSurveyQuestion,
choiceIdx: number,
surveyLanguageCodes: string[]
): TI18nString => {
const choiceQuestion = question as TSurveyMultipleChoiceQuestion;
return choiceQuestion.choices[choiceIdx]?.label || createI18nString("", surveyLanguageCodes);
};
export const getMatrixLabel = (
question: TSurveyQuestion,
idx: number,
surveyLanguageCodes: string[],
type: "row" | "column"
): TI18nString => {
const matrixQuestion = question as TSurveyMatrixQuestion;
const labels = type === "row" ? matrixQuestion.rows : matrixQuestion.columns;
return labels[idx] || createI18nString("", surveyLanguageCodes);
};
export const getCardText = (
survey: TSurvey,
id: string,
isThankYouCard: boolean,
surveyLanguageCodes: string[]
): TI18nString => {
const card = isThankYouCard ? survey.thankYouCard : survey.welcomeCard;
return (card[id as keyof typeof card] as TI18nString) || createI18nString("", surveyLanguageCodes);
};
export const determineImageUploaderVisibility = (questionIdx: number, localSurvey: TSurvey) => {
switch (questionIdx) {
case localSurvey.questions.length: // Thank You Card
return !!localSurvey.thankYouCard.imageUrl || !!localSurvey.thankYouCard.videoUrl;
case -1: // Welcome Card
return false;
default:
// Regular Survey Question
const question = localSurvey.questions[questionIdx];
return (!!question && !!question.imageUrl) || (!!question && !!question.videoUrl);
}
};
export const getLabelById = (id: string) => {
switch (id) {
case "headline":
return "Question";
case "subheader":
return "Description";
case "placeholder":
return "Placeholder";
case "buttonLabel":
return `"Next" Button Label`;
case "backButtonLabel":
return `"Back" Button Label`;
case "lowerLabel":
return "Lower Label";
case "upperLabel":
return "Upper Label";
default:
return "";
}
};
export const getPlaceHolderById = (id: string) => {
switch (id) {
case "headline":
return "Your question here. Recall information with @";
case "subheader":
return "Your description here. Recall information with @";
default:
return "";
}
};
export const isValueIncomplete = (
id: string,
isInvalid: boolean,
surveyLanguageCodes: string[],
value?: TI18nString
) => {
// Define a list of IDs for which a default value needs to be checked.
const labelIds = [
"label",
"headline",
"subheader",
"lowerLabel",
"upperLabel",
"buttonLabel",
"placeholder",
"backButtonLabel",
"dismissButtonLabel",
];
// If value is not provided, immediately return false as it cannot be incomplete.
if (value === undefined) return false;
// Check if the default value is incomplete. This applies only to specific label IDs.
// For these IDs, the default value should not be an empty string.
const isDefaultIncomplete = labelIds.includes(id) ? value["default"]?.trim() !== "" : false;
// Return true if all the following conditions are met:
// 1. The field is marked as invalid.
// 2. The label is not valid for all provided language codes in the survey.
// 4. For specific label IDs, the default value is incomplete as defined above.
return isInvalid && !isLabelValidForAllLanguages(value, surveyLanguageCodes) && isDefaultIncomplete;
};