import React from "react"; import { useHistory, useParams } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { FormProvider, useForm } from "react-hook-form"; import { ActionGroup, AlertVariant, Button, FormGroup, PageSection, TextInput, } from "@patternfly/react-core"; import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation"; import type { ProviderRouteParams } from "../routes/NewProvider"; import { HelpItem } from "../../components/help-enabler/HelpItem"; import { FormAccess } from "../../components/form-access/FormAccess"; import { toUserFederation } from "../routes/UserFederation"; import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; import { useRealm } from "../../context/realm-context/RealmContext"; import { useAlerts } from "../../components/alert/Alerts"; import { SettingsCache } from "../shared/SettingsCache"; import { ExtendedHeader } from "../shared/ExtendedHeader"; import "./custom-provider-settings.css"; export default function CustomProviderSettings() { const { t } = useTranslation("user-federation"); const { id, providerId } = useParams(); const history = useHistory(); const form = useForm({ mode: "onChange", }); const { register, errors, reset, handleSubmit, formState: { isDirty }, } = form; const adminClient = useAdminClient(); const { addAlert, addError } = useAlerts(); const { realm } = useRealm(); useFetch( async () => { if (id) { return await adminClient.components.findOne({ id }); } return undefined; }, (fetchedComponent) => { if (fetchedComponent) { reset({ ...fetchedComponent }); } else if (id) { throw new Error(t("common:notFound")); } }, [] ); const save = async (component: ComponentRepresentation) => { const saveComponent = { ...component, providerId, providerType: "org.keycloak.storage.UserStorageProvider", parentId: realm, }; try { if (!id) { await adminClient.components.create(saveComponent); history.push(toUserFederation({ realm })); } else { await adminClient.components.update({ id }, saveComponent); } reset({ ...component }); addAlert(t(!id ? "createSuccess" : "saveSuccess"), AlertVariant.success); } catch (error) { addError(`user-federation:${!id ? "createError" : "saveError"}`, error); } }; return ( handleSubmit(save)()} /> } helperTextInvalid={t("validateName")} validated={errors.name ? "error" : "default"} fieldId="kc-console-display-name" isRequired > ); }