moved workaround for dots in form names to util (#182)

This commit is contained in:
Erik Jan de Wit 2020-10-20 22:47:23 +02:00 committed by GitHub
parent 6514c5e410
commit 34723a0ebf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 9 deletions

View file

@ -23,6 +23,7 @@ import { RealmContext } from "../../context/realm-context/RealmContext";
import { useAlerts } from "../../components/alert/Alerts";
import { useLoginProviders } from "../../context/server-info/ServerInfoProvider";
import { ViewHeader } from "../../components/view-header/ViewHeader";
import { convertFormValuesToObject, convertToFormValues } from "../../util";
export const ClientScopeForm = () => {
const { t } = useTranslation("client-scopes");
@ -49,10 +50,7 @@ export const ClientScopeForm = () => {
if (response.data) {
Object.entries(response.data).map((entry) => {
if (entry[0] === "attributes") {
Object.keys(entry[1]).map((key) => {
const newKey = key.replace(/\./g, "_");
setValue("attributes." + newKey, entry[1][key]);
});
convertToFormValues(entry[1], "attributes", setValue);
}
setValue(entry[0], entry[1]);
});
@ -63,11 +61,9 @@ export const ClientScopeForm = () => {
const save = async (clientScopes: ClientScopeRepresentation) => {
try {
const keyValues = Object.keys(clientScopes.attributes!).map((key) => {
const newKey = key.replace(/_/g, ".");
return { [newKey]: clientScopes.attributes![key] };
});
clientScopes.attributes = Object.assign({}, ...keyValues);
clientScopes.attributes = convertFormValuesToObject(
clientScopes.attributes!
);
const url = `/admin/realms/${realm}/client-scopes/`;
if (id) {

View file

@ -41,3 +41,22 @@ export const exportClient = (client: ClientRepresentation): void => {
clientCopy.clientId + ".json"
);
};
export const convertToFormValues = (
obj: any,
prefix: string,
setValue: (name: string, value: any) => void
) => {
return Object.keys(obj).map((key) => {
const newKey = key.replace(/\./g, "_");
setValue(prefix + "." + newKey, obj[key]);
});
};
export const convertFormValuesToObject = (obj: any) => {
const keyValues = Object.keys(obj).map((key) => {
const newKey = key.replace(/_/g, ".");
return { [newKey]: obj[key] };
});
return Object.assign({}, ...keyValues);
};