Changed the load to also include the existing values (#2507)

This commit is contained in:
Erik Jan de Wit 2022-04-27 17:30:13 +02:00 committed by GitHub
parent af0b704cf8
commit 593d061224
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 21 deletions

View file

@ -296,8 +296,8 @@ export default function PermissionDetails() {
> >
<ResourcesPolicySelect <ResourcesPolicySelect
name="resources" name="resources"
searchFunction="listResources"
clientId={id} clientId={id}
permissionId={permissionId}
preSelected={ preSelected={
permissionType === "scope" ? undefined : selectedId permissionType === "scope" ? undefined : selectedId
} }
@ -343,8 +343,8 @@ export default function PermissionDetails() {
> >
<ResourcesPolicySelect <ResourcesPolicySelect
name="policies" name="policies"
searchFunction="listPolicies"
clientId={id} clientId={id}
permissionId={permissionId}
/> />
</FormGroup> </FormGroup>
<FormGroup <FormGroup

View file

@ -3,14 +3,17 @@ import { useTranslation } from "react-i18next";
import { Controller, useFormContext } from "react-hook-form"; import { Controller, useFormContext } from "react-hook-form";
import { Select, SelectOption, SelectVariant } from "@patternfly/react-core"; import { Select, SelectOption, SelectVariant } from "@patternfly/react-core";
import type ResourceRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceRepresentation";
import type PolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyRepresentation"; import type PolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyRepresentation";
import type { Clients } from "@keycloak/keycloak-admin-client/lib/resources/clients"; import type { Clients } from "@keycloak/keycloak-admin-client/lib/resources/clients";
import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
type Type = "resources" | "policies";
type ResourcesPolicySelectProps = { type ResourcesPolicySelectProps = {
name: keyof PolicyRepresentation; name: Type;
clientId: string; clientId: string;
searchFunction: keyof Pick<Clients, "listPolicies" | "listResources">; permissionId?: string;
variant?: SelectVariant; variant?: SelectVariant;
preSelected?: string; preSelected?: string;
isRequired?: boolean; isRequired?: boolean;
@ -21,10 +24,31 @@ type Policies = {
name?: string; name?: string;
}; };
type TypeMapping = {
[key in Type]: {
searchFunction: keyof Pick<Clients, "listPolicies" | "listResources">;
fetchFunction: keyof Pick<
Clients,
"getAssociatedPolicies" | "getAssociatedResources"
>;
};
};
const typeMapping: TypeMapping = {
resources: {
searchFunction: "listResources",
fetchFunction: "getAssociatedResources",
},
policies: {
searchFunction: "listPolicies",
fetchFunction: "getAssociatedPolicies",
},
};
export const ResourcesPolicySelect = ({ export const ResourcesPolicySelect = ({
name, name,
searchFunction,
clientId, clientId,
permissionId,
variant = SelectVariant.typeaheadMulti, variant = SelectVariant.typeaheadMulti,
preSelected, preSelected,
isRequired = false, isRequired = false,
@ -40,20 +64,40 @@ export const ResourcesPolicySelect = ({
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const functions = typeMapping[name];
const convert = (
p: PolicyRepresentation | ResourceRepresentation
): Policies => ({
id: "_id" in p ? p._id : "id" in p ? p.id : undefined,
name: p.name,
});
useFetch( useFetch(
async () => async () =>
( (
await adminClient.clients[searchFunction]( await Promise.all([
Object.assign( adminClient.clients[functions.searchFunction](
{ id: clientId, first: 0, max: 10 }, Object.assign(
search === "" ? null : { name: search } { id: clientId, first: 0, max: 10 },
) search === "" ? null : { name: search }
) )
).map((p) => ({ ),
id: "_id" in p ? p._id : "id" in p ? p.id : undefined, permissionId
name: p.name, ? adminClient.clients[functions.fetchFunction]({
})), id: clientId,
(policies) => setItems(policies), permissionId,
})
: Promise.resolve([]),
])
)
.flat()
.map(convert)
.filter(
({ id }, index, self) =>
index === self.findIndex(({ id: otherId }) => id === otherId)
),
setItems,
[search] [search]
); );

View file

@ -24,11 +24,7 @@ export const Aggregate = () => {
/> />
} }
> >
<ResourcesPolicySelect <ResourcesPolicySelect name="policies" clientId={id} />
name="policies"
searchFunction="listPolicies"
clientId={id}
/>
</FormGroup> </FormGroup>
<DecisionStrategySelect helpLabel="policyDecisionStagey" /> <DecisionStrategySelect helpLabel="policyDecisionStagey" />
</> </>