import React, { useContext, 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 { getBaseUrl } from "../util"; import "./RealmSettingsSection.css"; import type UserRepresentation from "keycloak-admin/lib/defs/userRepresentation"; import { WhoAmIContext } from "../context/whoami/WhoAmI"; import { AddUserEmailModal } from "./AddUserEmailModal"; type RealmSettingsEmailTabProps = { realm: RealmRepresentation; user: UserRepresentation; }; export const RealmSettingsEmailTab = ({ realm: initialRealm, user, }: RealmSettingsEmailTabProps) => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const { realm: realmName } = useRealm(); const { addAlert } = useAlerts(); const { whoAmI } = useContext(WhoAmIContext); const [isAuthenticationEnabled, setAuthenticationEnabled] = useState("true"); const [realm, setRealm] = useState(initialRealm); const [userEmailModalOpen, setUserEmailModalOpen] = useState(false); const [currentUser, setCurrentUser] = useState(); const { register, control, handleSubmit, errors, setValue, reset: resetForm, } = useForm(); const userForm = useForm({ mode: "onChange" }); useEffect(() => { reset(); }, [realm]); useEffect(() => { setCurrentUser(user); }, []); const handleModalToggle = () => { setUserEmailModalOpen(!userEmailModalOpen); }; 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 saveAndTestEmail = async (user: UserRepresentation) => { await adminClient.users.update({ id: whoAmI.getUserId() }, user); const updated = await adminClient.users.findOne({ id: whoAmI.getUserId() }); setCurrentUser(updated); handleModalToggle(); testConnection(); }; const reset = () => { if (realm) { resetForm(realm); Object.entries(realm).map((entry) => setValue(entry[0], entry[1])); } }; const testConnection = async () => { const response = await fetch( `${getBaseUrl(adminClient)}admin/realms/${ realm.realm }/testSMTPConnection`, { method: "POST", headers: { Authorization: `bearer ${await adminClient.getAccessToken()}`, Accept: "application/json", "Content-Type": "application/json", }, body: JSON.stringify(realm.smtpServer! as BodyInit), } ); response.ok ? addAlert(t("testConnectionSuccess"), AlertVariant.success) : addAlert(t("testConnectionError"), AlertVariant.danger); }; return ( <> {userEmailModalOpen && ( { saveAndTestEmail(user!); }} form={userForm} user={currentUser!} /> )} } > } > } > ( onChange("" + value)} /> )} /> ( onChange("" + value)} /> )} /> ( { onChange("" + value); setAuthenticationEnabled(String(value)); }} /> )} /> {isAuthenticationEnabled === "true" && ( <> } > )} ); };