import React, { useState } from "react"; import { ActionGroup, AlertVariant, Button, Chip, ChipGroup, FormGroup, InputGroup, 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 type UserRepresentation from "keycloak-admin/lib/defs/userRepresentation"; import { HelpItem } from "../components/help-enabler/HelpItem"; import { useRealm } from "../context/realm-context/RealmContext"; import { useFetch, useAdminClient } from "../context/auth/AdminClient"; import moment from "moment"; import type GroupRepresentation from "keycloak-admin/lib/defs/groupRepresentation"; import { useAlerts } from "../components/alert/Alerts"; import { emailRegexPattern } from "../util"; import { GroupPickerDialog } from "../components/group/GroupPickerDialog"; export type UserFormProps = { form: UseFormMethods; save: (user: UserRepresentation) => void; editMode: boolean; timestamp?: number; onGroupsUpdate: (groups: GroupRepresentation[]) => void; }; export const UserForm = ({ form: { handleSubmit, register, errors, watch, control, setValue, reset }, save, editMode, onGroupsUpdate, }: 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 watchUsernameInput = watch("username"); const [user, setUser] = useState(); const [selectedGroups, setSelectedGroups] = useState( [] ); const { addAlert } = useAlerts(); const [open, setOpen] = useState(false); useFetch( async () => { if (editMode) return await adminClient.users.findOne({ id: id }); }, (user) => { if (user) { setupForm(user); setUser(user); } }, [selectedGroups] ); const setupForm = (user: UserRepresentation) => { reset(); Object.entries(user).map((entry) => { setValue(entry[0], entry[1]); }); }; const requiredUserActionsOptions = [ {t("configureOTP")} , {t("updatePassword")} , {t("updateProfile")} , {t("verifyEmail")} , ]; const clearSelection = () => { setRequiredUserActionsDropdownOpen(false); }; const deleteItem = (id: string) => { setSelectedGroups(selectedGroups.filter((item) => item.name !== id)); onGroupsUpdate(selectedGroups); }; const addChips = async (groups: GroupRepresentation[]): Promise => { setSelectedGroups([...selectedGroups!, ...groups]); onGroupsUpdate([...selectedGroups!, ...groups]); }; const addGroups = async (groups: GroupRepresentation[]): Promise => { const newGroups = groups; newGroups.forEach(async (group) => { try { await adminClient.users.addToGroup({ id, groupId: group.id!, }); addAlert(t("users:addedGroupMembership"), AlertVariant.success); } catch (error) { addAlert( t("users:addedGroupMembershipError", { error }), AlertVariant.danger ); } }); }; const toggleModal = () => { setOpen(!open); }; return ( {open && ( { editMode ? addGroups(groups) : addChips(groups); setOpen(false); }} onClose={() => setOpen(false)} filterGroups={selectedGroups.map((g) => g.name!)} /> )} {editMode && user ? ( <> ) : ( )} } > ( onChange(value)} isChecked={value} label={t("common:on")} labelOff={t("common:off")} /> )} /> } > ( onChange(value)} isChecked={value} label={t("common:on")} labelOff={t("common:off")} /> )} /> } > ( )} /> {!editMode && ( } > ( <> {selectedGroups.map((currentChip) => ( deleteItem(currentChip.name!)} > {currentChip.path} ))} )} /> )} ); };