2023-01-26 09:31:07 +00:00
|
|
|
import type CredentialRepresentation from "@keycloak/keycloak-admin-client/lib/defs/credentialRepresentation";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { AlertVariant, Button, Form, FormGroup } from "@patternfly/react-core";
|
2022-02-09 14:39:10 +00:00
|
|
|
import { CheckIcon, PencilAltIcon, TimesIcon } from "@patternfly/react-icons";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2022-02-09 14:39:10 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../../admin-client";
|
2022-02-09 14:39:10 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
|
2022-02-09 14:39:10 +00:00
|
|
|
|
|
|
|
type UserLabelForm = {
|
|
|
|
userLabel: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type InlineLabelEditProps = {
|
|
|
|
userId: string;
|
|
|
|
credential: CredentialRepresentation;
|
|
|
|
isEditable: boolean;
|
|
|
|
toggle: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const InlineLabelEdit = ({
|
|
|
|
userId,
|
|
|
|
credential,
|
|
|
|
isEditable,
|
|
|
|
toggle,
|
|
|
|
}: InlineLabelEditProps) => {
|
|
|
|
const { t } = useTranslation("users");
|
|
|
|
const { register, handleSubmit } = useForm<UserLabelForm>();
|
|
|
|
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
const saveUserLabel = async (userLabel: UserLabelForm) => {
|
|
|
|
try {
|
|
|
|
await adminClient.users.updateCredentialLabel(
|
|
|
|
{
|
|
|
|
id: userId,
|
|
|
|
credentialId: credential.id!,
|
|
|
|
},
|
|
|
|
userLabel.userLabel || ""
|
|
|
|
);
|
|
|
|
addAlert(t("updateCredentialUserLabelSuccess"), AlertVariant.success);
|
|
|
|
toggle();
|
|
|
|
} catch (error) {
|
|
|
|
addError("users:updateCredentialUserLabelError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-03-28 13:32:06 +00:00
|
|
|
<Form
|
|
|
|
isHorizontal
|
|
|
|
className="kc-form-userLabel"
|
|
|
|
onSubmit={handleSubmit(saveUserLabel)}
|
|
|
|
>
|
2022-02-09 14:39:10 +00:00
|
|
|
<FormGroup fieldId="kc-userLabel" className="kc-userLabel-row">
|
|
|
|
<div className="kc-form-group-userLabel">
|
|
|
|
{isEditable ? (
|
|
|
|
<>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-03-07 14:32:34 +00:00
|
|
|
data-testid="userLabelFld"
|
2022-02-09 14:39:10 +00:00
|
|
|
defaultValue={credential.userLabel}
|
|
|
|
className="kc-userLabel"
|
|
|
|
aria-label={t("userLabel")}
|
2023-01-26 09:31:07 +00:00
|
|
|
{...register("userLabel")}
|
2022-02-09 14:39:10 +00:00
|
|
|
/>
|
|
|
|
<div className="kc-userLabel-actionBtns">
|
|
|
|
<Button
|
2022-03-07 14:32:34 +00:00
|
|
|
data-testid="editUserLabelAcceptBtn"
|
2022-02-09 14:39:10 +00:00
|
|
|
variant="link"
|
2022-03-07 14:32:34 +00:00
|
|
|
className="kc-editUserLabelAcceptBtn"
|
2022-03-28 13:32:06 +00:00
|
|
|
type="submit"
|
2022-02-09 14:39:10 +00:00
|
|
|
icon={<CheckIcon />}
|
|
|
|
/>
|
|
|
|
<Button
|
2022-03-07 14:32:34 +00:00
|
|
|
data-testid="editUserLabelCancelBtn"
|
2022-02-09 14:39:10 +00:00
|
|
|
variant="link"
|
|
|
|
className="kc-editUserLabel-cancelBtn"
|
|
|
|
onClick={toggle}
|
|
|
|
icon={<TimesIcon />}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{credential.userLabel}
|
|
|
|
<Button
|
2022-06-27 08:45:15 +00:00
|
|
|
aria-label={t("editUserLabel")}
|
2022-02-09 14:39:10 +00:00
|
|
|
variant="link"
|
|
|
|
className="kc-editUserLabel-btn"
|
|
|
|
onClick={toggle}
|
|
|
|
data-testid="editUserLabelBtn"
|
|
|
|
icon={<PencilAltIcon />}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|