@keycloakify/keycloak-admin-ui
Version:
Repackaged Keycloak Admin UI
49 lines • 2.98 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { ActionGroup, Button, PageSection } from "@patternfly/react-core";
import { useState } from "react";
import { FormProvider, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { FormSubmitButton, TextControl } from "../../ui-shared";
import { useAdminClient } from "../../admin-client";
import { DefaultSwitchControl } from "../../components/SwitchControl";
import { useAlerts } from "../../ui-shared";
import { FormAccess } from "../../components/form/FormAccess";
import { JsonFileUpload } from "../../components/json-file-upload/JsonFileUpload";
import { ViewHeader } from "../../components/view-header/ViewHeader";
import { useWhoAmI } from "../../context/whoami/WhoAmI";
import { toDashboard } from "../../dashboard/routes/Dashboard";
import { convertFormValuesToObject, convertToFormValues } from "../../util";
export default function NewRealmForm() {
const { adminClient } = useAdminClient();
const { t } = useTranslation();
const navigate = useNavigate();
const { refresh, whoAmI } = useWhoAmI();
const { addAlert, addError } = useAlerts();
const [realm, setRealm] = useState();
const form = useForm({
mode: "onChange",
});
const { handleSubmit, setValue, formState } = form;
const handleFileChange = (obj) => {
const defaultRealm = { id: "", realm: "", enabled: true };
convertToFormValues(obj || defaultRealm, setValue);
setRealm(obj || defaultRealm);
};
const save = async (fields) => {
try {
await adminClient.realms.create({
...realm,
...convertFormValuesToObject(fields),
});
addAlert(t("saveRealmSuccess"));
refresh();
navigate(toDashboard({ realm: fields.realm }));
}
catch (error) {
addError("saveRealmError", error);
}
};
return (_jsxs(_Fragment, { children: [_jsx(ViewHeader, { titleKey: "createRealm", subKey: "realmExplain" }), _jsx(PageSection, { variant: "light", children: _jsx(FormProvider, { ...form, children: _jsxs(FormAccess, { isHorizontal: true, onSubmit: handleSubmit(save), role: "view-realm", isReadOnly: !whoAmI.canCreateRealm(), children: [_jsx(JsonFileUpload, { id: "kc-realm-filename", allowEditingUploadedText: true, onChange: handleFileChange }), _jsx(TextControl, { name: "realm", label: t("realmNameField"), rules: { required: t("required") } }), _jsx(DefaultSwitchControl, { name: "enabled", label: t("enabled"), defaultValue: true }), _jsxs(ActionGroup, { children: [_jsx(FormSubmitButton, { formState: formState, allowInvalid: true, allowNonDirty: true, children: t("create") }), _jsx(Button, { variant: "link", onClick: () => navigate(-1), children: t("cancel") })] })] }) }) })] }));
}
//# sourceMappingURL=NewRealmForm.js.map