UNPKG

@keycloakify/keycloak-admin-ui

Version:
72 lines 4.2 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { TextControl, useAlerts, useFetch } from "../../../ui-shared"; import { ActionGroup, AlertVariant, Button, PageSection, } from "@patternfly/react-core"; import { FormProvider, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import { useAdminClient } from "../../../admin-client"; import { DynamicComponents } from "../../../components/dynamic/DynamicComponents"; import { FormAccess } from "../../../components/form/FormAccess"; import { ViewHeader } from "../../../components/view-header/ViewHeader"; import { useServerInfo } from "../../../context/server-info/ServerInfoProvider"; import { KEY_PROVIDER_TYPE } from "../../../util"; import { useParams } from "../../../utils/useParams"; import { toKeysTab } from "../../../realm-settings/routes/KeysTab"; export const KeyProviderForm = ({ providerType, onClose, }) => { var _a, _b, _c; const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { id } = useParams(); const { addAlert, addError } = useAlerts(); const serverInfo = useServerInfo(); const allComponentTypes = (_b = (_a = serverInfo.componentTypes) === null || _a === void 0 ? void 0 : _a[KEY_PROVIDER_TYPE]) !== null && _b !== void 0 ? _b : []; const form = useForm({ mode: "onChange", }); const { handleSubmit, reset } = form; const save = async (component) => { if (component.config) Object.entries(component.config).forEach(([key, value]) => (component.config[key] = Array.isArray(value) ? value : [value])); try { if (id) { await adminClient.components.update({ id }, { ...component, providerType: KEY_PROVIDER_TYPE, }); addAlert(t("saveProviderSuccess"), AlertVariant.success); } else { await adminClient.components.create({ ...component, providerId: providerType, providerType: KEY_PROVIDER_TYPE, }); addAlert(t("saveProviderSuccess"), AlertVariant.success); onClose === null || onClose === void 0 ? void 0 : onClose(); } } catch (error) { addError("saveProviderError", error); } }; useFetch(async () => { if (id) return await adminClient.components.findOne({ id }); }, (result) => { if (result) { reset({ ...result }); } }, []); return (_jsx(FormAccess, { isHorizontal: true, role: "manage-realm", onSubmit: handleSubmit(save), children: _jsxs(FormProvider, { ...form, children: [id && (_jsx(TextControl, { name: "id", label: t("providerId"), labelIcon: t("providerIdHelp"), rules: { required: t("required"), }, readOnly: true })), _jsx(TextControl, { name: "name", defaultValue: providerType, label: t("name"), labelIcon: t("keyProviderMapperNameHelp"), rules: { required: t("required"), } }), _jsx(DynamicComponents, { properties: ((_c = allComponentTypes.find((type) => type.id === providerType)) === null || _c === void 0 ? void 0 : _c.properties) || [] }), _jsxs(ActionGroup, { children: [_jsx(Button, { "data-testid": "add-provider-button", variant: "primary", type: "submit", children: t("save") }), _jsx(Button, { onClick: () => onClose === null || onClose === void 0 ? void 0 : onClose(), variant: "link", children: t("cancel") })] })] }) })); }; export default function KeyProviderFormPage() { const { t } = useTranslation(); const params = useParams(); const navigate = useNavigate(); return (_jsxs(_Fragment, { children: [_jsx(ViewHeader, { titleKey: t("editProvider"), subKey: params.providerType }), _jsx(PageSection, { variant: "light", children: _jsx(KeyProviderForm, { ...params, onClose: () => navigate(toKeysTab({ realm: params.realm, tab: "providers" })) }) })] })); } //# sourceMappingURL=KeyProviderForm.js.map