Added preselected / disabled on create permission (#2236)

This commit is contained in:
Erik Jan de Wit 2022-03-14 11:17:16 +01:00 committed by GitHub
parent 6539c8a0c1
commit d85fc18a63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 17 deletions

View file

@ -46,7 +46,7 @@ export default function PermissionDetails() {
const { register, control, reset, errors, handleSubmit } = form; const { register, control, reset, errors, handleSubmit } = form;
const history = useHistory(); const history = useHistory();
const { id, realm, permissionType, permissionId } = useParams< const { id, realm, permissionType, permissionId, selectedId } = useParams<
NewPermissionParams & PermissionDetailsParams NewPermissionParams & PermissionDetailsParams
>(); >();
@ -287,6 +287,9 @@ export default function PermissionDetails() {
name="resources" name="resources"
searchFunction="listResources" searchFunction="listResources"
clientId={id} clientId={id}
preSelected={
permissionType === "scope" ? undefined : selectedId
}
variant={ variant={
permissionType === "scope" permissionType === "scope"
? SelectVariant.typeahead ? SelectVariant.typeahead
@ -309,7 +312,11 @@ export default function PermissionDetails() {
validated={errors.scopes ? "error" : "default"} validated={errors.scopes ? "error" : "default"}
isRequired isRequired
> >
<ScopeSelect clientId={id} resourceId={resourcesIds?.[0]} /> <ScopeSelect
clientId={id}
resourceId={resourcesIds?.[0]}
preSelected={selectedId}
/>
</FormGroup> </FormGroup>
)} )}
<FormGroup <FormGroup

View file

@ -218,6 +218,7 @@ export const AuthorizationResources = ({ clientId }: ResourcesProps) => {
realm, realm,
id: clientId, id: clientId,
permissionType: "resource", permissionType: "resource",
selectedId: resource._id,
})} })}
/> />
)} )}

View file

@ -12,6 +12,12 @@ type ResourcesPolicySelectProps = {
clientId: string; clientId: string;
searchFunction: keyof Pick<Clients, "listPolicies" | "listResources">; searchFunction: keyof Pick<Clients, "listPolicies" | "listResources">;
variant?: SelectVariant; variant?: SelectVariant;
preSelected?: string;
};
type Policies = {
id?: string;
name?: string;
}; };
export const ResourcesPolicySelect = ({ export const ResourcesPolicySelect = ({
@ -19,12 +25,13 @@ export const ResourcesPolicySelect = ({
searchFunction, searchFunction,
clientId, clientId,
variant = SelectVariant.typeaheadMulti, variant = SelectVariant.typeaheadMulti,
preSelected,
}: ResourcesPolicySelectProps) => { }: ResourcesPolicySelectProps) => {
const { t } = useTranslation("clients"); const { t } = useTranslation("clients");
const adminClient = useAdminClient(); const adminClient = useAdminClient();
const { control } = useFormContext<PolicyRepresentation>(); const { control } = useFormContext<PolicyRepresentation>();
const [items, setItems] = useState<JSX.Element[]>([]); const [items, setItems] = useState<Policies[]>([]);
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
@ -41,21 +48,21 @@ export const ResourcesPolicySelect = ({
id: "_id" in p ? p._id : "id" in p ? p.id : undefined, id: "_id" in p ? p._id : "id" in p ? p.id : undefined,
name: p.name, name: p.name,
})), })),
(policies) => (policies) => setItems(policies),
setItems(
policies.map((p) => (
<SelectOption key={p.id} value={p.id}>
{p.name}
</SelectOption>
))
),
[search] [search]
); );
const toSelectOptions = () =>
items.map((p) => (
<SelectOption key={p.id} value={p.id}>
{p.name}
</SelectOption>
));
return ( return (
<Controller <Controller
name={name} name={name}
defaultValue={[]} defaultValue={preSelected ? [preSelected] : []}
control={control} control={control}
render={({ onChange, value }) => ( render={({ onChange, value }) => (
<Select <Select
@ -64,7 +71,7 @@ export const ResourcesPolicySelect = ({
onToggle={setOpen} onToggle={setOpen}
onFilter={(_, filter) => { onFilter={(_, filter) => {
setSearch(filter); setSearch(filter);
return items; return toSelectOptions();
}} }}
onClear={() => { onClear={() => {
onChange([]); onChange([]);
@ -81,8 +88,9 @@ export const ResourcesPolicySelect = ({
}} }}
isOpen={open} isOpen={open}
aria-labelledby={t(name)} aria-labelledby={t(name)}
isDisabled={!!preSelected}
> >
{items} {toSelectOptions()}
</Select> </Select>
)} )}
/> />

View file

@ -9,9 +9,14 @@ import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
type ScopeSelectProps = { type ScopeSelectProps = {
clientId: string; clientId: string;
resourceId?: string; resourceId?: string;
preSelected?: string;
}; };
export const ScopeSelect = ({ clientId, resourceId }: ScopeSelectProps) => { export const ScopeSelect = ({
clientId,
resourceId,
preSelected,
}: ScopeSelectProps) => {
const { t } = useTranslation("clients"); const { t } = useTranslation("clients");
const adminClient = useAdminClient(); const adminClient = useAdminClient();
@ -57,7 +62,7 @@ export const ScopeSelect = ({ clientId, resourceId }: ScopeSelectProps) => {
return ( return (
<Controller <Controller
name="scopes" name="scopes"
defaultValue={[]} defaultValue={preSelected ? [preSelected] : []}
control={control} control={control}
rules={{ validate: (value) => value.length > 0 }} rules={{ validate: (value) => value.length > 0 }}
render={({ onChange, value }) => ( render={({ onChange, value }) => (
@ -85,6 +90,7 @@ export const ScopeSelect = ({ clientId, resourceId }: ScopeSelectProps) => {
isOpen={open} isOpen={open}
aria-labelledby={t("scopes")} aria-labelledby={t("scopes")}
validated={errors.scopes ? "error" : "default"} validated={errors.scopes ? "error" : "default"}
isDisabled={!!preSelected}
> >
{toSelectOptions(scopes)} {toSelectOptions(scopes)}
</Select> </Select>

View file

@ -208,6 +208,7 @@ export const AuthorizationScopes = ({ clientId }: ScopesProps) => {
realm, realm,
id: clientId, id: clientId,
permissionType: "scope", permissionType: "scope",
selectedId: scope.id,
})} })}
/> />
)} )}

View file

@ -9,10 +9,11 @@ export type NewPermissionParams = {
realm: string; realm: string;
id: string; id: string;
permissionType: PermissionType; permissionType: PermissionType;
selectedId?: string;
}; };
export const NewPermissionRoute: RouteDef = { export const NewPermissionRoute: RouteDef = {
path: "/:realm/clients/:id/authorization/permission/new/:permissionType", path: "/:realm/clients/:id/authorization/permission/new/:permissionType/:selectedId?",
component: lazy(() => import("../authorization/PermissionDetails")), component: lazy(() => import("../authorization/PermissionDetails")),
breadcrumb: (t) => t("clients:createPermission"), breadcrumb: (t) => t("clients:createPermission"),
access: "manage-clients", access: "manage-clients",