import React, { useEffect, useState } from "react"; import { useHistory } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { Controller, FormProvider, useForm } from "react-hook-form"; import { AlertVariant, ButtonVariant, DropdownItem, DropdownSeparator, PageSection, Tab, Tabs, TabTitleText, } from "@patternfly/react-core"; import type RealmRepresentation from "keycloak-admin/lib/defs/realmRepresentation"; import { toUpperCase } from "../util"; import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; import { useAdminClient, useFetch } from "../context/auth/AdminClient"; import { useRealm } from "../context/realm-context/RealmContext"; import { ViewHeader } from "../components/view-header/ViewHeader"; import { useAlerts } from "../components/alert/Alerts"; import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs"; import { RealmSettingsLoginTab } from "./LoginTab"; import { RealmSettingsGeneralTab } from "./GeneralTab"; import { PartialImportDialog } from "./PartialImport"; import { RealmSettingsThemesTab } from "./ThemesTab"; import { RealmSettingsEmailTab } from "./EmailTab"; import { KeysListTab } from "./KeysListTab"; import { KeyMetadataRepresentation } from "keycloak-admin/lib/defs/keyMetadataRepresentation"; import ComponentRepresentation from "keycloak-admin/lib/defs/componentRepresentation"; import { KeysProviderTab } from "./KeysProvidersTab"; type RealmSettingsHeaderProps = { onChange: (value: boolean) => void; value: boolean; save: () => void; realmName: string; }; const RealmSettingsHeader = ({ save, onChange, value, realmName, }: RealmSettingsHeaderProps) => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const { addAlert } = useAlerts(); const history = useHistory(); const { refresh } = useRealm(); const [partialImportOpen, setPartialImportOpen] = useState(false); const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({ titleKey: "realm-settings:disableConfirmTitle", messageKey: "realm-settings:disableConfirm", continueButtonLabel: "common:disable", onConfirm: () => { onChange(!value); save(); }, }); const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ titleKey: "realm-settings:deleteConfirmTitle", messageKey: "realm-settings:deleteConfirm", continueButtonLabel: "common:delete", continueButtonVariant: ButtonVariant.danger, onConfirm: async () => { try { await adminClient.realms.del({ realm: realmName }); addAlert(t("deletedSuccess"), AlertVariant.success); history.push("/master/"); refresh(); } catch (error) { addAlert(t("deleteError", { error }), AlertVariant.danger); } }, }); return ( <> setPartialImportOpen(!partialImportOpen)} /> { setPartialImportOpen(true); }} > {t("partialImport")} , {}}> {t("partialExport")} , , {t("common:delete")} , ]} isEnabled={value} onToggle={(value) => { if (!value) { toggleDisableDialog(); } else { onChange(value); save(); } }} /> ); }; export const RealmSettingsSection = () => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const { realm: realmName } = useRealm(); const { addAlert } = useAlerts(); const form = useForm(); const { control, getValues, setValue } = form; const [realm, setRealm] = useState(); const [activeTab2, setActiveTab2] = useState(0); const [keys, setKeys] = useState([]); const [realmComponents, setRealmComponents] = useState< ComponentRepresentation[] >([]); useFetch( () => adminClient.realms.findOne({ realm: realmName }), (realm) => { setupForm(realm); setRealm(realm); }, [] ); useEffect(() => { const update = async () => { const keysMetaData = await adminClient.realms.getKeys({ realm: realmName, }); setKeys(keysMetaData.keys!); const realmComponents = await adminClient.components.find({ type: "org.keycloak.keys.KeyProvider", realm: realmName, }); setRealmComponents(realmComponents); }; setTimeout(update, 100); }, []); const setupForm = (realm: RealmRepresentation) => { Object.entries(realm).map((entry) => setValue(entry[0], entry[1])); }; const save = async (realm: RealmRepresentation) => { try { await adminClient.realms.update({ realm: realmName }, realm); setRealm(realm); addAlert(t("saveSuccess"), AlertVariant.success); } catch (error) { addAlert(t("saveError", { error }), AlertVariant.danger); } }; return ( <> ( save(getValues())} /> )} /> {t("realm-settings:general")}} data-testid="rs-general-tab" > setupForm(realm!)} /> {t("realm-settings:login")}} data-testid="rs-login-tab" > {t("realm-settings:email")}} data-testid="rs-email-tab" > setupForm(realm!)} /> {t("realm-settings:themes")}} data-testid="rs-themes-tab" > setupForm(realm!)} /> {t("realm-settings:keys")}} data-testid="rs-keys-tab" > setActiveTab2(key as number)} > {t("keysList")}} > {t("providers")}} > {/* */} ); };