import React, { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Controller, useForm } from "react-hook-form"; import { ActionGroup, AlertVariant, Button, Checkbox, FormGroup, PageSection, Switch, TextInput, } from "@patternfly/react-core"; import type RealmRepresentation from "keycloak-admin/lib/defs/realmRepresentation"; import { FormAccess } from "../components/form-access/FormAccess"; import { HelpItem } from "../components/help-enabler/HelpItem"; import { FormPanel } from "../components/scroll-form/FormPanel"; import { emailRegexPattern } from "../util"; import { useAdminClient } from "../context/auth/AdminClient"; import { useAlerts } from "../components/alert/Alerts"; import { useRealm } from "../context/realm-context/RealmContext"; import "./RealmSettingsSection.css"; type RealmSettingsEmailTabProps = { realm: RealmRepresentation; }; export const RealmSettingsEmailTab = ({ realm: initialRealm, }: RealmSettingsEmailTabProps) => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const { realm: realmName } = useRealm(); const { addAlert } = useAlerts(); const [isAuthenticationEnabled, setAuthenticationEnabled] = useState("true"); const [realm, setRealm] = useState(initialRealm); const { register, control, handleSubmit, errors, setValue, reset: resetForm, } = useForm(); useEffect(() => { reset(); }, [realm]); const save = async (form: RealmRepresentation) => { try { const savedRealm = { ...realm, ...form }; await adminClient.realms.update({ realm: realmName }, savedRealm); setRealm(savedRealm); addAlert(t("saveSuccess"), AlertVariant.success); } catch (error) { addAlert( t("saveError", { error: error.response?.data?.errorMessage || error }), AlertVariant.danger ); } }; const reset = () => { if (realm) { resetForm(realm); Object.entries(realm).map((entry) => setValue(entry[0], entry[1])); } }; return ( <> } > } > } > ( onChange("" + value)} /> )} /> ( onChange("" + value)} /> )} /> ( { onChange("" + value); setAuthenticationEnabled(String(value)); }} /> )} /> {isAuthenticationEnabled === "true" && ( <> } > )} ); };