2022-12-07 09:03:38 +00:00
|
|
|
import type PolicyProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyProviderRepresentation";
|
2022-01-12 16:01:54 +00:00
|
|
|
import {
|
2022-01-24 14:17:32 +00:00
|
|
|
ActionGroup,
|
|
|
|
Button,
|
2022-01-12 16:01:54 +00:00
|
|
|
Dropdown,
|
|
|
|
DropdownToggle,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
} from "@patternfly/react-core";
|
2022-12-07 09:03:38 +00:00
|
|
|
import { useEffect } from "react";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2022-12-07 09:03:38 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-01-12 16:01:54 +00:00
|
|
|
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
|
2022-12-07 09:03:38 +00:00
|
|
|
import useToggle from "../../utils/useToggle";
|
2022-01-12 16:01:54 +00:00
|
|
|
|
|
|
|
import "./search-dropdown.css";
|
|
|
|
|
2022-01-24 14:17:32 +00:00
|
|
|
export type SearchForm = {
|
|
|
|
name?: string;
|
|
|
|
resource?: string;
|
|
|
|
scope?: string;
|
|
|
|
type?: string;
|
2022-12-07 09:03:38 +00:00
|
|
|
uri?: string;
|
|
|
|
owner?: string;
|
2022-01-24 14:17:32 +00:00
|
|
|
};
|
|
|
|
|
2022-01-12 16:01:54 +00:00
|
|
|
type SearchDropdownProps = {
|
2022-03-16 13:57:50 +00:00
|
|
|
types?: PolicyProviderRepresentation[] | PolicyProviderRepresentation[];
|
|
|
|
search: SearchForm;
|
2022-01-24 14:17:32 +00:00
|
|
|
onSearch: (form: SearchForm) => void;
|
2022-03-16 13:57:50 +00:00
|
|
|
isResource?: boolean;
|
2022-01-12 16:01:54 +00:00
|
|
|
};
|
|
|
|
|
2022-03-16 13:57:50 +00:00
|
|
|
export const SearchDropdown = ({
|
|
|
|
types,
|
|
|
|
search,
|
|
|
|
onSearch,
|
|
|
|
isResource = false,
|
|
|
|
}: SearchDropdownProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2022-01-24 14:17:32 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
control,
|
2022-03-16 13:57:50 +00:00
|
|
|
reset,
|
2022-01-24 14:17:32 +00:00
|
|
|
formState: { isDirty },
|
|
|
|
handleSubmit,
|
|
|
|
} = useForm<SearchForm>({ mode: "onChange" });
|
2022-01-12 16:01:54 +00:00
|
|
|
|
|
|
|
const [open, toggle] = useToggle();
|
|
|
|
const [typeOpen, toggleType] = useToggle();
|
|
|
|
|
2022-01-24 14:17:32 +00:00
|
|
|
const submit = (form: SearchForm) => {
|
|
|
|
toggle();
|
|
|
|
onSearch(form);
|
|
|
|
};
|
|
|
|
|
2022-03-16 13:57:50 +00:00
|
|
|
useEffect(() => reset(search), [search]);
|
|
|
|
|
2022-12-07 09:03:38 +00:00
|
|
|
const typeOptions = (value?: string) => [
|
2022-01-24 14:17:32 +00:00
|
|
|
<SelectOption key="empty" value="">
|
|
|
|
{t("allTypes")}
|
|
|
|
</SelectOption>,
|
|
|
|
...(types || []).map((type) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={type.type === value}
|
|
|
|
key={type.type}
|
|
|
|
value={type.type}
|
|
|
|
>
|
|
|
|
{type.name}
|
|
|
|
</SelectOption>
|
|
|
|
)),
|
|
|
|
];
|
|
|
|
|
2022-01-12 16:01:54 +00:00
|
|
|
return (
|
|
|
|
<Dropdown
|
|
|
|
data-testid="searchdropdown_dorpdown"
|
|
|
|
className="pf-u-ml-md"
|
|
|
|
toggle={
|
|
|
|
<DropdownToggle
|
|
|
|
onToggle={toggle}
|
|
|
|
className="keycloak__client_authentication__searchdropdown"
|
|
|
|
>
|
|
|
|
{t("searchForPermission")}
|
|
|
|
</DropdownToggle>
|
|
|
|
}
|
|
|
|
isOpen={open}
|
|
|
|
>
|
|
|
|
<Form
|
|
|
|
isHorizontal
|
|
|
|
className="keycloak__client_authentication__searchdropdown_form"
|
2022-01-24 14:17:32 +00:00
|
|
|
onSubmit={handleSubmit(submit)}
|
2022-01-12 16:01:54 +00:00
|
|
|
>
|
2023-09-14 09:01:15 +00:00
|
|
|
<FormGroup label={t("name")} fieldId="name">
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-01-12 16:01:54 +00:00
|
|
|
id="name"
|
|
|
|
data-testid="searchdropdown_name"
|
2022-12-07 09:03:38 +00:00
|
|
|
{...register("name")}
|
2022-01-12 16:01:54 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
2022-03-16 13:57:50 +00:00
|
|
|
{isResource && (
|
|
|
|
<>
|
2023-09-14 09:01:15 +00:00
|
|
|
<FormGroup label={t("type")} fieldId="type">
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-03-16 13:57:50 +00:00
|
|
|
id="type"
|
|
|
|
data-testid="searchdropdown_type"
|
2022-12-07 09:03:38 +00:00
|
|
|
{...register("type")}
|
2022-03-16 13:57:50 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup label={t("uris")} fieldId="uri">
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-03-16 13:57:50 +00:00
|
|
|
id="uri"
|
|
|
|
data-testid="searchdropdown_uri"
|
2022-12-07 09:03:38 +00:00
|
|
|
{...register("uri")}
|
2022-03-16 13:57:50 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup label={t("owner")} fieldId="owner">
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-03-16 13:57:50 +00:00
|
|
|
id="owner"
|
|
|
|
data-testid="searchdropdown_owner"
|
2022-12-07 09:03:38 +00:00
|
|
|
{...register("owner")}
|
2022-03-16 13:57:50 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{!isResource && (
|
|
|
|
<FormGroup label={t("resource")} fieldId="resource">
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-03-16 13:57:50 +00:00
|
|
|
id="resource"
|
|
|
|
data-testid="searchdropdown_resource"
|
2022-12-07 09:03:38 +00:00
|
|
|
{...register("resource")}
|
2022-03-16 13:57:50 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
2022-01-24 14:17:32 +00:00
|
|
|
<FormGroup label={t("scope")} fieldId="scope">
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2022-01-24 14:17:32 +00:00
|
|
|
id="scope"
|
|
|
|
data-testid="searchdropdown_scope"
|
2022-12-07 09:03:38 +00:00
|
|
|
{...register("scope")}
|
2022-01-24 14:17:32 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
2022-03-16 13:57:50 +00:00
|
|
|
{!isResource && (
|
2023-09-14 09:01:15 +00:00
|
|
|
<FormGroup label={t("type")} fieldId="type">
|
2022-03-16 13:57:50 +00:00
|
|
|
<Controller
|
|
|
|
name="type"
|
|
|
|
defaultValue=""
|
|
|
|
control={control}
|
2022-12-07 09:03:38 +00:00
|
|
|
render={({ field }) => (
|
2022-03-16 13:57:50 +00:00
|
|
|
<Select
|
|
|
|
toggleId="type"
|
|
|
|
onToggle={toggleType}
|
|
|
|
onSelect={(event, value) => {
|
|
|
|
event.stopPropagation();
|
2022-12-07 09:03:38 +00:00
|
|
|
field.onChange(value);
|
2022-03-16 13:57:50 +00:00
|
|
|
toggleType();
|
|
|
|
}}
|
2022-12-07 09:03:38 +00:00
|
|
|
selections={field.value || t("allTypes")}
|
2022-03-16 13:57:50 +00:00
|
|
|
variant={SelectVariant.single}
|
2023-09-14 09:01:15 +00:00
|
|
|
aria-label={t("type")}
|
2022-03-16 13:57:50 +00:00
|
|
|
isOpen={typeOpen}
|
|
|
|
>
|
2022-12-07 09:03:38 +00:00
|
|
|
{typeOptions(field.value)}
|
2022-03-16 13:57:50 +00:00
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
2022-01-24 14:17:32 +00:00
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
data-testid="search-btn"
|
|
|
|
isDisabled={!isDirty}
|
|
|
|
>
|
2023-09-14 09:01:15 +00:00
|
|
|
{t("search")}
|
2022-01-24 14:17:32 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
data-testid="revert-btn"
|
|
|
|
onClick={() => onSearch({})}
|
|
|
|
>
|
2023-09-14 09:01:15 +00:00
|
|
|
{t("clear")}
|
2022-01-24 14:17:32 +00:00
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
2022-01-12 16:01:54 +00:00
|
|
|
</Form>
|
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
};
|