From e69031d411c981fa78b8320b9588771069b4b86d Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Tue, 5 Dec 2023 14:09:55 +0100 Subject: [PATCH] enable dot in attribute when user profile enabled (#25104) fixes: #24918 Signed-off-by: Erik Jan de Wit --- js/apps/admin-ui/src/user/form-state.ts | 20 ++++++++++++++++---- js/apps/admin-ui/src/util.ts | 13 +++++-------- js/libs/ui-shared/src/user-profile/utils.ts | 7 ++++--- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/js/apps/admin-ui/src/user/form-state.ts b/js/apps/admin-ui/src/user/form-state.ts index 922d48b8b0..089095959e 100644 --- a/js/apps/admin-ui/src/user/form-state.ts +++ b/js/apps/admin-ui/src/user/form-state.ts @@ -4,6 +4,7 @@ import { arrayToKeyValue, keyValueToArray, } from "../components/key-value-form/key-value-convert"; +import { beerify, debeerify } from "../util"; export type UserFormFields = Omit< UIUserRepresentation, @@ -21,9 +22,15 @@ export function toUserFormFields( data: UIUserRepresentation, userProfileEnabled: boolean, ): UserFormFields { - const attributes = userProfileEnabled - ? data.attributes - : arrayToKeyValue(data.attributes); + let attributes: Record = {}; + if (userProfileEnabled) { + Object.entries(data.attributes || {}).forEach( + ([k, v]) => (attributes[beerify(k)] = v), + ); + } else { + attributes = arrayToKeyValue(data.attributes); + } + const unmanagedAttributes = arrayToKeyValue(data.unmanagedAttributes); return { ...data, attributes, unmanagedAttributes }; } @@ -34,7 +41,12 @@ export function toUserRepresentation( const username = data.username?.trim(); const attributes = Array.isArray(data.attributes) ? keyValueToArray(data.attributes) - : data.attributes; + : Object.fromEntries( + Object.entries(data.attributes || {}).map(([k, v]) => [ + debeerify(k), + v, + ]), + ); const unmanagedAttributes = Array.isArray(data.unmanagedAttributes) ? keyValueToArray(data.unmanagedAttributes) : data.unmanagedAttributes; diff --git a/js/apps/admin-ui/src/util.ts b/js/apps/admin-ui/src/util.ts index 9a81d68df0..8defa9741f 100644 --- a/js/apps/admin-ui/src/util.ts +++ b/js/apps/admin-ui/src/util.ts @@ -63,13 +63,10 @@ export const exportClient = (client: ClientRepresentation): void => { export const toUpperCase = (name: T) => (name.charAt(0).toUpperCase() + name.slice(1)) as Capitalize; -const isAttributesObject = (value: any) => { - return ( - Object.values(value).filter( - (value) => Array.isArray(value) && value.length >= 1, - ).length !== 0 - ); -}; +const isAttributesObject = (value: any) => + Object.values(value).filter( + (value) => Array.isArray(value) && value.length >= 1, + ).length !== 0; const isAttributeArray = (value: any) => { if (!Array.isArray(value)) { @@ -95,7 +92,7 @@ export function convertAttributeNameToForm( export const beerify = (name: T) => name.replaceAll(".", "🍺") as ReplaceString; -const debeerify = (name: T) => +export const debeerify = (name: T) => name.replaceAll("🍺", ".") as ReplaceString; export function convertToFormValues( diff --git a/js/libs/ui-shared/src/user-profile/utils.ts b/js/libs/ui-shared/src/user-profile/utils.ts index 02b4d27074..3cd70a408c 100644 --- a/js/libs/ui-shared/src/user-profile/utils.ts +++ b/js/libs/ui-shared/src/user-profile/utils.ts @@ -44,9 +44,10 @@ export const isRootAttribute = (attr?: string) => attr && ROOT_ATTRIBUTES.includes(attr); export const fieldName = (name?: string) => - `${ - isRootAttribute(name) ? "" : "attributes." - }${name}` as FieldPath; + `${isRootAttribute(name) ? "" : "attributes."}${name?.replaceAll( + ".", + "🍺", + )}` as FieldPath; export function setUserProfileServerError( error: UserProfileError,