UNPKG

@keycloakify/keycloak-admin-ui

Version:
55 lines 3.25 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { FormGroup, Spinner, Switch } from "@patternfly/react-core"; import debouncePromise from "p-debounce"; import { useMemo, useState } from "react"; import { useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { HelpItem, TextControl } from "../../ui-shared"; import { useAdminClient } from "../../admin-client"; export const DiscoveryEndpointField = ({ id, fileUpload, children, }) => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { setValue, clearErrors, formState: { errors }, } = useFormContext(); const [discovery, setDiscovery] = useState(true); const [discovering, setDiscovering] = useState(false); const [discoveryResult, setDiscoveryResult] = useState(); const setupForm = (result) => { Object.keys(result).map((k) => setValue(`config.${k}`, result[k])); }; const discover = async (fromUrl) => { setDiscovering(true); try { const result = await adminClient.identityProviders.importFromUrl({ providerId: id, fromUrl, }); setupForm(result); setDiscoveryResult(result); } catch (error) { return error.message; } finally { setDiscovering(false); } }; const discoverDebounced = useMemo(() => debouncePromise(discover, 1000), []); return (_jsxs(_Fragment, { children: [_jsx(FormGroup, { label: t(id === "oidc" ? "useDiscoveryEndpoint" : "useEntityDescriptor"), fieldId: "kc-discovery-endpoint", labelIcon: _jsx(HelpItem, { helpText: t(id === "oidc" ? "useDiscoveryEndpointHelp" : "useEntityDescriptorHelp"), fieldLabelId: "discoveryEndpoint" }), children: _jsx(Switch, { id: "kc-discovery-endpoint-switch", label: t("on"), labelOff: t("off"), isChecked: discovery, onChange: (_event, checked) => { clearErrors("discoveryError"); setDiscovery(checked); }, "aria-label": t(id === "oidc" ? "useDiscoveryEndpoint" : "useEntityDescriptor") }) }), discovery && (_jsx(TextControl, { name: "discoveryEndpoint", label: t(id === "oidc" ? "discoveryEndpoint" : "samlEntityDescriptor"), labelIcon: t(id === "oidc" ? "discoveryEndpointHelp" : "samlEntityDescriptorHelp"), type: "url", placeholder: id === "oidc" ? "https://hostname/realms/master/.well-known/openid-configuration" : "", validated: errors.discoveryError || errors.discoveryEndpoint ? "error" : !discoveryResult ? "default" : "success", customIcon: discovering ? _jsx(Spinner, { isInline: true }) : undefined, rules: { required: t("required"), validate: (value) => discoverDebounced(value), } })), !discovery && fileUpload, discovery && !errors.discoveryError && children(true), !discovery && children(false)] })); }; //# sourceMappingURL=DiscoveryEndpointField.js.map