import React, { useEffect, useState } from "react"; import { ActionGroup, Button, FormGroup, Select, SelectOption, Switch, TextInput, } from "@patternfly/react-core"; import { useTranslation } from "react-i18next"; import { Controller, UseFormMethods } from "react-hook-form"; import { useHistory, useParams } from "react-router-dom"; import { FormAccess } from "../components/form-access/FormAccess"; import UserRepresentation from "keycloak-admin/lib/defs/userRepresentation"; import { HelpItem } from "../components/help-enabler/HelpItem"; import { useRealm } from "../context/realm-context/RealmContext"; import { asyncStateFetch, useAdminClient } from "../context/auth/AdminClient"; import { useErrorHandler } from "react-error-boundary"; import moment from "moment"; export type UserFormProps = { form: UseFormMethods; save: (user: UserRepresentation) => void; editMode: boolean; timestamp?: number; }; export const UserForm = ({ form: { handleSubmit, register, errors, watch, control, setValue, reset }, save, editMode, }: UserFormProps) => { const { t } = useTranslation("users"); const { realm } = useRealm(); const [ isRequiredUserActionsDropdownOpen, setRequiredUserActionsDropdownOpen, ] = useState(false); const history = useHistory(); const adminClient = useAdminClient(); const { id } = useParams<{ id: string }>(); const handleError = useErrorHandler(); const watchUsernameInput = watch("username"); const [timestamp, setTimestamp] = useState(null); useEffect(() => { if (editMode) { return asyncStateFetch( () => adminClient.users.findOne({ id: id }), (user) => { setupForm(user); }, handleError ); } }, []); const setupForm = (user: UserRepresentation) => { reset(); Object.entries(user).map((entry) => { if (entry[0] == "createdTimestamp") { setTimestamp(entry[1]); } else { setValue(entry[0], entry[1]); } }); }; const emailRegexPattern = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; const requiredUserActionsOptions = [ {t("configureOTP")} , {t("updatePassword")} , {t("updateProfile")} , {t("verifyEmail")} , ]; const clearSelection = () => { setRequiredUserActionsDropdownOpen(false); }; return ( {editMode ? ( <> ) : ( )} } > ( onChange(value)} isChecked={value} label={t("common:on")} labelOff={t("common:off")} /> )} > } > ( onChange(value)} isChecked={value} label={t("common:on")} labelOff={t("common:off")} /> )} > } > ( )} /> ); };