From 93ee81b6afabed0c482d00d1f1f00de9e14d5726 Mon Sep 17 00:00:00 2001 From: agagancarczyk Date: Tue, 30 Nov 2021 16:18:47 +0000 Subject: [PATCH] User label cred edit (#1624) * user credentials - wip * user credentials - wip * user credentials - wip * user credentials - wip * user credentials - wip * user credentials - wip * user credentials - wip * user credentials - wip * user credentials - wip * added deleting credentials * replaced DataList with Table * added reset password - wip * added reset password * added show data dialog - wip * added show data dialog - wip * added password data dialog * added few translations * added sorting to password data * tidied up * clean up rows code * feedback fixes * editanle user label - wip * editanle user label - wip * editable user label - wip * editable user label - wip * editable user label - wip * editable user label - final * added extra check for credentialId Co-authored-by: Agnieszka Gancarczyk Co-authored-by: Jon Koops --- src/user/UserCredentials.tsx | 110 ++++++++++++++++++++++++++++++++++- src/user/messages.ts | 3 + src/user/user-section.css | 29 +++++++++ 3 files changed, 139 insertions(+), 3 deletions(-) diff --git a/src/user/UserCredentials.tsx b/src/user/UserCredentials.tsx index 86935e9895..43233fcb7b 100644 --- a/src/user/UserCredentials.tsx +++ b/src/user/UserCredentials.tsx @@ -13,6 +13,7 @@ import { ModalVariant, Switch, Text, + TextInput, TextVariants, ValidatedOptions, } from "@patternfly/react-core"; @@ -28,6 +29,7 @@ import { Thead, Tr, } from "@patternfly/react-table"; +import { PencilAltIcon, CheckIcon, TimesIcon } from "@patternfly/react-icons"; import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation"; import { useTranslation } from "react-i18next"; import { useAlerts } from "../components/alert/Alerts"; @@ -40,6 +42,7 @@ import { HelpItem } from "../components/help-enabler/HelpItem"; import "./user-section.css"; import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; import type CredentialRepresentation from "@keycloak/keycloak-admin-client/lib/defs/credentialRepresentation"; +import { FormAccess } from "../components/form-access/FormAccess"; type UserCredentialsProps = { user: UserRepresentation; @@ -51,7 +54,7 @@ type CredentialsForm = { temporaryPassword: boolean; }; -const defaultValues: CredentialsForm = { +const credFormDefaultValues: CredentialsForm = { password: "", passwordConfirmation: "", temporaryPassword: true, @@ -62,6 +65,14 @@ type DisplayDialogProps = { onClose: () => void; }; +type UserLabelForm = { + userLabel: string; +}; + +const userLabelDefaultValues: UserLabelForm = { + userLabel: "", +}; + const DisplayDialog: FunctionComponent = ({ titleKey, onClose, @@ -90,8 +101,18 @@ export const UserCredentials = ({ user }: UserCredentialsProps) => { const [openSaveConfirm, setOpenSaveConfirm] = useState(false); const [kebabOpen, setKebabOpen] = useState(false); const adminClient = useAdminClient(); - const form = useForm({ defaultValues }); + const form = useForm({ + defaultValues: credFormDefaultValues, + }); + const userLabelForm = useForm({ + defaultValues: userLabelDefaultValues, + }); const { control, errors, handleSubmit, register } = form; + const { + getValues: getValues1, + handleSubmit: handleSubmit1, + register: register1, + } = userLabelForm; const [credentials, setCredentials] = useState(); const [userCredentials, setUserCredentials] = useState< CredentialRepresentation[] @@ -100,6 +121,9 @@ export const UserCredentials = ({ user }: UserCredentialsProps) => { useState({}); const [isResetPassword, setIsResetPassword] = useState(false); const [showData, setShowData] = useState(false); + const [isUserLabelEdit, setIsUserLabelEdit] = useState(false); + const [editedUserCredential, setEditedUserCredential] = + useState({}); useFetch( () => adminClient.users.getCredentials({ id: user.id! }), @@ -218,6 +242,30 @@ export const UserCredentials = ({ user }: UserCredentialsProps) => { }); }, [selectedCredential.credentialData]); + const saveUserLabel = async () => { + const userLabelFormValue = getValues1(); + + if (Object.keys(editedUserCredential).length === 0) { + return; + } + + try { + await adminClient.users.updateCredentialLabel( + { + id: user.id!, + credentialId: editedUserCredential.id!, + }, + userLabelFormValue.userLabel || "" + ); + refresh(); + addAlert(t("updateCredentialUserLabelSuccess"), AlertVariant.success); + setIsUserLabelEdit(false); + } catch (error) { + addError(t("updateCredentialUserLabelError"), error); + setIsUserLabelEdit(false); + } + }; + return ( <> {open && ( @@ -439,7 +487,63 @@ export const UserCredentials = ({ user }: UserCredentialsProps) => { {credential.type?.charAt(0).toUpperCase()! + credential.type?.slice(1)} - My Password + + + +
+ {isUserLabelEdit ? ( + <> + +
+
+ + ) : ( + <> + {credential.userLabel ?? ""} +
+
+
+