f550ac9035
* Bump prettier from 3.0.3 to 3.1.0 in /js Bumps [prettier](https://github.com/prettier/prettier) from 3.0.3 to 3.1.0. - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/3.0.3...3.1.0) --- updated-dependencies: - dependency-name: prettier dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Fix linting issues Signed-off-by: Jon Koops <jonkoops@gmail.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Jon Koops <jonkoops@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jon Koops <jonkoops@gmail.com>
165 lines
4.6 KiB
TypeScript
165 lines
4.6 KiB
TypeScript
import { FormGroup, Switch } from "@patternfly/react-core";
|
|
import { ReactNode, useEffect, useState } from "react";
|
|
import { useFormContext } from "react-hook-form";
|
|
import { useTranslation } from "react-i18next";
|
|
import { HelpItem } from "ui-shared";
|
|
|
|
import { adminClient } from "../../admin-client";
|
|
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
|
|
import environment from "../../environment";
|
|
|
|
type DiscoveryEndpointFieldProps = {
|
|
id: string;
|
|
fileUpload: ReactNode;
|
|
children: (readOnly: boolean) => ReactNode;
|
|
};
|
|
|
|
export const DiscoveryEndpointField = ({
|
|
id,
|
|
fileUpload,
|
|
children,
|
|
}: DiscoveryEndpointFieldProps) => {
|
|
const { t } = useTranslation();
|
|
const {
|
|
setValue,
|
|
register,
|
|
setError,
|
|
watch,
|
|
clearErrors,
|
|
formState: { errors },
|
|
} = useFormContext();
|
|
const discoveryUrl = watch("discoveryEndpoint");
|
|
|
|
const [discovery, setDiscovery] = useState(true);
|
|
const [discovering, setDiscovering] = useState(false);
|
|
const [discoveryResult, setDiscoveryResult] =
|
|
useState<Record<string, string>>();
|
|
|
|
const setupForm = (result: Record<string, string>) => {
|
|
Object.keys(result).map((k) => setValue(`config.${k}`, result[k]));
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (!discoveryUrl) {
|
|
setDiscovering(false);
|
|
return;
|
|
}
|
|
|
|
(async () => {
|
|
clearErrors("discoveryError");
|
|
try {
|
|
const result = await adminClient.identityProviders.importFromUrl({
|
|
providerId: id,
|
|
fromUrl: discoveryUrl,
|
|
});
|
|
setupForm(result);
|
|
setDiscoveryResult(result);
|
|
} catch (error) {
|
|
setError("discoveryError", {
|
|
type: "manual",
|
|
message: (error as Error).message,
|
|
});
|
|
}
|
|
|
|
setDiscovering(false);
|
|
})();
|
|
}, [discovering]);
|
|
|
|
return (
|
|
<>
|
|
<FormGroup
|
|
label={t(
|
|
id === "oidc" ? "useDiscoveryEndpoint" : "useEntityDescriptor",
|
|
)}
|
|
fieldId="kc-discovery-endpoint"
|
|
labelIcon={
|
|
<HelpItem
|
|
helpText={t(
|
|
id === "oidc"
|
|
? "useDiscoveryEndpointHelp"
|
|
: "useEntityDescriptorHelp",
|
|
)}
|
|
fieldLabelId="discoveryEndpoint"
|
|
/>
|
|
}
|
|
>
|
|
<Switch
|
|
id="kc-discovery-endpoint-switch"
|
|
label={t("on")}
|
|
labelOff={t("off")}
|
|
isChecked={discovery}
|
|
onChange={(checked) => {
|
|
clearErrors("discoveryError");
|
|
setDiscovery(checked);
|
|
}}
|
|
aria-label={t(
|
|
id === "oidc" ? "useDiscoveryEndpoint" : "useEntityDescriptor",
|
|
)}
|
|
/>
|
|
</FormGroup>
|
|
{discovery && (
|
|
<FormGroup
|
|
label={t(
|
|
id === "oidc" ? "discoveryEndpoint" : "samlEntityDescriptor",
|
|
)}
|
|
fieldId="kc-discovery-endpoint"
|
|
labelIcon={
|
|
<HelpItem
|
|
helpText={t(
|
|
id === "oidc"
|
|
? "discoveryEndpointHelp"
|
|
: "samlEntityDescriptorHelp",
|
|
)}
|
|
fieldLabelId="discoveryEndpoint"
|
|
/>
|
|
}
|
|
validated={
|
|
errors.discoveryError || errors.discoveryEndpoint
|
|
? "error"
|
|
: !discoveryResult
|
|
? "default"
|
|
: "success"
|
|
}
|
|
helperTextInvalid={
|
|
errors.discoveryEndpoint
|
|
? t("required")
|
|
: t("noValidMetaDataFound", {
|
|
error: errors.discoveryError?.message,
|
|
})
|
|
}
|
|
isRequired
|
|
>
|
|
<KeycloakTextInput
|
|
type="url"
|
|
data-testid="discoveryEndpoint"
|
|
id="kc-discovery-endpoint"
|
|
placeholder={
|
|
id === "oidc"
|
|
? "https://hostname/auth/realms/master/.well-known/openid-configuration"
|
|
: ""
|
|
}
|
|
validated={
|
|
errors.discoveryError || errors.discoveryEndpoint
|
|
? "error"
|
|
: !discoveryResult
|
|
? "default"
|
|
: "success"
|
|
}
|
|
customIconUrl={
|
|
discovering
|
|
? environment.resourceUrl + "/discovery-load-indicator.svg"
|
|
: ""
|
|
}
|
|
{...register("discoveryEndpoint", {
|
|
required: true,
|
|
onBlur: () => setDiscovering(true),
|
|
})}
|
|
/>
|
|
</FormGroup>
|
|
)}
|
|
{!discovery && fileUpload}
|
|
{discovery && !errors.discoveryError && children(true)}
|
|
{!discovery && children(false)}
|
|
</>
|
|
);
|
|
};
|