parent
1902b368c9
commit
33aab79d9d
2 changed files with 16 additions and 31 deletions
|
@ -15,6 +15,10 @@ import { useNavigate } from "react-router-dom";
|
|||
import { adminClient } from "../admin-client";
|
||||
import { useAlerts } from "../components/alert/Alerts";
|
||||
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
||||
import {
|
||||
KeyValueType,
|
||||
keyValueToArray,
|
||||
} from "../components/key-value-form/key-value-convert";
|
||||
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
|
||||
import {
|
||||
RoutableTabs,
|
||||
|
@ -145,13 +149,18 @@ const EditUserForm = ({ user, bruteForced, refresh }: EditUserFormProps) => {
|
|||
useUpdateEffect(() => userForm.reset(user), [user]);
|
||||
|
||||
const save = async (formUser: UserRepresentation) => {
|
||||
const attributes =
|
||||
"key" in (formUser.attributes?.[0] || [])
|
||||
? keyValueToArray(formUser.attributes as KeyValueType[])
|
||||
: user.attributes;
|
||||
try {
|
||||
await adminClient.users.update(
|
||||
{ id: user.id! },
|
||||
{
|
||||
...user,
|
||||
...formUser,
|
||||
username: formUser.username?.trim(),
|
||||
attributes: { ...user.attributes, ...formUser.attributes },
|
||||
attributes,
|
||||
},
|
||||
);
|
||||
addAlert(t("userSaved"), AlertVariant.success);
|
||||
|
@ -258,7 +267,7 @@ const EditUserForm = ({ user, bruteForced, refresh }: EditUserFormProps) => {
|
|||
title={<TabTitleText>{t("common:attributes")}</TabTitleText>}
|
||||
{...attributesTab}
|
||||
>
|
||||
<UserAttributes user={user} />
|
||||
<UserAttributes user={user} save={save} />
|
||||
</Tab>
|
||||
)}
|
||||
<Tab
|
||||
|
|
|
@ -1,33 +1,21 @@
|
|||
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
|
||||
import {
|
||||
AlertVariant,
|
||||
PageSection,
|
||||
PageSectionVariants,
|
||||
} from "@patternfly/react-core";
|
||||
import { useEffect, useState } from "react";
|
||||
import { PageSection, PageSectionVariants } from "@patternfly/react-core";
|
||||
import { useEffect } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { adminClient } from "../admin-client";
|
||||
import { useAlerts } from "../components/alert/Alerts";
|
||||
import {
|
||||
AttributeForm,
|
||||
AttributesForm,
|
||||
} from "../components/key-value-form/AttributeForm";
|
||||
import {
|
||||
arrayToKeyValue,
|
||||
keyValueToArray,
|
||||
} from "../components/key-value-form/key-value-convert";
|
||||
import { arrayToKeyValue } from "../components/key-value-form/key-value-convert";
|
||||
import { useUserProfile } from "../realm-settings/user-profile/UserProfileContext";
|
||||
|
||||
type UserAttributesProps = {
|
||||
user: UserRepresentation;
|
||||
save: (user: UserRepresentation) => void;
|
||||
};
|
||||
|
||||
export const UserAttributes = ({ user: defaultUser }: UserAttributesProps) => {
|
||||
const { t } = useTranslation("users");
|
||||
const { addAlert, addError } = useAlerts();
|
||||
const [user, setUser] = useState<UserRepresentation>(defaultUser);
|
||||
export const UserAttributes = ({ user, save }: UserAttributesProps) => {
|
||||
const form = useForm<AttributeForm>({ mode: "onChange" });
|
||||
const { config } = useUserProfile();
|
||||
|
||||
|
@ -39,18 +27,6 @@ export const UserAttributes = ({ user: defaultUser }: UserAttributesProps) => {
|
|||
form.setValue("attributes", convertAttributes());
|
||||
}, [user, config]);
|
||||
|
||||
const save = async (attributeForm: AttributeForm) => {
|
||||
try {
|
||||
const attributes = keyValueToArray(attributeForm.attributes!);
|
||||
await adminClient.users.update({ id: user.id! }, { ...user, attributes });
|
||||
|
||||
setUser({ ...user, attributes });
|
||||
addAlert(t("userSaved"), AlertVariant.success);
|
||||
} catch (error) {
|
||||
addError("groups:groupUpdateError", error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<PageSection variant={PageSectionVariants.light}>
|
||||
<AttributesForm
|
||||
|
|
Loading…
Reference in a new issue