Client credentials: "Regenerate" button fix (#2337)

This commit is contained in:
Jenny 2022-03-28 16:54:15 -04:00 committed by GitHub
parent 0c628e2b7c
commit 86b3fb93a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 13 deletions

View file

@ -216,6 +216,7 @@ export default function ClientDetails() {
}); });
const setupForm = (client: ClientRepresentation) => { const setupForm = (client: ClientRepresentation) => {
form.reset({ ...client });
convertToFormValues(client, form.setValue); convertToFormValues(client, form.setValue);
form.setValue( form.setValue(
"attributes.request.uris", "attributes.request.uris",
@ -383,7 +384,11 @@ export default function ClientDetails() {
title={<TabTitleText>{t("credentials")}</TabTitleText>} title={<TabTitleText>{t("credentials")}</TabTitleText>}
{...route("credentials")} {...route("credentials")}
> >
<Credentials clientId={clientId} save={() => save()} /> <Credentials
form={form}
clientId={clientId}
save={() => save()}
/>
</Tab> </Tab>
)} )}
<Tab <Tab

View file

@ -7,17 +7,18 @@ import {
Split, Split,
SplitItem, SplitItem,
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import { useFormContext } from "react-hook-form";
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation"; import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
import type { UseFormMethods } from "react-hook-form";
export type ClientSecretProps = { export type ClientSecretProps = {
secret: string; secret: string;
toggle: () => void; toggle: () => void;
form: UseFormMethods<ClientRepresentation>;
}; };
export const ClientSecret = ({ secret, toggle }: ClientSecretProps) => { export const ClientSecret = ({ secret, toggle, form }: ClientSecretProps) => {
const { t } = useTranslation("clients"); const { t } = useTranslation("clients");
const { formState } = useFormContext<ClientRepresentation>();
return ( return (
<FormGroup label={t("clientSecret")} fieldId="kc-client-secret"> <FormGroup label={t("clientSecret")} fieldId="kc-client-secret">
<Split hasGutter> <Split hasGutter>
@ -29,8 +30,8 @@ export const ClientSecret = ({ secret, toggle }: ClientSecretProps) => {
<SplitItem> <SplitItem>
<Button <Button
variant="secondary" variant="secondary"
isDisabled={form.formState.isDirty}
onClick={toggle} onClick={toggle}
isDisabled={formState.isDirty}
> >
{t("regenerate")} {t("regenerate")}
</Button> </Button>

View file

@ -1,5 +1,10 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { Controller, useFormContext, useWatch } from "react-hook-form"; import {
Controller,
useFormContext,
UseFormMethods,
useWatch,
} from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { import {
ActionGroup, ActionGroup,
@ -29,6 +34,7 @@ import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
import { ClientSecret } from "./ClientSecret"; import { ClientSecret } from "./ClientSecret";
import { SignedJWT } from "./SignedJWT"; import { SignedJWT } from "./SignedJWT";
import { X509 } from "./X509"; import { X509 } from "./X509";
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
type AccessToken = { type AccessToken = {
registrationAccessToken: string; registrationAccessToken: string;
@ -37,9 +43,10 @@ type AccessToken = {
export type CredentialsProps = { export type CredentialsProps = {
clientId: string; clientId: string;
save: () => void; save: () => void;
form: UseFormMethods<ClientRepresentation>;
}; };
export const Credentials = ({ clientId, save }: CredentialsProps) => { export const Credentials = ({ clientId, save, form }: CredentialsProps) => {
const { t } = useTranslation("clients"); const { t } = useTranslation("clients");
const adminClient = useAdminClient(); const adminClient = useAdminClient();
const { addAlert, addError } = useAlerts(); const { addAlert, addError } = useAlerts();
@ -51,7 +58,9 @@ export const Credentials = ({ clientId, save }: CredentialsProps) => {
const { const {
control, control,
formState: { isDirty }, formState: { isDirty },
handleSubmit,
} = useFormContext(); } = useFormContext();
const clientAuthenticatorType = useWatch({ const clientAuthenticatorType = useWatch({
control: control, control: control,
name: "clientAuthenticatorType", name: "clientAuthenticatorType",
@ -131,7 +140,12 @@ export const Credentials = ({ clientId, save }: CredentialsProps) => {
return ( return (
<PageSection> <PageSection>
<FormAccess isHorizontal className="pf-u-mt-md" role="manage-clients"> <FormAccess
onSubmit={handleSubmit(save)}
isHorizontal
className="pf-u-mt-md"
role="manage-clients"
>
<ClientSecretConfirm /> <ClientSecretConfirm />
<AccessTokenConfirm /> <AccessTokenConfirm />
<Card isFlat> <Card isFlat>
@ -180,11 +194,7 @@ export const Credentials = ({ clientId, save }: CredentialsProps) => {
{clientAuthenticatorType === "client-jwt" && <SignedJWT />} {clientAuthenticatorType === "client-jwt" && <SignedJWT />}
{clientAuthenticatorType === "client-x509" && <X509 />} {clientAuthenticatorType === "client-x509" && <X509 />}
<ActionGroup> <ActionGroup>
<Button <Button variant="primary" type="submit" isDisabled={!isDirty}>
variant="primary"
onClick={() => save()}
isDisabled={!isDirty}
>
{t("common:save")} {t("common:save")}
</Button> </Button>
</ActionGroup> </ActionGroup>
@ -195,6 +205,7 @@ export const Credentials = ({ clientId, save }: CredentialsProps) => {
clientAuthenticatorType === "client-secret-jwt") && ( clientAuthenticatorType === "client-secret-jwt") && (
<CardBody> <CardBody>
<ClientSecret <ClientSecret
form={form}
secret={secret} secret={secret}
toggle={toggleClientSecretConfirm} toggle={toggleClientSecretConfirm}
/> />