Make always the default (#3944)

This commit is contained in:
Erik Jan de Wit 2022-12-07 04:05:32 -05:00 committed by GitHub
parent f000e7de69
commit d9e44202d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
import { useState } from "react"; import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation";
import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
import { import {
Divider, Divider,
FormGroup, FormGroup,
@ -8,20 +9,20 @@ import {
SelectVariant, SelectVariant,
Switch, Switch,
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import { useTranslation } from "react-i18next"; import { isEqual } from "lodash-es";
import { HelpItem } from "../../../components/help-enabler/HelpItem"; import { useState } from "react";
import { Controller, useFormContext, useWatch } from "react-hook-form"; import { Controller, useFormContext, useWatch } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useParams } from "react-router-dom";
import { FormAccess } from "../../../components/form-access/FormAccess"; import { FormAccess } from "../../../components/form-access/FormAccess";
import { HelpItem } from "../../../components/help-enabler/HelpItem";
import { KeycloakSpinner } from "../../../components/keycloak-spinner/KeycloakSpinner";
import { KeycloakTextInput } from "../../../components/keycloak-text-input/KeycloakTextInput"; import { KeycloakTextInput } from "../../../components/keycloak-text-input/KeycloakTextInput";
import { useAdminClient, useFetch } from "../../../context/auth/AdminClient"; import { useAdminClient, useFetch } from "../../../context/auth/AdminClient";
import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation"; import { USERNAME_EMAIL } from "../../NewAttributeSettings";
import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
import type { AttributeParams } from "../../routes/Attribute"; import type { AttributeParams } from "../../routes/Attribute";
import { useParams } from "react-router-dom";
import { isEqual } from "lodash-es";
import "../../realm-settings-section.css"; import "../../realm-settings-section.css";
import { USERNAME_EMAIL } from "../../NewAttributeSettings";
const REQUIRED_FOR = [ const REQUIRED_FOR = [
{ label: "requiredForLabel.both", value: ["admin", "user"] }, { label: "requiredForLabel.both", value: ["admin", "user"] },
@ -64,6 +65,9 @@ export const AttributeGeneralSettings = () => {
useFetch(() => adminClient.clientScopes.find(), setClientScopes, []); useFetch(() => adminClient.clientScopes.find(), setClientScopes, []);
useFetch(() => adminClient.users.getProfile(), setConfig, []); useFetch(() => adminClient.users.getProfile(), setConfig, []);
if (!clientScopes) {
return <KeycloakSpinner />;
}
return ( return (
<FormAccess role="manage-realm" isHorizontal> <FormAccess role="manage-realm" isHorizontal>
<FormGroup <FormGroup
@ -163,14 +167,14 @@ export const AttributeGeneralSettings = () => {
<Radio <Radio
id="always" id="always"
data-testid="always" data-testid="always"
isChecked={selectedScopes.length === clientScopes?.length} isChecked={selectedScopes.length === clientScopes.length}
name="enabledWhen" name="enabledWhen"
label={t("always")} label={t("always")}
onChange={(value) => { onChange={(value) => {
if (value) { if (value) {
form.setValue( form.setValue(
"selector.scopes", "selector.scopes",
clientScopes?.map((s) => s.name) clientScopes.map((s) => s.name)
); );
} else { } else {
form.setValue("selector.scopes", []); form.setValue("selector.scopes", []);
@ -181,7 +185,7 @@ export const AttributeGeneralSettings = () => {
<Radio <Radio
id="scopesAsRequested" id="scopesAsRequested"
data-testid="scopesAsRequested" data-testid="scopesAsRequested"
isChecked={selectedScopes.length !== clientScopes?.length} isChecked={selectedScopes.length !== clientScopes.length}
name="enabledWhen" name="enabledWhen"
label={t("scopesAsRequested")} label={t("scopesAsRequested")}
onChange={(value) => { onChange={(value) => {
@ -190,7 +194,7 @@ export const AttributeGeneralSettings = () => {
} else { } else {
form.setValue( form.setValue(
"selector.scopes", "selector.scopes",
clientScopes?.map((s) => s.name) clientScopes.map((s) => s.name)
); );
} }
}} }}
@ -201,7 +205,7 @@ export const AttributeGeneralSettings = () => {
<Controller <Controller
name="selector.scopes" name="selector.scopes"
control={form.control} control={form.control}
defaultValue={[]} defaultValue={clientScopes.map((s) => s.name)}
render={({ onChange, value }) => ( render={({ onChange, value }) => (
<Select <Select
name="scopes" name="scopes"
@ -233,10 +237,10 @@ export const AttributeGeneralSettings = () => {
onChange([]); onChange([]);
}} }}
isOpen={selectEnabledWhenOpen} isOpen={selectEnabledWhenOpen}
isDisabled={selectedScopes.length === clientScopes?.length} isDisabled={selectedScopes.length === clientScopes.length}
aria-labelledby={"scope"} aria-labelledby={"scope"}
> >
{clientScopes?.map((option) => ( {clientScopes.map((option) => (
<SelectOption key={option.name} value={option.name} /> <SelectOption key={option.name} value={option.name} />
))} ))}
</Select> </Select>
@ -313,14 +317,14 @@ export const AttributeGeneralSettings = () => {
<Radio <Radio
id="requiredAlways" id="requiredAlways"
data-testid="requiredAlways" data-testid="requiredAlways"
isChecked={requiredScopes.length === clientScopes?.length} isChecked={requiredScopes.length === clientScopes.length}
name="requiredWhen" name="requiredWhen"
label={t("always")} label={t("always")}
onChange={(value) => { onChange={(value) => {
if (value) { if (value) {
form.setValue( form.setValue(
"required.scopes", "required.scopes",
clientScopes?.map((s) => s.name) clientScopes.map((s) => s.name)
); );
} else { } else {
form.setValue("required.scopes", []); form.setValue("required.scopes", []);
@ -331,7 +335,7 @@ export const AttributeGeneralSettings = () => {
<Radio <Radio
id="requiredScopesAsRequested" id="requiredScopesAsRequested"
data-testid="requiredScopesAsRequested" data-testid="requiredScopesAsRequested"
isChecked={requiredScopes.length !== clientScopes?.length} isChecked={requiredScopes.length !== clientScopes.length}
name="requiredWhen" name="requiredWhen"
label={t("scopesAsRequested")} label={t("scopesAsRequested")}
onChange={(value) => { onChange={(value) => {
@ -340,7 +344,7 @@ export const AttributeGeneralSettings = () => {
} else { } else {
form.setValue( form.setValue(
"required.scopes", "required.scopes",
clientScopes?.map((s) => s.name) clientScopes.map((s) => s.name)
); );
} }
}} }}
@ -382,12 +386,10 @@ export const AttributeGeneralSettings = () => {
onChange([]); onChange([]);
}} }}
isOpen={selectRequiredForOpen} isOpen={selectRequiredForOpen}
isDisabled={ isDisabled={requiredScopes.length === clientScopes.length}
requiredScopes.length === clientScopes?.length
}
aria-labelledby={"scope"} aria-labelledby={"scope"}
> >
{clientScopes?.map((option) => ( {clientScopes.map((option) => (
<SelectOption key={option.name} value={option.name} /> <SelectOption key={option.name} value={option.name} />
))} ))}
</Select> </Select>