UNPKG

@keycloakify/keycloak-admin-ui

Version:
69 lines 3.82 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { ActionGroup, AlertVariant, Button, PageSection, } from "@patternfly/react-core"; import { useMemo } from "react"; import { FormProvider, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { Link, useNavigate } from "react-router-dom"; import { useAdminClient } from "../../admin-client"; import { useAlerts } from "../../ui-shared"; import { DynamicComponents } from "../../components/dynamic/DynamicComponents"; import { FormAccess } from "../../components/form/FormAccess"; import { ViewHeader } from "../../components/view-header/ViewHeader"; import { useRealm } from "../../context/realm-context/RealmContext"; import { useServerInfo } from "../../context/server-info/ServerInfoProvider"; import { toUpperCase } from "../../util"; import { useParams } from "../../utils/useParams"; import { toIdentityProvider } from "../../identity-providers/routes/IdentityProvider"; import { toIdentityProviders } from "../../identity-providers/routes/IdentityProviders"; import { GeneralSettings } from "../../identity-providers/add/GeneralSettings"; export default function AddIdentityProvider() { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { providerId } = useParams(); const form = useForm({ mode: "onChange" }); const serverInfo = useServerInfo(); const providerInfo = useMemo(() => { var _a, _b; const namespaces = [ "org.keycloak.broker.social.SocialIdentityProvider", "org.keycloak.broker.provider.IdentityProvider", ]; for (const namespace of namespaces) { const social = (_b = (_a = serverInfo.componentTypes) === null || _a === void 0 ? void 0 : _a[namespace]) === null || _b === void 0 ? void 0 : _b.find(({ id }) => id === providerId); if (social) { return social; } } }, [serverInfo, providerId]); const { handleSubmit, formState: { isValid }, } = form; const { addAlert, addError } = useAlerts(); const navigate = useNavigate(); const { realm } = useRealm(); const onSubmit = async (provider) => { try { await adminClient.identityProviders.create({ ...provider, providerId, alias: provider.alias, }); addAlert(t("createIdentityProviderSuccess"), AlertVariant.success); navigate(toIdentityProvider({ realm, providerId, alias: provider.alias, tab: "settings", })); } catch (error) { addError("createError", error); } }; const alias = form.getValues("alias"); if (!alias) { form.setValue("alias", providerId); } return (_jsxs(_Fragment, { children: [_jsx(ViewHeader, { titleKey: t("addIdentityProvider", { provider: toUpperCase(providerId), }) }), _jsx(PageSection, { variant: "light", children: _jsxs(FormAccess, { role: "manage-identity-providers", isHorizontal: true, onSubmit: handleSubmit(onSubmit), children: [_jsxs(FormProvider, { ...form, children: [_jsx(GeneralSettings, { id: providerId }), providerInfo && (_jsx(DynamicComponents, { stringify: true, properties: providerInfo.properties }))] }), _jsxs(ActionGroup, { children: [_jsx(Button, { isDisabled: !isValid, variant: "primary", type: "submit", "data-testid": "createProvider", children: t("add") }), _jsx(Button, { variant: "link", "data-testid": "cancel", component: (props) => (_jsx(Link, { ...props, to: toIdentityProviders({ realm }) })), children: t("cancel") })] })] }) })] })); } //# sourceMappingURL=AddIdentityProvider.js.map