2023-05-03 13:51:02 +00:00
|
|
|
import type ScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/scopeRepresentation";
|
2021-12-01 07:58:25 +00:00
|
|
|
import {
|
|
|
|
FormGroup,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
} from "@patternfly/react-core";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2023-03-07 09:29:40 +00:00
|
|
|
import { HelpItem } from "ui-shared";
|
2021-12-01 07:58:25 +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";
|
2023-05-03 13:51:02 +00:00
|
|
|
|
2021-12-01 07:58:25 +00:00
|
|
|
type Scope = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ScopePicker = ({ clientId }: { clientId: string }) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2021-12-01 07:58:25 +00:00
|
|
|
const { control } = useFormContext();
|
|
|
|
|
|
|
|
const [open, setOpen] = useState(false);
|
2022-09-21 14:49:25 +00:00
|
|
|
const [scopes, setScopes] = useState<ScopeRepresentation[]>();
|
2021-12-01 07:58:25 +00:00
|
|
|
const [search, setSearch] = useState("");
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => {
|
|
|
|
const params = {
|
|
|
|
id: clientId,
|
|
|
|
first: 0,
|
|
|
|
max: 20,
|
|
|
|
deep: false,
|
|
|
|
name: search,
|
|
|
|
};
|
|
|
|
return adminClient.clients.listAllScopes(params);
|
|
|
|
},
|
2022-09-21 14:49:25 +00:00
|
|
|
setScopes,
|
2023-07-11 14:03:21 +00:00
|
|
|
[search],
|
2021-12-01 07:58:25 +00:00
|
|
|
);
|
|
|
|
|
2022-09-21 14:49:25 +00:00
|
|
|
const renderScopes = (scopes?: ScopeRepresentation[]) =>
|
|
|
|
scopes?.map((option) => (
|
|
|
|
<SelectOption key={option.id} value={option}>
|
|
|
|
{option.name}
|
|
|
|
</SelectOption>
|
|
|
|
));
|
|
|
|
|
2021-12-01 07:58:25 +00:00
|
|
|
return (
|
|
|
|
<FormGroup
|
|
|
|
label={t("authorizationScopes")}
|
|
|
|
labelIcon={
|
2023-09-13 14:05:17 +00:00
|
|
|
<HelpItem helpText={t("clientScopesHelp")} fieldLabelId="scopes" />
|
2021-12-01 07:58:25 +00:00
|
|
|
}
|
|
|
|
fieldId="scopes"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="scopes"
|
|
|
|
defaultValue={[]}
|
|
|
|
control={control}
|
2023-01-18 10:12:32 +00:00
|
|
|
render={({ field }) => (
|
2021-12-01 07:58:25 +00:00
|
|
|
<Select
|
|
|
|
toggleId="scopes"
|
|
|
|
variant={SelectVariant.typeaheadMulti}
|
|
|
|
chipGroupProps={{
|
|
|
|
numChips: 3,
|
2023-09-14 09:01:15 +00:00
|
|
|
expandedText: t("hide"),
|
|
|
|
collapsedText: t("showRemaining"),
|
2021-12-01 07:58:25 +00:00
|
|
|
}}
|
2022-09-21 14:49:25 +00:00
|
|
|
onToggle={setOpen}
|
2021-12-01 07:58:25 +00:00
|
|
|
isOpen={open}
|
2023-01-18 10:12:32 +00:00
|
|
|
selections={field.value.map((o: Scope) => o.name)}
|
2021-12-01 07:58:25 +00:00
|
|
|
onFilter={(_, value) => {
|
|
|
|
setSearch(value);
|
2022-09-21 14:49:25 +00:00
|
|
|
return renderScopes(scopes);
|
2021-12-01 07:58:25 +00:00
|
|
|
}}
|
|
|
|
onSelect={(_, selectedValue) => {
|
|
|
|
const option =
|
|
|
|
typeof selectedValue === "string"
|
|
|
|
? selectedValue
|
|
|
|
: (selectedValue as Scope).name;
|
2023-01-18 10:12:32 +00:00
|
|
|
const changedValue = field.value.find(
|
2023-07-11 14:03:21 +00:00
|
|
|
(o: Scope) => o.name === option,
|
2023-01-18 10:12:32 +00:00
|
|
|
)
|
|
|
|
? field.value.filter((item: Scope) => item.name !== option)
|
|
|
|
: [...field.value, selectedValue];
|
|
|
|
field.onChange(changedValue);
|
2021-12-01 07:58:25 +00:00
|
|
|
}}
|
|
|
|
onClear={(event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
setSearch("");
|
2023-01-18 10:12:32 +00:00
|
|
|
field.onChange([]);
|
2021-12-01 07:58:25 +00:00
|
|
|
}}
|
2023-03-01 14:16:00 +00:00
|
|
|
typeAheadAriaLabel={t("authorizationScopes")}
|
2021-12-01 07:58:25 +00:00
|
|
|
>
|
2022-09-21 14:49:25 +00:00
|
|
|
{renderScopes(scopes)}
|
2021-12-01 07:58:25 +00:00
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
);
|
|
|
|
};
|