enable dot in attribute when user profile enabled

Closes #24918

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
Signed-off-by: Sebastian Schuster <sebastian.schuster@bosch.io>
Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
This commit is contained in:
Sebastian Schuster
2024-01-09 15:50:48 +01:00
committed by GitHub
parent 3947958ab6
commit 4c82f231d8
3 changed files with 24 additions and 15 deletions
+15 -4
View File
@@ -4,6 +4,7 @@ import {
arrayToKeyValue,
keyValueToArray,
} from "../components/key-value-form/key-value-convert";
import { beerify, debeerify } from "../util";
export type UserFormFields = Omit<
UserRepresentation,
@@ -16,9 +17,14 @@ export function toUserFormFields(
data: UserRepresentation,
userProfileEnabled: boolean,
): UserFormFields {
const attributes = userProfileEnabled
? data.attributes
: arrayToKeyValue(data.attributes);
let attributes: Record<string, string | string[]> = {};
if (userProfileEnabled) {
Object.entries(data.attributes || {}).forEach(
([k, v]) => (attributes[beerify(k)] = v),
);
} else {
attributes = arrayToKeyValue(data.attributes);
}
return { ...data, attributes };
}
@@ -27,7 +33,12 @@ export function toUserRepresentation(data: UserFormFields): UserRepresentation {
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,
]),
);
return { ...data, username, attributes };
}
+5 -8
View File
@@ -63,13 +63,10 @@ export const exportClient = (client: ClientRepresentation): void => {
export const toUpperCase = <T extends string>(name: T) =>
(name.charAt(0).toUpperCase() + name.slice(1)) as Capitalize<T>;
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<T>(
export const beerify = <T extends string>(name: T) =>
name.replaceAll(".", "🍺") as ReplaceString<T, ".", "🍺">;
const debeerify = <T extends string>(name: T) =>
export const debeerify = <T extends string>(name: T) =>
name.replaceAll("🍺", ".") as ReplaceString<T, "🍺", ".">;
export function convertToFormValues<T extends FieldValues>(
+4 -3
View File
@@ -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<UserFormFields>;
`${isRootAttribute(name) ? "" : "attributes."}${name?.replaceAll(
".",
"🍺",
)}` as FieldPath<UserFormFields>;
export function setUserProfileServerError<T>(
error: UserProfileError,