Fixed realm import file (#3051)

This commit is contained in:
Erik Jan de Wit 2022-08-04 17:18:33 +02:00 committed by GitHub
parent 223c5b03da
commit ae5200154b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,9 @@
import { useState } from "react";
import { useHistory } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { Controller, useForm } from "react-hook-form";
import { import {
ActionGroup, ActionGroup,
AlertVariant,
Button, Button,
FormGroup, FormGroup,
PageSection, PageSection,
@ -8,9 +11,6 @@ import {
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
import { Controller, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { useAlerts } from "../../components/alert/Alerts"; import { useAlerts } from "../../components/alert/Alerts";
import { FormAccess } from "../../components/form-access/FormAccess"; import { FormAccess } from "../../components/form-access/FormAccess";
import { JsonFileUpload } from "../../components/json-file-upload/JsonFileUpload"; import { JsonFileUpload } from "../../components/json-file-upload/JsonFileUpload";
@ -20,6 +20,7 @@ import { useAdminClient } from "../../context/auth/AdminClient";
import { useRealms } from "../../context/RealmsContext"; import { useRealms } from "../../context/RealmsContext";
import { useWhoAmI } from "../../context/whoami/WhoAmI"; import { useWhoAmI } from "../../context/whoami/WhoAmI";
import { toDashboard } from "../../dashboard/routes/Dashboard"; import { toDashboard } from "../../dashboard/routes/Dashboard";
import { convertFormValuesToObject, convertToFormValues } from "../../util";
export default function NewRealmForm() { export default function NewRealmForm() {
const { t } = useTranslation("realm"); const { t } = useTranslation("realm");
@ -28,6 +29,7 @@ export default function NewRealmForm() {
const { refresh: refreshRealms } = useRealms(); const { refresh: refreshRealms } = useRealms();
const { adminClient } = useAdminClient(); const { adminClient } = useAdminClient();
const { addAlert, addError } = useAlerts(); const { addAlert, addError } = useAlerts();
const [realm, setRealm] = useState<RealmRepresentation>();
const { const {
register, register,
@ -37,21 +39,23 @@ export default function NewRealmForm() {
formState: { errors }, formState: { errors },
} = useForm<RealmRepresentation>({ mode: "onChange" }); } = useForm<RealmRepresentation>({ mode: "onChange" });
const handleFileChange = (obj: object) => { const handleFileChange = (obj?: object) => {
const defaultRealm = { id: "", realm: "", enabled: true }; const defaultRealm = { id: "", realm: "", enabled: true };
Object.entries(obj || defaultRealm).map((entry) => convertToFormValues(obj || defaultRealm, setValue);
setValue(entry[0], entry[1]) setRealm(obj || defaultRealm);
);
}; };
const save = async (realm: RealmRepresentation) => { const save = async (fields: RealmRepresentation) => {
try { try {
await adminClient.realms.create(realm); await adminClient.realms.create({
addAlert(t("saveRealmSuccess"), AlertVariant.success); ...realm,
...convertFormValuesToObject(fields),
});
addAlert(t("saveRealmSuccess"));
refresh(); refresh();
await refreshRealms(); await refreshRealms();
history.push(toDashboard({ realm: realm.realm })); history.push(toDashboard({ realm: fields.realm }));
} catch (error) { } catch (error) {
addError("realm:saveRealmError", error); addError("realm:saveRealmError", error);
} }