changed to use ui-shared (#27996)

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
This commit is contained in:
Erik Jan de Wit 2024-03-22 11:47:06 +01:00 committed by GitHub
parent 53d52ecf15
commit 2d73c86306
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 74 deletions

View file

@ -6,10 +6,9 @@ export default class InitialAccessTokenTab extends CommonPage {
#emptyAction = "no-initial-access-tokens-empty-action"; #emptyAction = "no-initial-access-tokens-empty-action";
#expirationNumberInput = "expiration"; #expirationNumberInput = "expiration";
#expirationInput = 'input[name="count"]';
#expirationText = "#expiration-helper"; #expirationText = "#expiration-helper";
#countInput = '[data-testid="count"] input'; #countInput = "#count input";
#countPlusBtn = '[data-testid="count"] [aria-label="Plus"]'; #countPlusBtn = '#count [aria-label="Plus"]';
#saveBtn = "save"; #saveBtn = "save";
goToInitialAccessTokenTab() { goToInitialAccessTokenTab() {
@ -42,7 +41,8 @@ export default class InitialAccessTokenTab extends CommonPage {
} }
fillNewTokenData(expiration: number, count: number) { fillNewTokenData(expiration: number, count: number) {
cy.findByTestId(this.#expirationNumberInput).clear().type(`${expiration}`); cy.findByTestId(this.#expirationNumberInput).clear();
cy.findByTestId(this.#expirationNumberInput).type(`${expiration}`);
cy.get(this.#countInput).clear(); cy.get(this.#countInput).clear();
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
@ -66,7 +66,7 @@ export default class InitialAccessTokenTab extends CommonPage {
} }
checkCountValue(value: number) { checkCountValue(value: number) {
cy.get(this.#expirationInput).should("have.value", value); cy.get(this.#countInput).should("have.value", value);
return this; return this;
} }

View file

@ -3,32 +3,29 @@ import {
ActionGroup, ActionGroup,
AlertVariant, AlertVariant,
Button, Button,
FormGroup,
NumberInput,
PageSection, PageSection,
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import { useState } from "react"; import { useState } from "react";
import { Controller, useForm } from "react-hook-form"; import { FormProvider, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { FormAccess } from "../../components/form/FormAccess";
import { ViewHeader } from "../../components/view-header/ViewHeader";
import { HelpItem } from "ui-shared";
import { TimeSelector } from "../../components/time-selector/TimeSelector";
import { Link, useNavigate } from "react-router-dom"; import { Link, useNavigate } from "react-router-dom";
import { NumberControl } from "ui-shared";
import { adminClient } from "../../admin-client"; import { adminClient } from "../../admin-client";
import { useAlerts } from "../../components/alert/Alerts"; import { useAlerts } from "../../components/alert/Alerts";
import { FormAccess } from "../../components/form/FormAccess";
import { TimeSelectorControl } from "../../components/time-selector/TimeSelectorControl";
import { ViewHeader } from "../../components/view-header/ViewHeader";
import { useRealm } from "../../context/realm-context/RealmContext"; import { useRealm } from "../../context/realm-context/RealmContext";
import { toClients } from "../routes/Clients"; import { toClients } from "../routes/Clients";
import { AccessTokenDialog } from "./AccessTokenDialog"; import { AccessTokenDialog } from "./AccessTokenDialog";
export default function CreateInitialAccessToken() { export default function CreateInitialAccessToken() {
const { t } = useTranslation(); const { t } = useTranslation();
const form = useForm({ mode: "onChange" });
const { const {
handleSubmit, handleSubmit,
control, formState: { isValid },
formState: { isValid, errors }, } = form;
} = useForm({ mode: "onChange" });
const { realm } = useRealm(); const { realm } = useRealm();
const { addAlert, addError } = useAlerts(); const { addAlert, addError } = useAlerts();
@ -49,7 +46,7 @@ export default function CreateInitialAccessToken() {
}; };
return ( return (
<> <FormProvider {...form}>
{token && ( {token && (
<AccessTokenDialog <AccessTokenDialog
token={token} token={token}
@ -67,64 +64,29 @@ export default function CreateInitialAccessToken() {
role="create-client" role="create-client"
onSubmit={handleSubmit(save)} onSubmit={handleSubmit(save)}
> >
<FormGroup <TimeSelectorControl
label={t("expiration")}
fieldId="expiration"
labelIcon={
<HelpItem
helpText={t("tokenExpirationHelp")}
fieldLabelId="expiration"
/>
}
helperTextInvalid={t("expirationValueNotValid")}
validated={errors.expiration ? "error" : "default"}
>
<Controller
name="expiration" name="expiration"
defaultValue={86400} label={t("expiration")}
control={control} labelIcon={t("tokenExpirationHelp")}
rules={{ min: 1 }} controller={{
render={({ field }) => ( defaultValue: 86400,
<TimeSelector rules: {
data-testid="expiration" min: {
value={field.value} value: 1,
onChange={field.onChange} message: t("expirationValueNotValid"),
min={1} },
validated={errors.expiration ? "error" : "default"} },
/>
)}
/>
</FormGroup>
<FormGroup
label={t("count")}
fieldId="count"
labelIcon={
<HelpItem helpText={t("countHelp")} fieldLabelId="count" />
}
>
<Controller
name="count"
defaultValue={1}
control={control}
render={({ field }) => (
<NumberInput
data-testid="count"
inputName="count"
inputAriaLabel={t("count")}
min={1}
value={field.value}
onPlus={() => field.onChange(field.value + 1)}
onMinus={() => field.onChange(field.value - 1)}
onChange={(event) => {
const value = Number(
(event.target as HTMLInputElement).value,
);
field.onChange(value < 1 ? 1 : value);
}} }}
/> />
)} <NumberControl
name="count"
label={t("count")}
labelIcon={t("countHelp")}
controller={{
defaultValue: 1,
}}
min={1}
/> />
</FormGroup>
<ActionGroup> <ActionGroup>
<Button <Button
variant="primary" variant="primary"
@ -149,6 +111,6 @@ export default function CreateInitialAccessToken() {
</ActionGroup> </ActionGroup>
</FormAccess> </FormAccess>
</PageSection> </PageSection>
</> </FormProvider>
); );
} }