2023-05-03 13:51:02 +00:00
|
|
|
import type ScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/scopeRepresentation";
|
|
|
|
import { Select, SelectOption, SelectVariant } from "@patternfly/react-core";
|
2022-08-03 12:12:07 +00:00
|
|
|
import { useRef, useState } from "react";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-02-10 08:23:23 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../../admin-client";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../../utils/useFetch";
|
2022-02-10 08:23:23 +00:00
|
|
|
|
|
|
|
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) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2022-02-10 08:23:23 +00:00
|
|
|
|
2022-04-08 12:37:31 +00:00
|
|
|
const {
|
|
|
|
control,
|
2022-09-21 14:49:25 +00:00
|
|
|
getValues,
|
2022-04-08 12:37:31 +00:00
|
|
|
setValue,
|
|
|
|
formState: { errors },
|
|
|
|
} = useFormContext();
|
2022-02-10 08:23:23 +00:00
|
|
|
|
|
|
|
const [scopes, setScopes] = useState<ScopeRepresentation[]>([]);
|
2022-09-21 14:49:25 +00:00
|
|
|
const [selectedScopes, setSelectedScopes] = useState<ScopeRepresentation[]>(
|
2023-07-11 14:03:21 +00:00
|
|
|
[],
|
2022-09-21 14:49:25 +00:00
|
|
|
);
|
2022-02-10 08:23:23 +00:00
|
|
|
const [search, setSearch] = useState("");
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const firstUpdate = useRef(true);
|
|
|
|
|
2022-09-21 14:49:25 +00:00
|
|
|
const values: string[] | undefined = getValues("scopes");
|
|
|
|
|
2022-02-10 08:23:23 +00:00
|
|
|
const toSelectOptions = (scopes: ScopeRepresentation[]) =>
|
|
|
|
scopes.map((scope) => (
|
2022-09-21 14:49:25 +00:00
|
|
|
<SelectOption key={scope.id} value={scope}>
|
2022-02-10 08:23:23 +00:00
|
|
|
{scope.name}
|
|
|
|
</SelectOption>
|
|
|
|
));
|
|
|
|
|
|
|
|
useFetch(
|
2022-09-21 14:49:25 +00:00
|
|
|
async (): Promise<ScopeRepresentation[]> => {
|
2022-02-10 08:23:23 +00:00
|
|
|
if (!resourceId) {
|
|
|
|
return adminClient.clients.listAllScopes(
|
|
|
|
Object.assign(
|
2022-09-21 14:49:25 +00:00
|
|
|
{ id: clientId, deep: false },
|
2023-07-11 14:03:21 +00:00
|
|
|
search === "" ? null : { name: search },
|
|
|
|
),
|
2022-02-10 08:23:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resourceId && !firstUpdate.current) {
|
|
|
|
setValue("scopes", []);
|
|
|
|
}
|
|
|
|
|
|
|
|
firstUpdate.current = false;
|
|
|
|
return adminClient.clients.listScopesByResource({
|
|
|
|
id: clientId,
|
|
|
|
resourceName: resourceId,
|
|
|
|
});
|
|
|
|
},
|
2022-09-21 14:49:25 +00:00
|
|
|
(scopes) => {
|
|
|
|
setScopes(scopes);
|
|
|
|
if (!search)
|
|
|
|
setSelectedScopes(
|
2023-07-11 14:03:21 +00:00
|
|
|
scopes.filter((s: ScopeRepresentation) => values?.includes(s.id!)),
|
2022-09-21 14:49:25 +00:00
|
|
|
);
|
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
[resourceId, search],
|
2022-02-10 08:23:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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 }}
|
2023-01-18 10:12:32 +00:00
|
|
|
render={({ field }) => (
|
2022-02-10 08:23:23 +00:00
|
|
|
<Select
|
|
|
|
toggleId="scopes"
|
|
|
|
variant={SelectVariant.typeaheadMulti}
|
|
|
|
onToggle={setOpen}
|
|
|
|
onFilter={(_, filter) => {
|
|
|
|
setSearch(filter);
|
|
|
|
return toSelectOptions(scopes);
|
|
|
|
}}
|
|
|
|
onClear={() => {
|
2023-01-18 10:12:32 +00:00
|
|
|
field.onChange([]);
|
2022-02-10 08:23:23 +00:00
|
|
|
setSearch("");
|
|
|
|
}}
|
2022-09-21 14:49:25 +00:00
|
|
|
selections={selectedScopes.map((s) => s.name)}
|
2022-02-10 08:23:23 +00:00
|
|
|
onSelect={(_, selectedValue) => {
|
2022-09-21 14:49:25 +00:00
|
|
|
const option =
|
|
|
|
typeof selectedValue === "string"
|
|
|
|
? selectedScopes.find((s) => s.name === selectedValue)!
|
|
|
|
: (selectedValue as ScopeRepresentation);
|
|
|
|
const changedValue = selectedScopes.find((p) => p.id === option.id)
|
|
|
|
? selectedScopes.filter((p) => p.id !== option.id)
|
|
|
|
: [...selectedScopes, option];
|
|
|
|
|
2023-01-18 10:12:32 +00:00
|
|
|
field.onChange(changedValue.map((s) => s.id));
|
2022-09-21 14:49:25 +00:00
|
|
|
setSelectedScopes(changedValue);
|
2022-02-10 08:23:23 +00:00
|
|
|
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>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|