Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 3135 Fix choice language #3136

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useTranslation } from "next-i18next";
import { cn } from "@lib/utils";
import { FormElement } from "@lib/types";
import { Button } from "@components/globals";
import { useTemplateStore } from "@formbuilder/store";
import { LocalizedFormProperties, LocalizedElementProperties } from "../../../types";

type Choice = {
label: string;
Expand Down Expand Up @@ -121,16 +123,23 @@ export const ConditionalSelector = ({
updateElementId: (index: number, id: string) => void;
removeSelector: (index: number) => void;
}) => {
const { t, i18n } = useTranslation("form-builder");
const { t } = useTranslation("form-builder");

const { localizeField, translationLanguagePriority } = useTemplateStore((s) => ({
localizeField: s.localizeField,
translationLanguagePriority: s.translationLanguagePriority,
}));

const language = translationLanguagePriority;

const questions = useMemo(() => {
const items = elements
.filter((item) => {
return item.id !== itemId;
})
.map((question) => {
const titleKey = i18n.language === "en" ? "titleEn" : "titleFr";
const descKey = i18n.language === "en" ? "descriptionEn" : "descriptionFr";
const titleKey = localizeField(LocalizedFormProperties.TITLE, language);
const descKey = localizeField(LocalizedElementProperties.DESCRIPTION, language);

let label = "";
if (question.properties[titleKey]) {
Expand All @@ -148,7 +157,7 @@ export const ConditionalSelector = ({
// Prepend empty option
items.unshift({ label: "", value: "" });
return items;
}, [elements, itemId, i18n.language]);
}, [elements, itemId, language, localizeField]);

const choiceParentQuestion = choiceId?.split(".")[0] || null;

Expand All @@ -160,10 +169,10 @@ export const ConditionalSelector = ({

const choices = useMemo(() => {
return selectedElement?.properties.choices?.map((choice, index) => {
const result = { label: choice.en, value: `${choiceParentQuestion}.${index}` };
const result = { label: choice[language], value: `${choiceParentQuestion}.${index}` };
return result;
});
}, [selectedElement, choiceParentQuestion]);
}, [selectedElement, choiceParentQuestion, language]);

const handleQuestionChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const value = e.target.value;
Expand Down
Loading