keycloak-scim/src/user/UserForm.tsx

232 lines
6.6 KiB
TypeScript
Raw Normal View History

2021-03-03 18:56:03 +00:00
import React, { useState } from "react";
2021-03-03 13:53:42 +00:00
import {
ActionGroup,
Button,
FormGroup,
2021-03-03 18:56:03 +00:00
Select,
SelectOption,
Switch,
2021-03-03 13:53:42 +00:00
TextInput,
} from "@patternfly/react-core";
import { useTranslation } from "react-i18next";
2021-03-03 18:56:03 +00:00
import { Controller, UseFormMethods } from "react-hook-form";
import { useHistory } from "react-router-dom";
2021-03-03 13:53:42 +00:00
import { FormAccess } from "../components/form-access/FormAccess";
import UserRepresentation from "keycloak-admin/lib/defs/userRepresentation";
2021-03-03 18:56:03 +00:00
import { HelpItem } from "../components/help-enabler/HelpItem";
import { useRealm } from "../context/realm-context/RealmContext";
2021-03-03 13:53:42 +00:00
export type UserFormProps = {
form: UseFormMethods<UserRepresentation>;
save: (user: UserRepresentation) => void;
};
2021-03-03 18:56:03 +00:00
export const UserForm = ({ form, save }: UserFormProps) => {
2021-03-03 13:53:42 +00:00
const { t } = useTranslation("users");
2021-03-03 18:56:03 +00:00
const { realm } = useRealm();
const [
isRequiredUserActionsDropdownOpen,
setRequiredUserActionsDropdownOpen,
] = useState(false);
const [selected, setSelected] = useState<string[]>([]);
const history = useHistory();
const requiredUserActionsOptions = [
<SelectOption key={0} value="Configure OTP">
{t("configureOTP")}
</SelectOption>,
<SelectOption key={1} value="Update Password">
{t("updatePassword")}
</SelectOption>,
<SelectOption key={2} value="Update Profile">
{t("updateProfile")}
</SelectOption>,
<SelectOption key={3} value="Verify Email">
{t("verifyEmail")}
</SelectOption>,
<SelectOption key={4} value="Update User Locale">
{t("updateUserLocale")}
</SelectOption>,
];
const clearSelection = () => {
setSelected([]);
setRequiredUserActionsDropdownOpen(false);
};
2021-03-03 13:53:42 +00:00
return (
<FormAccess
isHorizontal
onSubmit={form.handleSubmit(save)}
2021-03-03 18:56:03 +00:00
role="manage-users"
2021-03-03 13:53:42 +00:00
className="pf-u-mt-lg"
>
<FormGroup
2021-03-03 18:56:03 +00:00
label={t("username")}
fieldId="kc-username"
2021-03-03 13:53:42 +00:00
isRequired
2021-03-03 18:56:03 +00:00
validated={form.errors.username ? "error" : "default"}
2021-03-03 13:53:42 +00:00
helperTextInvalid={t("common:required")}
>
<TextInput
2021-03-03 18:56:03 +00:00
ref={form.register()}
2021-03-03 13:53:42 +00:00
type="text"
2021-03-03 18:56:03 +00:00
id="kc-username"
name="username"
2021-03-03 13:53:42 +00:00
/>
</FormGroup>
<FormGroup
2021-03-03 18:56:03 +00:00
label={t("email")}
2021-03-03 13:53:42 +00:00
fieldId="kc-description"
2021-03-03 18:56:03 +00:00
validated={form.errors.email ? "error" : "default"}
helperTextInvalid={form.errors.email?.message}
>
<TextInput
ref={form.register()}
type="text"
id="kc-email"
name="email"
/>
</FormGroup>
<FormGroup
label={t("emailVerified")}
fieldId="kc-email-verified"
helperTextInvalid={t("common:required")}
labelIcon={
<HelpItem
helpText={t("emailVerifiedHelpText")}
forLabel={t("emailVerified")}
forID="email-verified"
/>
}
>
<Controller
name="user-email-verified"
defaultValue={false}
control={form.control}
render={({ onChange, value }) => (
<Switch
id={"kc-user-email-verified"}
isDisabled={false}
onChange={(value) => onChange([`${value}`])}
isChecked={value[0] === "true"}
label={t("common:on")}
labelOff={t("common:off")}
/>
)}
></Controller>
</FormGroup>
<FormGroup
label={t("firstName")}
fieldId="kc-firstname"
validated={form.errors.firstName ? "error" : "default"}
helperTextInvalid={t("common:required")}
>
<TextInput
ref={form.register()}
type="text"
id="kc-firstname"
name="firstname"
/>
</FormGroup>
<FormGroup
label={t("lastName")}
fieldId="kc-name"
validated={form.errors.lastName ? "error" : "default"}
2021-03-03 13:53:42 +00:00
>
2021-03-03 18:56:03 +00:00
<TextInput
ref={form.register()}
2021-03-03 13:53:42 +00:00
type="text"
2021-03-03 18:56:03 +00:00
id="kc-lastname"
name="lastname"
2021-03-03 13:53:42 +00:00
/>
</FormGroup>
2021-03-03 18:56:03 +00:00
<FormGroup
label={t("common:enabled")}
fieldId="kc-enabled"
labelIcon={
<HelpItem
helpText={t("disabledHelpText")}
forLabel={t("enabled")}
forID="enabled-label"
/>
}
>
<Controller
name="user-enabled"
defaultValue={false}
control={form.control}
render={({ onChange, value }) => (
<Switch
id={"kc-user-enabled"}
isDisabled={false}
onChange={(value) => onChange([`${value}`])}
isChecked={value[0] === "true"}
label={t("common:on")}
labelOff={t("common:off")}
/>
)}
></Controller>
</FormGroup>
<FormGroup
label={t("requiredUserActions")}
fieldId="kc-required-user-actions"
validated={form.errors.requiredActions ? "error" : "default"}
helperTextInvalid={t("common:required")}
labelIcon={
<HelpItem
helpText={t("requiredUserActionsHelpText")}
forLabel={t("requiredUserActions")}
forID="required-user-actions-label"
/>
}
>
<Controller
name="required-user-actions"
defaultValue={["0"]}
typeAheadAriaLabel="Select an action"
control={form.control}
render={() => (
<Select
placeholderText="Select action"
toggleId="kc-required-user-actions"
onToggle={() =>
setRequiredUserActionsDropdownOpen(
!isRequiredUserActionsDropdownOpen
)
}
isOpen={isRequiredUserActionsDropdownOpen}
selections={selected}
onSelect={(_, value) => {
const option = value as string;
if (selected.includes(option)) {
setSelected(selected.filter((item) => item !== option));
} else {
setSelected([...selected, option]);
}
}}
onClear={clearSelection}
variant="typeaheadmulti"
>
{requiredUserActionsOptions}
</Select>
)}
></Controller>
</FormGroup>
2021-03-03 13:53:42 +00:00
<ActionGroup>
<Button variant="primary" type="submit">
2021-03-03 18:56:03 +00:00
{t("common:Create")}
2021-03-03 13:53:42 +00:00
</Button>
2021-03-03 19:12:16 +00:00
<Button
data-testid="cancel-create-user"
onClick={() => history.push(`/${realm}/users`)}
variant="link"
>
2021-03-03 18:56:03 +00:00
{t("common:cancel")}
2021-03-03 13:53:42 +00:00
</Button>
</ActionGroup>
</FormAccess>
);
};