import React, { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Controller, useForm, useWatch } from "react-hook-form"; import { ActionGroup, AlertVariant, Button, FormGroup, PageSection, Switch, } 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 { useAdminClient } from "../context/auth/AdminClient"; import { useAlerts } from "../components/alert/Alerts"; import { useRealm } from "../context/realm-context/RealmContext"; import "./RealmSettingsSection.css"; import type UserRepresentation from "keycloak-admin/lib/defs/userRepresentation"; import { TimeSelector } from "../components/time-selector/TimeSelector"; type RealmSettingsSessionsTabProps = { realm?: RealmRepresentation; user?: UserRepresentation; }; export const RealmSettingsSessionsTab = ({ realm: initialRealm, }: RealmSettingsSessionsTabProps) => { const { t } = useTranslation("realm-settings"); const adminClient = useAdminClient(); const { realm: realmName } = useRealm(); const { addAlert, addError } = useAlerts(); const [realm, setRealm] = useState(initialRealm); const { control, handleSubmit, reset: resetForm, formState, } = useForm(); const offlineSessionMaxEnabled = useWatch({ control, name: "offlineSessionMaxLifespanEnabled", defaultValue: realm?.offlineSessionMaxLifespanEnabled, }); useEffect(() => resetForm(realm), [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) { addError("realm-settings:saveError", error); } }; const reset = () => { if (realm) { resetForm(realm); } }; return ( <> } > ( )} /> } > ( )} /> } > ( )} /> } > ( )} /> } > ( )} /> } > ( )} /> } > ( )} /> } > ( onChange(value.toString())} /> )} /> {offlineSessionMaxEnabled && ( } > ( )} /> )} } > ( )} /> } > ( )} /> ); };