2022-08-03 12:12:07 +00:00
|
|
|
import { useRef, useState } from "react";
|
2022-02-10 08:23:23 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
|
|
|
import { Select, SelectOption, SelectVariant } from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import type ScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/scopeRepresentation";
|
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
|
|
|
|
|
|
|
type ScopeSelectProps = {
|
|
|
|
clientId: string;
|
|
|
|
resourceId?: string;
|
2022-03-14 10:17:16 +00:00
|
|
|
preSelected?: string;
|
2022-02-10 08:23:23 +00:00
|
|
|
};
|
|
|
|
|
2022-03-14 10:17:16 +00:00
|
|
|
export const ScopeSelect = ({
|
|
|
|
clientId,
|
|
|
|
resourceId,
|
|
|
|
preSelected,
|
|
|
|
}: ScopeSelectProps) => {
|
2022-02-10 08:23:23 +00:00
|
|
|
const { t } = useTranslation("clients");
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2022-02-10 08:23:23 +00:00
|
|
|
|
2022-04-08 12:37:31 +00:00
|
|
|
const {
|
|
|
|
control,
|
|
|
|
setValue,
|
|
|
|
formState: { errors },
|
|
|
|
} = useFormContext();
|
2022-02-10 08:23:23 +00:00
|
|
|
|
|
|
|
const [scopes, setScopes] = useState<ScopeRepresentation[]>([]);
|
|
|
|
const [search, setSearch] = useState("");
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const firstUpdate = useRef(true);
|
|
|
|
|
|
|
|
const toSelectOptions = (scopes: ScopeRepresentation[]) =>
|
|
|
|
scopes.map((scope) => (
|
|
|
|
<SelectOption key={scope.id} value={scope.id}>
|
|
|
|
{scope.name}
|
|
|
|
</SelectOption>
|
|
|
|
));
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
async () => {
|
|
|
|
if (!resourceId) {
|
|
|
|
return adminClient.clients.listAllScopes(
|
|
|
|
Object.assign(
|
|
|
|
{ id: clientId, first: 0, max: 10, deep: false },
|
|
|
|
search === "" ? null : { name: search }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resourceId && !firstUpdate.current) {
|
|
|
|
setValue("scopes", []);
|
|
|
|
}
|
|
|
|
|
|
|
|
firstUpdate.current = false;
|
|
|
|
return adminClient.clients.listScopesByResource({
|
|
|
|
id: clientId,
|
|
|
|
resourceName: resourceId,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setScopes,
|
|
|
|
[resourceId, search]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Controller
|
|
|
|
name="scopes"
|
2022-03-14 10:17:16 +00:00
|
|
|
defaultValue={preSelected ? [preSelected] : []}
|
2022-02-10 08:23:23 +00:00
|
|
|
control={control}
|
|
|
|
rules={{ validate: (value) => value.length > 0 }}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="scopes"
|
|
|
|
variant={SelectVariant.typeaheadMulti}
|
|
|
|
onToggle={setOpen}
|
|
|
|
onFilter={(_, filter) => {
|
|
|
|
setSearch(filter);
|
|
|
|
return toSelectOptions(scopes);
|
|
|
|
}}
|
|
|
|
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}
|
|
|
|
aria-labelledby={t("scopes")}
|
|
|
|
validated={errors.scopes ? "error" : "default"}
|
2022-03-14 10:17:16 +00:00
|
|
|
isDisabled={!!preSelected}
|
2022-06-27 08:45:15 +00:00
|
|
|
typeAheadAriaLabel={t("scopes")}
|
2022-02-10 08:23:23 +00:00
|
|
|
>
|
|
|
|
{toSelectOptions(scopes)}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|