From ae637cf1ee66d0404ba6798bd1c6154896f3c57c Mon Sep 17 00:00:00 2001 From: Agnieszka Gancarczyk Date: Wed, 3 Apr 2024 13:16:40 +0100 Subject: [PATCH 01/10] fixed displaying default translations Signed-off-by: Agnieszka Gancarczyk --- .../attribute/AddTranslationsDialog.tsx | 65 +++++++++++++++++-- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx index 73dbc2b7014..269e42ea8eb 100644 --- a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx +++ b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx @@ -58,6 +58,9 @@ export const AddTranslationsDialog = ({ const [max, setMax] = useState(10); const [first, setFirst] = useState(0); const [filter, setFilter] = useState(""); + const [defaultTranslations, setDefaultTranslations] = useState<{ + [key: string]: string; + }>({}); const form = useForm<{ key: string; @@ -106,15 +109,65 @@ export const AddTranslationsDialog = ({ ); }, [combinedLocales, filter, whoAmI]); + const formattedTranslationKey = + translationKey.startsWith("${") && translationKey.endsWith("}") + ? translationKey.substring(2, translationKey.length - 1) + : translationKey; + + useEffect(() => { + const fetchLocalizationTexts = async () => { + try { + const selectedLocales = combinedLocales.map((locale) => locale); + + const results = await Promise.all( + selectedLocales.map((selectedLocale) => + adminClient.realms.getRealmLocalizationTexts({ + realm: realmName, + selectedLocale, + }), + ), + ); + + const translations = results.map((result, index) => { + const locale = selectedLocales[index]; + const value = result[formattedTranslationKey]; + return { + key: formattedTranslationKey, + translations: [{ locale, value }], + }; + }); + + const defaultValuesMap = translations.reduce((acc, translation) => { + const locale = translation.translations[0].locale; + const value = translation.translations[0].value; + return { ...acc, [locale]: value }; + }, {}); + + setDefaultTranslations((prevTranslations) => { + if (prevTranslations !== defaultValuesMap) { + return defaultValuesMap; + } + return prevTranslations; + }); + } catch (error) { + console.error("Error fetching localization texts:", error); + } + }; + + fetchLocalizationTexts(); + }, [combinedLocales, formattedTranslationKey, realmName]); + useEffect(() => { combinedLocales.forEach((locale, rowIndex) => { - setValue(`translations.${rowIndex}`, { - locale, - value: "", - }); - setValue("key", translationKey); + setValue(`translations.${rowIndex}.locale`, locale); + setValue( + `translations.${rowIndex}.value`, + defaultTranslations[locale] || "", + ); }); - }, [combinedLocales, translationKey, setValue]); + + setValue("key", formattedTranslationKey); + }, [combinedLocales, defaultTranslations, formattedTranslationKey, setValue]); const handleOk = () => { const formData = getValues(); From 7855e6fe96e2289d6beec1e2ce6cfac1bd98c890 Mon Sep 17 00:00:00 2001 From: Agnieszka Gancarczyk Date: Wed, 3 Apr 2024 14:48:58 +0100 Subject: [PATCH 02/10] fixed the issue translations Signed-off-by: Agnieszka Gancarczyk --- .../attribute/AddTranslationsDialog.tsx | 15 +++++---------- .../attribute/AttributeGeneralSettings.tsx | 7 ++++++- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx index 269e42ea8eb..0a98696b0cd 100644 --- a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx +++ b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx @@ -109,11 +109,6 @@ export const AddTranslationsDialog = ({ ); }, [combinedLocales, filter, whoAmI]); - const formattedTranslationKey = - translationKey.startsWith("${") && translationKey.endsWith("}") - ? translationKey.substring(2, translationKey.length - 1) - : translationKey; - useEffect(() => { const fetchLocalizationTexts = async () => { try { @@ -130,9 +125,9 @@ export const AddTranslationsDialog = ({ const translations = results.map((result, index) => { const locale = selectedLocales[index]; - const value = result[formattedTranslationKey]; + const value = result[translationKey]; return { - key: formattedTranslationKey, + key: translationKey, translations: [{ locale, value }], }; }); @@ -155,7 +150,7 @@ export const AddTranslationsDialog = ({ }; fetchLocalizationTexts(); - }, [combinedLocales, formattedTranslationKey, realmName]); + }, [combinedLocales, translationKey, realmName]); useEffect(() => { combinedLocales.forEach((locale, rowIndex) => { @@ -166,8 +161,8 @@ export const AddTranslationsDialog = ({ ); }); - setValue("key", formattedTranslationKey); - }, [combinedLocales, defaultTranslations, formattedTranslationKey, setValue]); + setValue("key", translationKey); + }, [combinedLocales, defaultTranslations, translationKey, setValue]); const handleOk = () => { const formData = getValues(); diff --git a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx index db1494fc458..95495c22534 100644 --- a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx +++ b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx @@ -171,13 +171,18 @@ export const AttributeGeneralSettings = ({ handleGeneratedDisplayName(generatedDisplayName); }; + const formattedAttributeDisplayName = attributeDisplayName?.substring( + 2, + attributeDisplayName.length - 1, + ); + return ( <> {addTranslationsModalOpen && ( Date: Wed, 3 Apr 2024 16:31:27 +0100 Subject: [PATCH 03/10] improvement Signed-off-by: Agnieszka Gancarczyk --- .../attribute/AddTranslationsDialog.tsx | 74 +++++++++---------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx index 0a98696b0cd..c275d0895da 100644 --- a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx +++ b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx @@ -109,48 +109,46 @@ export const AddTranslationsDialog = ({ ); }, [combinedLocales, filter, whoAmI]); - useEffect(() => { - const fetchLocalizationTexts = async () => { - try { - const selectedLocales = combinedLocales.map((locale) => locale); - - const results = await Promise.all( - selectedLocales.map((selectedLocale) => - adminClient.realms.getRealmLocalizationTexts({ - realm: realmName, - selectedLocale, - }), - ), - ); + useFetch( + async () => { + const selectedLocales = combinedLocales.map((locale) => locale); - const translations = results.map((result, index) => { - const locale = selectedLocales[index]; - const value = result[translationKey]; - return { - key: translationKey, - translations: [{ locale, value }], - }; - }); + const results = await Promise.all( + selectedLocales.map((selectedLocale) => + adminClient.realms.getRealmLocalizationTexts({ + realm: realmName, + selectedLocale, + }), + ), + ); - const defaultValuesMap = translations.reduce((acc, translation) => { - const locale = translation.translations[0].locale; - const value = translation.translations[0].value; - return { ...acc, [locale]: value }; - }, {}); + const translations = results.map((result, index) => { + const locale = selectedLocales[index]; + const value = result[translationKey]; + return { + key: translationKey, + translations: [{ locale, value }], + }; + }); - setDefaultTranslations((prevTranslations) => { - if (prevTranslations !== defaultValuesMap) { - return defaultValuesMap; - } - return prevTranslations; - }); - } catch (error) { - console.error("Error fetching localization texts:", error); - } - }; + const defaultValuesMap = translations.reduce((acc, translation) => { + const locale = translation.translations[0].locale; + const value = translation.translations[0].value; + return { ...acc, [locale]: value }; + }, {}); - fetchLocalizationTexts(); - }, [combinedLocales, translationKey, realmName]); + return defaultValuesMap; + }, + (fetchedData) => { + setDefaultTranslations((prevTranslations) => { + if (prevTranslations !== fetchedData) { + return fetchedData; + } + return prevTranslations; + }); + }, + [combinedLocales, translationKey, realmName], + ); useEffect(() => { combinedLocales.forEach((locale, rowIndex) => { From ed7d6e1e81896cecc9742fa51b355e74ef32e88f Mon Sep 17 00:00:00 2001 From: Agnieszka Gancarczyk Date: Thu, 4 Apr 2024 13:23:44 +0100 Subject: [PATCH 04/10] added logic for scenario 2 --- .../attribute/AddTranslationsDialog.tsx | 15 ++++++++++++--- .../attribute/AttributeGeneralSettings.tsx | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx index c275d0895da..eae83c48c0f 100644 --- a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx +++ b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx @@ -40,6 +40,7 @@ type Translations = { export type AddTranslationsDialogProps = { translationKey: string; + translations: Translations; onCancel: () => void; toggleDialog: () => void; onTranslationsAdded: (translations: Translations) => void; @@ -47,6 +48,7 @@ export type AddTranslationsDialogProps = { export const AddTranslationsDialog = ({ translationKey, + translations, onCancel, toggleDialog, onTranslationsAdded, @@ -155,12 +157,19 @@ export const AddTranslationsDialog = ({ setValue(`translations.${rowIndex}.locale`, locale); setValue( `translations.${rowIndex}.value`, - defaultTranslations[locale] || "", + translations.translations.length > 0 + ? translations.translations[rowIndex].value + : defaultTranslations[locale] || "", ); }); - setValue("key", translationKey); - }, [combinedLocales, defaultTranslations, translationKey, setValue]); + }, [ + combinedLocales, + defaultTranslations, + translationKey, + setValue, + translations, + ]); const handleOk = () => { const formData = getValues(); diff --git a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx index 95495c22534..801da377bf9 100644 --- a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx +++ b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AttributeGeneralSettings.tsx @@ -185,6 +185,7 @@ export const AttributeGeneralSettings = ({ ? formattedAttributeDisplayName : `profile.attributes.${newAttributeName}` } + translations={translationsData} onTranslationsAdded={handleTranslationsAdded} toggleDialog={handleToggleDialog} onCancel={() => { From f7d15da8421c3fb27ff946b35369db14260a8563 Mon Sep 17 00:00:00 2001 From: Agnieszka Gancarczyk Date: Thu, 4 Apr 2024 13:28:27 +0100 Subject: [PATCH 05/10] added logic for scenario 2 Signed-off-by: Agnieszka Gancarczyk --- .../user-profile/attribute/AddTranslationsDialog.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx index eae83c48c0f..f55b4af2edc 100644 --- a/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx +++ b/js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx @@ -173,7 +173,6 @@ export const AddTranslationsDialog = ({ const handleOk = () => { const formData = getValues(); - const updatedTranslations = formData.translations.map((translation) => { if (translation.locale === filter) { return { From 337a384ad935606ea6d2bdf2b8005e71e0bd0b42 Mon Sep 17 00:00:00 2001 From: Agnieszka Gancarczyk Date: Wed, 17 Apr 2024 09:11:39 +0100 Subject: [PATCH 06/10] improvement Signed-off-by: Agnieszka Gancarczyk --- .../realm-settings/NewAttributeSettings.tsx | 135 +++++++++++++++++- 1 file changed, 133 insertions(+), 2 deletions(-) diff --git a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx index b358305570d..c016873048a 100644 --- a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx +++ b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx @@ -10,7 +10,7 @@ import { PageSection, } from "@patternfly/react-core"; import { flatten } from "flat"; -import { useState } from "react"; +import { useEffect, useMemo, useState } from "react"; import { FormProvider, useForm, useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { Link, useNavigate } from "react-router-dom"; @@ -29,6 +29,7 @@ import { AttributeAnnotations } from "./user-profile/attribute/AttributeAnnotati import { AttributeGeneralSettings } from "./user-profile/attribute/AttributeGeneralSettings"; import { AttributePermission } from "./user-profile/attribute/AttributePermission"; import { AttributeValidations } from "./user-profile/attribute/AttributeValidations"; +import { DEFAULT_LOCALE } from "../i18n/i18n"; import "./realm-settings-section.css"; @@ -169,9 +170,137 @@ export default function NewAttributeSettings() { const [generatedDisplayName, setGeneratedDisplayName] = useState(""); const [realm, setRealm] = useState(); + const defaultSupportedLocales = useMemo(() => { + return realm?.supportedLocales!.length + ? realm.supportedLocales + : [DEFAULT_LOCALE]; + }, [realm]); + + const defaultLocales = useMemo(() => { + return realm?.defaultLocale!.length ? [realm.defaultLocale] : []; + }, [realm]); + + const combinedLocales = useMemo(() => { + return Array.from(new Set([...defaultLocales, ...defaultSupportedLocales])); + }, [defaultLocales, defaultSupportedLocales]); + + // const fetchTranslations = async () => { + // const translationResults: any[] = []; + // const translationsToSave: any[] = []; + // await Promise.all( + // combinedLocales.map(async (selectedLocale) => { + // try { + // const translations = + // await adminClient.realms.getRealmLocalizationTexts({ + // realm: realmName, + // selectedLocale, + // }); + + // const translationsFormatted = Object.entries(translations).map( + // ([key, value]) => ({ + // key, + // value, + // }), + // ); + + // translationResults.push(...translationsFormatted); + + // const translationObject: any = { + // key: translationsFormatted[0].key, + // translations: translationsFormatted.map((trans) => ({ + // locale: selectedLocale, + // value: trans.value, + // })), + // }; + + // translationsToSave.push(translationObject); + // console.log(">>> translationsToSave ", translationsToSave); + // } catch (error) { + // console.error( + // `Error fetching translations for ${selectedLocale}:`, + // error, + // ); + // } + // }), + // ); + // return translationsToSave; + // }; + + // fetchTranslations() + // .then((translationsToSave) => { + // console.log(">>>> translationsToSave ", translationsToSave); + // translationsToSave.forEach((translation) => { + // console.log(">>> translation ", translation); + // setTranslationsData({ + // key: translation.key, + // translations: translation.translations, + // }); + // }); + // }) + // .catch((error) => { + // console.error("Error in fetching translations:", error); + // }); + + useFetch( + async () => { + const translationResults: any[] = []; + const translationsToSave: any[] = []; + await Promise.all( + combinedLocales.map(async (selectedLocale) => { + try { + const translations = + await adminClient.realms.getRealmLocalizationTexts({ + realm: realmName, + selectedLocale, + }); + + const translationsFormatted = Object.entries(translations).map( + ([key, value]) => ({ + key, + value, + }), + ); + + translationResults.push(...translationsFormatted); + + const translationObject: any = { + key: translationsFormatted[0].key, + translations: translationsFormatted.map((trans) => ({ + locale: selectedLocale, + value: trans.value, + })), + }; + + translationsToSave.push(translationObject); + console.log(">>> translationsToSave ", translationsToSave); + } catch (error) { + console.error( + `Error fetching translations for ${selectedLocale}:`, + error, + ); + } + }), + ); + return translationsToSave; + }, + (translationsToSave) => { + console.log(">>>> translationsToSave ", translationsToSave); + translationsToSave.forEach((translation) => { + console.log(">>> translation ", translation); + setTranslationsData({ + key: translation.key, + translations: translation.translations, + }); + }); + }, + [], + ); + + console.log("translationsData ", translationsData); + useFetch( () => adminClient.realms.findOne({ realm: realmName }), - (realm) => { + async (realm) => { if (!realm) { throw new Error(t("notFound")); } @@ -226,6 +355,8 @@ export default function NewAttributeSettings() { [], ); + // console.log(translationsData.translations); + const saveTranslations = async () => { try { const nonEmptyTranslations = translationsData.translations From 2bf3c40a0675ba4e33b8001df531cab332e78faa Mon Sep 17 00:00:00 2001 From: Agnieszka Gancarczyk Date: Wed, 17 Apr 2024 11:38:32 +0100 Subject: [PATCH 07/10] added logic for scenario 3 Signed-off-by: Agnieszka Gancarczyk --- .../realm-settings/NewAttributeSettings.tsx | 116 +++--------------- 1 file changed, 20 insertions(+), 96 deletions(-) diff --git a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx index c016873048a..90348d129d3 100644 --- a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx +++ b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx @@ -10,7 +10,7 @@ import { PageSection, } from "@patternfly/react-core"; import { flatten } from "flat"; -import { useEffect, useMemo, useState } from "react"; +import { useMemo, useState } from "react"; import { FormProvider, useForm, useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { Link, useNavigate } from "react-router-dom"; @@ -184,66 +184,19 @@ export default function NewAttributeSettings() { return Array.from(new Set([...defaultLocales, ...defaultSupportedLocales])); }, [defaultLocales, defaultSupportedLocales]); - // const fetchTranslations = async () => { - // const translationResults: any[] = []; - // const translationsToSave: any[] = []; - // await Promise.all( - // combinedLocales.map(async (selectedLocale) => { - // try { - // const translations = - // await adminClient.realms.getRealmLocalizationTexts({ - // realm: realmName, - // selectedLocale, - // }); - - // const translationsFormatted = Object.entries(translations).map( - // ([key, value]) => ({ - // key, - // value, - // }), - // ); - - // translationResults.push(...translationsFormatted); - - // const translationObject: any = { - // key: translationsFormatted[0].key, - // translations: translationsFormatted.map((trans) => ({ - // locale: selectedLocale, - // value: trans.value, - // })), - // }; - - // translationsToSave.push(translationObject); - // console.log(">>> translationsToSave ", translationsToSave); - // } catch (error) { - // console.error( - // `Error fetching translations for ${selectedLocale}:`, - // error, - // ); - // } - // }), - // ); - // return translationsToSave; - // }; - - // fetchTranslations() - // .then((translationsToSave) => { - // console.log(">>>> translationsToSave ", translationsToSave); - // translationsToSave.forEach((translation) => { - // console.log(">>> translation ", translation); - // setTranslationsData({ - // key: translation.key, - // translations: translation.translations, - // }); - // }); - // }) - // .catch((error) => { - // console.error("Error in fetching translations:", error); - // }); + useFetch( + () => adminClient.realms.findOne({ realm: realmName }), + async (realm) => { + if (!realm) { + throw new Error(t("notFound")); + } + setRealm(realm); + }, + [], + ); useFetch( async () => { - const translationResults: any[] = []; const translationsToSave: any[] = []; await Promise.all( combinedLocales.map(async (selectedLocale) => { @@ -254,25 +207,15 @@ export default function NewAttributeSettings() { selectedLocale, }); - const translationsFormatted = Object.entries(translations).map( - ([key, value]) => ({ - key, - value, - }), - ); - - translationResults.push(...translationsFormatted); - const translationObject: any = { - key: translationsFormatted[0].key, - translations: translationsFormatted.map((trans) => ({ + key: Object.keys(translations)[0], + translations: Object.entries(translations).map(([value]) => ({ locale: selectedLocale, - value: trans.value, + value, })), }; translationsToSave.push(translationObject); - console.log(">>> translationsToSave ", translationsToSave); } catch (error) { console.error( `Error fetching translations for ${selectedLocale}:`, @@ -283,30 +226,13 @@ export default function NewAttributeSettings() { ); return translationsToSave; }, - (translationsToSave) => { - console.log(">>>> translationsToSave ", translationsToSave); - translationsToSave.forEach((translation) => { - console.log(">>> translation ", translation); - setTranslationsData({ - key: translation.key, - translations: translation.translations, - }); - }); + (toSaveTranslations) => { + setTranslationsData(() => ({ + key: toSaveTranslations[0].key, + translations: toSaveTranslations.flatMap((trans) => trans.translations), + })); }, - [], - ); - - console.log("translationsData ", translationsData); - - useFetch( - () => adminClient.realms.findOne({ realm: realmName }), - async (realm) => { - if (!realm) { - throw new Error(t("notFound")); - } - setRealm(realm); - }, - [], + [combinedLocales], ); useFetch( @@ -355,8 +281,6 @@ export default function NewAttributeSettings() { [], ); - // console.log(translationsData.translations); - const saveTranslations = async () => { try { const nonEmptyTranslations = translationsData.translations From 89623f9b5d3d12789b7df9296451bb99e9be779d Mon Sep 17 00:00:00 2001 From: Agnieszka Gancarczyk Date: Wed, 17 Apr 2024 16:23:08 +0100 Subject: [PATCH 08/10] refactor Signed-off-by: Agnieszka Gancarczyk --- .../realm-settings/NewAttributeSettings.tsx | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx index 90348d129d3..99287defb05 100644 --- a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx +++ b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx @@ -207,15 +207,37 @@ export default function NewAttributeSettings() { selectedLocale, }); - const translationObject: any = { - key: Object.keys(translations)[0], - translations: Object.entries(translations).map(([value]) => ({ - locale: selectedLocale, + const formData = form.getValues(); + const formattedKey = formData.displayName?.substring( + 2, + formData.displayName.length - 1, + ); + const filteredTranslations: Array<{ + locale: string; + value: string; + }> = []; + const allTranslations = Object.entries(translations).map( + ([key, value]) => ({ + key, value, - })), + }), + ); + + allTranslations.forEach((translation) => { + if (translation.key === formattedKey) { + filteredTranslations.push({ + locale: selectedLocale, + value: translation.value, + }); + } + }); + + const translationToSave: any = { + key: formattedKey, + translations: filteredTranslations, }; - translationsToSave.push(translationObject); + translationsToSave.push(translationToSave); } catch (error) { console.error( `Error fetching translations for ${selectedLocale}:`, @@ -226,15 +248,19 @@ export default function NewAttributeSettings() { ); return translationsToSave; }, - (toSaveTranslations) => { + (translationsToSaveData) => { setTranslationsData(() => ({ - key: toSaveTranslations[0].key, - translations: toSaveTranslations.flatMap((trans) => trans.translations), + key: translationsToSaveData[0].key, + translations: translationsToSaveData.flatMap( + (translationData) => translationData.translations, + ), })); }, [combinedLocales], ); + console.log(">>> translationsData", translationsData); + useFetch( () => adminClient.users.getProfile(), (config) => { From bf2b5c4d07ad497ed5a0a4f45a0d5d1b3e954280 Mon Sep 17 00:00:00 2001 From: Agnieszka Gancarczyk Date: Wed, 17 Apr 2024 16:23:56 +0100 Subject: [PATCH 09/10] refactor Signed-off-by: Agnieszka Gancarczyk --- js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx index 99287defb05..d7d80ee3082 100644 --- a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx +++ b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx @@ -259,8 +259,6 @@ export default function NewAttributeSettings() { [combinedLocales], ); - console.log(">>> translationsData", translationsData); - useFetch( () => adminClient.users.getProfile(), (config) => { From c2d4978d8ee7035e0a90190f96037ec95a3cf1e5 Mon Sep 17 00:00:00 2001 From: Agnieszka Gancarczyk Date: Thu, 18 Apr 2024 12:52:25 +0100 Subject: [PATCH 10/10] refactor Signed-off-by: Agnieszka Gancarczyk --- .../admin-ui/src/realm-settings/NewAttributeSettings.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx index d7d80ee3082..bd72c373358 100644 --- a/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx +++ b/js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx @@ -307,9 +307,8 @@ export default function NewAttributeSettings() { const saveTranslations = async () => { try { - const nonEmptyTranslations = translationsData.translations - .filter((translation) => translation.value.trim() !== "") - .map(async (translation) => { + const nonEmptyTranslations = translationsData.translations.map( + async (translation) => { try { await adminClient.realms.addLocalization( { @@ -322,7 +321,8 @@ export default function NewAttributeSettings() { } catch (error) { console.error(`Error saving translation for ${translation.locale}`); } - }); + }, + ); await Promise.all(nonEmptyTranslations); } catch (error) { console.error(`Error saving translations: ${error}`);