diff --git a/src/components/CippFormPages/CippAddEditUser.jsx b/src/components/CippFormPages/CippAddEditUser.jsx index 991ade01854a..f772ae0e8e0e 100644 --- a/src/components/CippFormPages/CippAddEditUser.jsx +++ b/src/components/CippFormPages/CippAddEditUser.jsx @@ -8,6 +8,8 @@ import { CippFormLicenseSelector } from "/src/components/CippComponents/CippForm import Grid from "@mui/material/Grid"; import { ApiGetCall } from "../../api/ApiCall"; import { useSettings } from "../../hooks/use-settings"; +import { useWatch } from "react-hook-form"; +import { useEffect } from "react"; const CippAddEditUser = (props) => { const { formControl, userSettingsDefaults, formType = "add" } = props; @@ -16,6 +18,15 @@ const CippAddEditUser = (props) => { url: "/api/ListExtensionsConfig", queryKey: "ListExtensionsConfig", }); + + const watcher = useWatch({ control: formControl.control }); + useEffect(() => { + //if watch.firstname changes, and watch.lastname changes, set displayname to firstname + lastname + if (watcher.givenName && watcher.surname && formType === "add") { + formControl.setValue("displayName", `${watcher.givenName} ${watcher.surname}`); + } + }, [watcher.givenName, watcher.surname]); + return ( diff --git a/src/pages/identity/administration/users/add.jsx b/src/pages/identity/administration/users/add.jsx index 5d15605280e7..6db4b2a4bbf6 100644 --- a/src/pages/identity/administration/users/add.jsx +++ b/src/pages/identity/administration/users/add.jsx @@ -11,7 +11,7 @@ const Page = () => { const userSettingsDefaults = useSettings(); const formControl = useForm({ - mode: "onChange", + mode: "onBlur", defaultValues: { tenantFilter: userSettingsDefaults.currentTenant, usageLocation: userSettingsDefaults.usageLocation, @@ -36,6 +36,7 @@ const Page = () => { newFields.usageLocation = { label: usageLocation, value: usageLocation }; } newFields.tenantFilter = userSettingsDefaults.currentTenant; + formControl.reset(newFields); } }, [formValues]); diff --git a/src/pages/identity/administration/users/user/edit.jsx b/src/pages/identity/administration/users/user/edit.jsx index 539ed69d1d98..76998a10094b 100644 --- a/src/pages/identity/administration/users/user/edit.jsx +++ b/src/pages/identity/administration/users/user/edit.jsx @@ -25,7 +25,7 @@ const Page = () => { }); const formControl = useForm({ - mode: "onChange", + mode: "onBlur", defaultValues: { tenantFilter: userSettingsDefaults.currentTenant, }, @@ -34,8 +34,17 @@ const Page = () => { useEffect(() => { if (userRequest.isSuccess) { const user = userRequest.data?.[0]; + //if we have userSettingsDefaults.userAttributes set, grab the .label from each userSsettingsDefaults, then set defaultAttributes.${label}.value to user.${label} + let defaultAttributes = {}; + if (userSettingsDefaults.userAttributes) { + userSettingsDefaults.userAttributes.forEach((attribute) => { + defaultAttributes[attribute.label] = { Value: user?.[attribute.label] }; + }); + } + console.log(defaultAttributes); formControl.reset({ ...user, + defaultAttributes: defaultAttributes, tenantFilter: userSettingsDefaults.currentTenant, licenses: user.assignedLicenses.map((license) => ({ label: getCippLicenseTranslation([license]),