Added confirm dialog on disable (#2238)

This commit is contained in:
Erik Jan de Wit 2022-03-14 11:16:43 +01:00 committed by GitHub
parent 7c4509e9bf
commit 05f321fd06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -24,13 +24,15 @@ import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
import { useAlerts } from "../components/alert/Alerts"; import { useAlerts } from "../components/alert/Alerts";
import "./realm-settings-section.css";
import { useRealm } from "../context/realm-context/RealmContext"; import { useRealm } from "../context/realm-context/RealmContext";
import { toAddClientPolicy } from "./routes/AddClientPolicy"; import { toAddClientPolicy } from "./routes/AddClientPolicy";
import { toEditClientPolicy } from "./routes/EditClientPolicy"; import { toEditClientPolicy } from "./routes/EditClientPolicy";
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner"; import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
import { Controller, useForm } from "react-hook-form"; import { Controller, useForm } from "react-hook-form";
import { toClientPolicies } from "./routes/ClientPolicies"; import { toClientPolicies } from "./routes/ClientPolicies";
import "./realm-settings-section.css";
export const PoliciesTab = () => { export const PoliciesTab = () => {
const { t } = useTranslation("realm-settings"); const { t } = useTranslation("realm-settings");
const adminClient = useAdminClient(); const adminClient = useAdminClient();
@ -93,8 +95,24 @@ export const PoliciesTab = () => {
<Link to={toEditClientPolicy({ realm, policyName: name! })}>{name}</Link> <Link to={toEditClientPolicy({ realm, policyName: name! })}>{name}</Link>
); );
const SwitchRenderer = (clientPolicy: ClientPolicyRepresentation) => { const SwitchRenderer = ({
clientPolicy,
}: {
clientPolicy: ClientPolicyRepresentation;
}) => {
const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({
titleKey: "realm-settings:disablePolicyConfirmTitle",
messageKey: "realm-settings:disablePolicyConfirm",
continueButtonLabel: "common:disable",
onConfirm: () => {
form.setValue(clientPolicy.name!, false);
saveStatus();
},
});
return ( return (
<>
<DisableConfirm />
<Controller <Controller
name={clientPolicy.name!} name={clientPolicy.name!}
data-testid={`${clientPolicy.name!}-switch`} data-testid={`${clientPolicy.name!}-switch`}
@ -106,12 +124,17 @@ export const PoliciesTab = () => {
labelOff={t("common:disabled")} labelOff={t("common:disabled")}
isChecked={value} isChecked={value}
onChange={(value) => { onChange={(value) => {
if (!value) {
toggleDisableDialog();
} else {
onChange(value); onChange(value);
saveStatus(); saveStatus();
}
}} }}
/> />
)} )}
/> />
</>
); );
}; };
@ -247,7 +270,9 @@ export const PoliciesTab = () => {
{ {
name: "enabled", name: "enabled",
displayKey: "realm-settings:status", displayKey: "realm-settings:status",
cellRenderer: SwitchRenderer, cellRenderer: (clientPolicy) => (
<SwitchRenderer clientPolicy={clientPolicy} />
),
}, },
{ {
name: "description", name: "description",
@ -264,9 +289,7 @@ export const PoliciesTab = () => {
code={code} code={code}
language={Language.json} language={Language.json}
height="30rem" height="30rem"
onChange={(value) => { onChange={setCode}
setCode(value ?? "");
}}
/> />
</div> </div>
<div className="pf-u-mt-md"> <div className="pf-u-mt-md">
@ -285,7 +308,6 @@ export const PoliciesTab = () => {
setCode(prettyPrintJSON(tablePolicies)); setCode(prettyPrintJSON(tablePolicies));
}} }}
> >
{" "}
{t("reload")} {t("reload")}
</Button> </Button>
</div> </div>