keycloak-scim/js/apps/admin-ui/src/user/UserAttributes.tsx

45 lines
1.3 KiB
TypeScript
Raw Normal View History

import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
import { PageSection, PageSectionVariants } from "@patternfly/react-core";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
2021-10-25 14:38:54 +00:00
import {
AttributeForm,
AttributesForm,
} from "../components/key-value-form/AttributeForm";
import { arrayToKeyValue } from "../components/key-value-form/key-value-convert";
import { useUserProfile } from "../realm-settings/user-profile/UserProfileContext";
2021-10-25 14:38:54 +00:00
type UserAttributesProps = {
user: UserRepresentation;
save: (user: UserRepresentation) => void;
2021-10-25 14:38:54 +00:00
};
export const UserAttributes = ({ user, save }: UserAttributesProps) => {
2021-10-25 14:38:54 +00:00
const form = useForm<AttributeForm>({ mode: "onChange" });
const { config } = useUserProfile();
2021-10-25 14:38:54 +00:00
const convertAttributes = () => {
return arrayToKeyValue<UserRepresentation>(user.attributes!);
2021-10-25 14:38:54 +00:00
};
useEffect(() => {
form.setValue("attributes", convertAttributes());
}, [user, config]);
2021-10-25 14:38:54 +00:00
return (
<PageSection variant={PageSectionVariants.light}>
<AttributesForm
form={form}
save={save}
fineGrainedAccess={user.access?.manage}
2021-10-25 14:38:54 +00:00
reset={() =>
form.reset({
attributes: convertAttributes(),
})
}
/>
</PageSection>
);
};