2023-05-03 13:51:02 +00:00
|
|
|
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
|
2023-08-09 11:26:04 +00:00
|
|
|
import { PageSection, PageSectionVariants } from "@patternfly/react-core";
|
|
|
|
import { useEffect } from "react";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2021-10-25 14:38:54 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
AttributeForm,
|
|
|
|
AttributesForm,
|
2022-04-20 17:11:46 +00:00
|
|
|
} from "../components/key-value-form/AttributeForm";
|
2023-08-09 11:26:04 +00:00
|
|
|
import { arrayToKeyValue } from "../components/key-value-form/key-value-convert";
|
2022-11-17 16:14:47 +00:00
|
|
|
import { useUserProfile } from "../realm-settings/user-profile/UserProfileContext";
|
2021-10-25 14:38:54 +00:00
|
|
|
|
|
|
|
type UserAttributesProps = {
|
|
|
|
user: UserRepresentation;
|
2023-08-09 11:26:04 +00:00
|
|
|
save: (user: UserRepresentation) => void;
|
2021-10-25 14:38:54 +00:00
|
|
|
};
|
|
|
|
|
2023-08-09 11:26:04 +00:00
|
|
|
export const UserAttributes = ({ user, save }: UserAttributesProps) => {
|
2021-10-25 14:38:54 +00:00
|
|
|
const form = useForm<AttributeForm>({ mode: "onChange" });
|
2022-11-17 16:14:47 +00:00
|
|
|
const { config } = useUserProfile();
|
2021-10-25 14:38:54 +00:00
|
|
|
|
2022-11-17 16:14:47 +00:00
|
|
|
const convertAttributes = () => {
|
2023-06-14 06:02:12 +00:00
|
|
|
return arrayToKeyValue<UserRepresentation>(user.attributes!);
|
2021-10-25 14:38:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
form.setValue("attributes", convertAttributes());
|
2023-04-11 06:08:01 +00:00
|
|
|
}, [user, config]);
|
2021-10-25 14:38:54 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<PageSection variant={PageSectionVariants.light}>
|
|
|
|
<AttributesForm
|
|
|
|
form={form}
|
|
|
|
save={save}
|
2022-04-21 15:03:48 +00:00
|
|
|
fineGrainedAccess={user.access?.manage}
|
2021-10-25 14:38:54 +00:00
|
|
|
reset={() =>
|
|
|
|
form.reset({
|
|
|
|
attributes: convertAttributes(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PageSection>
|
|
|
|
);
|
|
|
|
};
|