2022-01-12 16:01:54 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
|
|
|
import { Select, SelectOption, SelectVariant } from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import type PolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyRepresentation";
|
|
|
|
import type { Clients } from "@keycloak/keycloak-admin-client/lib/resources/clients";
|
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
|
|
|
|
|
|
|
type ResourcesPolicySelectProps = {
|
|
|
|
name: string;
|
|
|
|
clientId: string;
|
|
|
|
searchFunction: keyof Pick<Clients, "listPolicies" | "listResources">;
|
2022-02-10 08:23:23 +00:00
|
|
|
variant?: SelectVariant;
|
2022-03-14 10:17:16 +00:00
|
|
|
preSelected?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Policies = {
|
|
|
|
id?: string;
|
|
|
|
name?: string;
|
2022-01-12 16:01:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const ResourcesPolicySelect = ({
|
|
|
|
name,
|
|
|
|
searchFunction,
|
|
|
|
clientId,
|
2022-02-10 08:23:23 +00:00
|
|
|
variant = SelectVariant.typeaheadMulti,
|
2022-03-14 10:17:16 +00:00
|
|
|
preSelected,
|
2022-01-12 16:01:54 +00:00
|
|
|
}: ResourcesPolicySelectProps) => {
|
|
|
|
const { t } = useTranslation("clients");
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
|
|
|
|
const { control } = useFormContext<PolicyRepresentation>();
|
2022-03-14 10:17:16 +00:00
|
|
|
const [items, setItems] = useState<Policies[]>([]);
|
2022-01-12 16:01:54 +00:00
|
|
|
const [search, setSearch] = useState("");
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
async () =>
|
|
|
|
(
|
|
|
|
await adminClient.clients[searchFunction](
|
|
|
|
Object.assign(
|
|
|
|
{ id: clientId, first: 0, max: 10 },
|
|
|
|
search === "" ? null : { name: search }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).map((p) => ({
|
|
|
|
id: "_id" in p ? p._id : "id" in p ? p.id : undefined,
|
|
|
|
name: p.name,
|
|
|
|
})),
|
2022-03-14 10:17:16 +00:00
|
|
|
(policies) => setItems(policies),
|
2022-01-12 16:01:54 +00:00
|
|
|
[search]
|
|
|
|
);
|
|
|
|
|
2022-03-14 10:17:16 +00:00
|
|
|
const toSelectOptions = () =>
|
|
|
|
items.map((p) => (
|
|
|
|
<SelectOption key={p.id} value={p.id}>
|
|
|
|
{p.name}
|
|
|
|
</SelectOption>
|
|
|
|
));
|
|
|
|
|
2022-01-12 16:01:54 +00:00
|
|
|
return (
|
|
|
|
<Controller
|
|
|
|
name={name}
|
2022-03-14 10:17:16 +00:00
|
|
|
defaultValue={preSelected ? [preSelected] : []}
|
2022-01-12 16:01:54 +00:00
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId={name}
|
2022-02-10 08:23:23 +00:00
|
|
|
variant={variant}
|
2022-01-12 16:01:54 +00:00
|
|
|
onToggle={setOpen}
|
|
|
|
onFilter={(_, filter) => {
|
|
|
|
setSearch(filter);
|
2022-03-14 10:17:16 +00:00
|
|
|
return toSelectOptions();
|
2022-01-12 16:01:54 +00:00
|
|
|
}}
|
|
|
|
onClear={() => {
|
|
|
|
onChange([]);
|
|
|
|
setSearch("");
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
onSelect={(_, selectedValue) => {
|
|
|
|
const option = selectedValue.toString();
|
|
|
|
const changedValue = value.find((p: string) => p === option)
|
|
|
|
? value.filter((p: string) => p !== option)
|
|
|
|
: [...value, option];
|
|
|
|
onChange(changedValue);
|
|
|
|
setSearch("");
|
|
|
|
}}
|
|
|
|
isOpen={open}
|
2022-02-10 08:23:23 +00:00
|
|
|
aria-labelledby={t(name)}
|
2022-03-14 10:17:16 +00:00
|
|
|
isDisabled={!!preSelected}
|
2022-01-12 16:01:54 +00:00
|
|
|
>
|
2022-03-14 10:17:16 +00:00
|
|
|
{toSelectOptions()}
|
2022-01-12 16:01:54 +00:00
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|