Use react-hook-form v7 for events section (#3813)

This commit is contained in:
Jon Koops 2022-11-21 16:54:33 +01:00 committed by GitHub
parent 926d71e760
commit c9b783f9e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,6 @@
import type EventRepresentation from "@keycloak/keycloak-admin-client/lib/defs/eventRepresentation";
import type EventType from "@keycloak/keycloak-admin-client/lib/defs/eventTypes";
import type { RealmEventsConfigRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/realmEventsConfigRepresentation";
import { import {
ActionGroup, ActionGroup,
Button, Button,
@ -24,30 +27,28 @@ import {
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import { CheckCircleIcon, WarningTriangleIcon } from "@patternfly/react-icons"; import { CheckCircleIcon, WarningTriangleIcon } from "@patternfly/react-icons";
import { cellWidth, expandable } from "@patternfly/react-table"; import { cellWidth, expandable } from "@patternfly/react-table";
import type EventRepresentation from "@keycloak/keycloak-admin-client/lib/defs/eventRepresentation";
import type EventType from "@keycloak/keycloak-admin-client/lib/defs/eventTypes";
import type { RealmEventsConfigRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/realmEventsConfigRepresentation";
import { pickBy } from "lodash-es"; import { pickBy } from "lodash-es";
import { useState } from "react"; import { useState } from "react";
import { Controller, useForm } from "react-hook-form"; import { Controller, useForm } from "react-hook-form-v7";
import { Trans, useTranslation } from "react-i18next"; import { Trans, useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom"; import { useHistory } from "react-router-dom";
import { Link } from "react-router-dom-v5-compat"; import { Link } from "react-router-dom-v5-compat";
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
import { ViewHeader } from "../components/view-header/ViewHeader";
import { KeycloakTextInput } from "../components/keycloak-text-input/KeycloakTextInput"; import { KeycloakTextInput } from "../components/keycloak-text-input/KeycloakTextInput";
import { useAdminClient, useFetch } from "../context/auth/AdminClient"; import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import { useRealm } from "../context/realm-context/RealmContext";
import { toRealmSettings } from "../realm-settings/routes/RealmSettings";
import { toUser } from "../user/routes/User";
import useFormatDate, { FORMAT_DATE_AND_TIME } from "../utils/useFormatDate";
import { AdminEvents } from "./AdminEvents";
import helpUrls from "../help-urls";
import { import {
routableTab, routableTab,
RoutableTabs, RoutableTabs,
} from "../components/routable-tabs/RoutableTabs"; } from "../components/routable-tabs/RoutableTabs";
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
import { ViewHeader } from "../components/view-header/ViewHeader";
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
import { useRealm } from "../context/realm-context/RealmContext";
import helpUrls from "../help-urls";
import { toRealmSettings } from "../realm-settings/routes/RealmSettings";
import { toUser } from "../user/routes/User";
import useFormatDate, { FORMAT_DATE_AND_TIME } from "../utils/useFormatDate";
import { AdminEvents } from "./AdminEvents";
import { EventsTab, toEvents } from "./routes/Events"; import { EventsTab, toEvents } from "./routes/Events";
import "./events.css"; import "./events.css";
@ -119,8 +120,8 @@ export default function EventsSection() {
reset, reset,
formState: { isDirty }, formState: { isDirty },
control, control,
handleSubmit,
} = useForm<UserEventSearchForm>({ } = useForm<UserEventSearchForm>({
shouldUnregister: false,
mode: "onChange", mode: "onChange",
defaultValues, defaultValues,
}); });
@ -141,7 +142,7 @@ export default function EventsSection() {
}); });
} }
function submitSearch() { function onSubmit() {
setSearchDropdownOpen(false); setSearchDropdownOpen(false);
commitFilters(); commitFilters();
} }
@ -228,9 +229,10 @@ export default function EventsSection() {
isOpen={searchDropdownOpen} isOpen={searchDropdownOpen}
> >
<Form <Form
isHorizontal
className="keycloak__events_search__form"
data-testid="searchForm" data-testid="searchForm"
className="keycloak__events_search__form"
onSubmit={handleSubmit(onSubmit)}
isHorizontal
> >
<FormGroup <FormGroup
label={t("userId")} label={t("userId")}
@ -238,11 +240,9 @@ export default function EventsSection() {
className="keycloak__events_search__form_label" className="keycloak__events_search__form_label"
> >
<KeycloakTextInput <KeycloakTextInput
ref={register()}
type="text"
id="kc-userId" id="kc-userId"
name="user"
data-testid="userId-searchField" data-testid="userId-searchField"
{...register("user")}
/> />
</FormGroup> </FormGroup>
<FormGroup <FormGroup
@ -253,13 +253,7 @@ export default function EventsSection() {
<Controller <Controller
name="type" name="type"
control={control} control={control}
render={({ render={({ field }) => (
onChange,
value,
}: {
onChange: (newValue: EventType[]) => void;
value: EventType[];
}) => (
<Select <Select
className="keycloak__events_search__type_select" className="keycloak__events_search__type_select"
name="eventType" name="eventType"
@ -272,29 +266,31 @@ export default function EventsSection() {
variant={SelectVariant.typeaheadMulti} variant={SelectVariant.typeaheadMulti}
typeAheadAriaLabel="Select" typeAheadAriaLabel="Select"
onToggle={(isOpen) => setSelectOpen(isOpen)} onToggle={(isOpen) => setSelectOpen(isOpen)}
selections={value} selections={field.value}
onSelect={(_, selectedValue) => { onSelect={(_, selectedValue) => {
const option = selectedValue.toString() as EventType; const option = selectedValue.toString() as EventType;
const changedValue = value.includes(option) const changedValue = field.value.includes(option)
? value.filter((item) => item !== option) ? field.value.filter((item) => item !== option)
: [...value, option]; : [...field.value, option];
onChange(changedValue); field.onChange(changedValue);
}} }}
onClear={(event) => { onClear={(event) => {
event.stopPropagation(); event.stopPropagation();
onChange([]); field.onChange([]);
}} }}
isOpen={selectOpen} isOpen={selectOpen}
aria-labelledby={"eventType"} aria-labelledby={"eventType"}
chipGroupComponent={ chipGroupComponent={
<ChipGroup> <ChipGroup>
{value.map((chip) => ( {field.value.map((chip) => (
<Chip <Chip
key={chip} key={chip}
onClick={(event) => { onClick={(event) => {
event.stopPropagation(); event.stopPropagation();
onChange(value.filter((val) => val !== chip)); field.onChange(
field.value.filter((val) => val !== chip)
);
}} }}
> >
{chip} {chip}
@ -316,11 +312,9 @@ export default function EventsSection() {
className="keycloak__events_search__form_label" className="keycloak__events_search__form_label"
> >
<KeycloakTextInput <KeycloakTextInput
ref={register()}
type="text"
id="kc-client" id="kc-client"
name="client"
data-testid="client-searchField" data-testid="client-searchField"
{...register("client")}
/> />
</FormGroup> </FormGroup>
<FormGroup <FormGroup
@ -331,11 +325,11 @@ export default function EventsSection() {
<Controller <Controller
name="dateFrom" name="dateFrom"
control={control} control={control}
render={({ onChange, value }) => ( render={({ field }) => (
<DatePicker <DatePicker
className="pf-u-w-100" className="pf-u-w-100"
value={value} value={field.value}
onChange={(value) => onChange(value)} onChange={(value) => field.onChange(value)}
inputProps={{ id: "kc-dateFrom" }} inputProps={{ id: "kc-dateFrom" }}
/> />
)} )}
@ -349,11 +343,11 @@ export default function EventsSection() {
<Controller <Controller
name="dateTo" name="dateTo"
control={control} control={control}
render={({ onChange, value }) => ( render={({ field }) => (
<DatePicker <DatePicker
className="pf-u-w-100" className="pf-u-w-100"
value={value} value={field.value}
onChange={(value) => onChange(value)} onChange={(value) => field.onChange(value)}
inputProps={{ id: "kc-dateTo" }} inputProps={{ id: "kc-dateTo" }}
/> />
)} )}
@ -361,9 +355,9 @@ export default function EventsSection() {
</FormGroup> </FormGroup>
<ActionGroup> <ActionGroup>
<Button <Button
variant={"primary"}
onClick={submitSearch}
data-testid="search-events-btn" data-testid="search-events-btn"
variant="primary"
type="submit"
isDisabled={!isDirty} isDisabled={!isDirty}
> >
{t("searchUserEventsBtn")} {t("searchUserEventsBtn")}