Do not nest components in NewClientPolicy (#28549)

Signed-off-by: Jon Koops <jonkoops@gmail.com>
This commit is contained in:
Jon Koops 2024-04-09 15:58:26 +02:00 committed by GitHub
parent 1bb5e14134
commit d8a8b3f1be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 60 additions and 84 deletions

View file

@ -48,9 +48,9 @@ import { toEditClientPolicyCondition } from "./routes/EditCondition";
import "./realm-settings-section.css"; import "./realm-settings-section.css";
type NewClientPolicyForm = Required<ClientPolicyRepresentation>; type FormFields = Required<ClientPolicyRepresentation>;
const defaultValues: NewClientPolicyForm = { const defaultValues: FormFields = {
name: "", name: "",
description: "", description: "",
conditions: [], conditions: [],
@ -63,7 +63,7 @@ type PolicyDetailAttributes = {
name: string; name: string;
}; };
export default function NewClientPolicyForm() { export default function NewClientPolicy() {
const { t } = useTranslation(); const { t } = useTranslation();
const { realm } = useRealm(); const { realm } = useRealm();
const { addAlert, addError } = useAlerts(); const { addAlert, addError } = useAlerts();
@ -90,7 +90,7 @@ export default function NewClientPolicyForm() {
const { policyName } = useParams<EditClientPolicyParams>(); const { policyName } = useParams<EditClientPolicyParams>();
const navigate = useNavigate(); const navigate = useNavigate();
const form = useForm<NewClientPolicyForm>({ const form = useForm<FormFields>({
mode: "onChange", mode: "onChange",
defaultValues, defaultValues,
}); });
@ -98,75 +98,6 @@ export default function NewClientPolicyForm() {
const formValues = form.getValues(); const formValues = form.getValues();
type ClientPoliciesHeaderProps = {
onChange: (value: boolean) => void;
value: boolean;
save: () => void;
realmName: string;
};
const ClientPoliciesHeader = ({
save,
onChange,
value,
}: ClientPoliciesHeaderProps) => {
const { t } = useTranslation();
const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({
titleKey: "disablePolicyConfirmTitle",
messageKey: "disablePolicyConfirm",
continueButtonLabel: "disable",
onConfirm: () => {
onChange(!value);
save();
},
});
if (!policies) {
return <KeycloakSpinner />;
}
return (
<>
<DisableConfirm />
<DeleteConfirm />
<ViewHeader
titleKey={
showAddConditionsAndProfilesForm || policyName
? policyName
: "createPolicy"
}
divider
dropdownItems={
showAddConditionsAndProfilesForm || policyName
? [
<DropdownItem
key="delete"
value="delete"
onClick={() => {
toggleDeleteDialog();
}}
data-testid="deleteClientPolicyDropdown"
>
{t("deleteClientPolicy")}
</DropdownItem>,
]
: undefined
}
isEnabled={value}
onToggle={(value) => {
if (!value) {
toggleDisableDialog();
} else {
onChange(value);
save();
}
}}
/>
</>
);
};
useFetch( useFetch(
async () => { async () => {
const [policies, profiles] = await Promise.all([ const [policies, profiles] = await Promise.all([
@ -422,6 +353,20 @@ export default function NewClientPolicyForm() {
} }
}; };
const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({
titleKey: "disablePolicyConfirmTitle",
messageKey: "disablePolicyConfirm",
continueButtonLabel: "disable",
onConfirm: () => {
form.setValue("enabled", !form.getValues().enabled);
save();
},
});
if (!policies) {
return <KeycloakSpinner />;
}
return ( return (
<> <>
<DeleteConditionConfirm /> <DeleteConditionConfirm />
@ -439,12 +384,43 @@ export default function NewClientPolicyForm() {
defaultValue={true} defaultValue={true}
control={form.control} control={form.control}
render={({ field }) => ( render={({ field }) => (
<ClientPoliciesHeader <>
value={field.value} <DisableConfirm />
onChange={field.onChange} <DeleteConfirm />
realmName={realm} <ViewHeader
save={save} titleKey={
/> showAddConditionsAndProfilesForm || policyName
? policyName
: "createPolicy"
}
divider
dropdownItems={
showAddConditionsAndProfilesForm || policyName
? [
<DropdownItem
key="delete"
value="delete"
onClick={() => {
toggleDeleteDialog();
}}
data-testid="deleteClientPolicyDropdown"
>
{t("deleteClientPolicy")}
</DropdownItem>,
]
: undefined
}
isEnabled={field.value}
onToggle={(value) => {
if (!value) {
toggleDisableDialog();
} else {
field.onChange(value);
save();
}
}}
/>
</>
)} )}
/> />
<PageSection variant="light"> <PageSection variant="light">
@ -461,7 +437,7 @@ export default function NewClientPolicyForm() {
rules={{ rules={{
required: { value: true, message: t("required") }, required: { value: true, message: t("required") },
validate: (value) => validate: (value) =>
policies?.some((policy) => policy.name === value) policies.some((policy) => policy.name === value)
? t("createClientProfileNameHelperText").toString() ? t("createClientProfileNameHelperText").toString()
: true, : true,
}} }}

View file

@ -5,11 +5,11 @@ import type { AppRouteObject } from "../../routes";
export type AddClientPolicyParams = { realm: string }; export type AddClientPolicyParams = { realm: string };
const NewClientPolicyForm = lazy(() => import("../NewClientPolicyForm")); const NewClientPolicy = lazy(() => import("../NewClientPolicy"));
export const AddClientPolicyRoute: AppRouteObject = { export const AddClientPolicyRoute: AppRouteObject = {
path: "/:realm/realm-settings/client-policies/policies/add-client-policy", path: "/:realm/realm-settings/client-policies/policies/add-client-policy",
element: <NewClientPolicyForm />, element: <NewClientPolicy />,
breadcrumb: (t) => t("createPolicy"), breadcrumb: (t) => t("createPolicy"),
handle: { handle: {
access: "manage-clients", access: "manage-clients",

View file

@ -8,11 +8,11 @@ export type EditClientPolicyParams = {
policyName: string; policyName: string;
}; };
const NewClientPolicyForm = lazy(() => import("../NewClientPolicyForm")); const NewClientPolicy = lazy(() => import("../NewClientPolicy"));
export const EditClientPolicyRoute: AppRouteObject = { export const EditClientPolicyRoute: AppRouteObject = {
path: "/:realm/realm-settings/client-policies/:policyName/edit-policy", path: "/:realm/realm-settings/client-policies/:policyName/edit-policy",
element: <NewClientPolicyForm />, element: <NewClientPolicy />,
breadcrumb: (t) => t("policyDetails"), breadcrumb: (t) => t("policyDetails"),
handle: { handle: {
access: "manage-realm", access: "manage-realm",