import React, { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Controller, useForm } from "react-hook-form"; import { useErrorHandler } from "react-error-boundary"; import { ActionGroup, AlertVariant, Button, FormGroup, PageSection, Select, SelectOption, SelectVariant, Switch, } from "@patternfly/react-core"; import RealmRepresentation from "keycloak-admin/lib/defs/realmRepresentation"; import { useAdminClient, asyncStateFetch } from "../context/auth/AdminClient"; import { useRealm } from "../context/realm-context/RealmContext"; import { useAlerts } from "../components/alert/Alerts"; import { FormAccess } from "../components/form-access/FormAccess"; import { HelpItem } from "../components/help-enabler/HelpItem"; import { useServerInfo } from "../context/server-info/ServerInfoProvider"; export const RealmSettingsThemesTab = () => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const handleError = useErrorHandler(); const { realm: realmName } = useRealm(); const { addAlert } = useAlerts(); const { control, setValue, handleSubmit } = useForm(); const [realm, setRealm] = useState(); const [loginThemeOpen, setLoginThemeOpen] = useState(false); const [accountThemeOpen, setAccountThemeOpen] = useState(false); const [adminConsoleThemeOpen, setAdminConsoleThemeOpen] = useState(false); const [emailThemeOpen, setEmailThemeOpen] = useState(false); const [supportedLocalesOpen, setSupportedLocalesOpen] = useState(false); const [defaultLocaleOpen, setDefaultLocaleOpen] = useState(false); const [selections, setSelections] = useState([]); const [ internationalizationEnabled, setInternationalizationEnabled, ] = useState(false); const form = useForm(); const themeTypes = useServerInfo().themes!; const watchSupportedLocales = form.watch( "supportedLocales", themeTypes?.account![0].locales ); useEffect(() => { return asyncStateFetch( () => adminClient.realms.findOne({ realm: realmName }), (realm) => { setRealm(realm); setupForm(realm); setInternationalizationEnabled(realm.internationalizationEnabled!); }, handleError ); }, []); useEffect(() => { setValue("supportedLocales", realm?.supportedLocales); setValue("defaultLocale", realm?.defaultLocale); }, [internationalizationEnabled]); const setupForm = (realm: RealmRepresentation) => { const { ...formValues } = realm; form.reset(formValues); Object.entries(realm).map((entry) => { if (entry[0] === "internationalizationEnabled") { setInternationalizationEnabled(realm!.internationalizationEnabled!); } setValue(entry[0], entry[1]); }); }; const save = async (realm: RealmRepresentation) => { try { await adminClient.realms.update({ realm: realmName }, realm); setRealm({ supportedLocales: selections, ...realm }); addAlert(t("saveSuccess"), AlertVariant.success); } catch (error) { addAlert(t("saveError", { error }), AlertVariant.danger); } }; return ( <> } > ( )} /> } > ( )} /> } > ( )} /> } > ( )} /> ( { onChange(value); if (value) { setValue("internationalizationEnabled", true); setInternationalizationEnabled(value); } setInternationalizationEnabled(value); }} /> )} /> {internationalizationEnabled && ( <> ( )} /> ( )} /> )} ); };