From 219b60ff77530ec837ad0b154ac97e035f64df61 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Wed, 1 Jun 2022 11:12:30 +0200 Subject: [PATCH] Changed to use one confirm dialog (#2722) --- .../manage/sessions/SessionsPage.ts | 2 +- src/sessions/LogoutAllSessionsModal.tsx | 74 --------------- src/sessions/SessionsSection.tsx | 38 +++++--- src/sessions/SessionsTable.tsx | 91 +++++++++++-------- 4 files changed, 78 insertions(+), 127 deletions(-) delete mode 100644 src/sessions/LogoutAllSessionsModal.tsx diff --git a/cypress/support/pages/admin_console/manage/sessions/SessionsPage.ts b/cypress/support/pages/admin_console/manage/sessions/SessionsPage.ts index 4483eeb439..d70aa35365 100644 --- a/cypress/support/pages/admin_console/manage/sessions/SessionsPage.ts +++ b/cypress/support/pages/admin_console/manage/sessions/SessionsPage.ts @@ -12,7 +12,7 @@ export default class SessionsPage { clearNotBeforeButton = "clear-not-before-button"; notBeforeInput = "not-before-input"; logoutAll = "logout-all"; - logoutAllConfirm = "logout-all-confirm-button"; + logoutAllConfirm = "confirm"; setToNow() { cy.findByTestId(this.actionDropdown).should("exist").click(); diff --git a/src/sessions/LogoutAllSessionsModal.tsx b/src/sessions/LogoutAllSessionsModal.tsx deleted file mode 100644 index e3c8b71c48..0000000000 --- a/src/sessions/LogoutAllSessionsModal.tsx +++ /dev/null @@ -1,74 +0,0 @@ -import React from "react"; -import { - AlertVariant, - Button, - ButtonVariant, - Modal, - ModalVariant, - TextContent, -} from "@patternfly/react-core"; -import { useTranslation } from "react-i18next"; -import { useAdminClient } from "../context/auth/AdminClient"; -import { useRealm } from "../context/realm-context/RealmContext"; -import { useAlerts } from "../components/alert/Alerts"; - -type RevocationModalProps = { - handleModalToggle: () => void; -}; - -export const LogoutAllSessionsModal = ({ - handleModalToggle, -}: RevocationModalProps) => { - const { t } = useTranslation("sessions"); - const { addAlert } = useAlerts(); - - const { realm: realmName } = useRealm(); - const adminClient = useAdminClient(); - - const logoutAllSessions = async () => { - try { - await adminClient.realms.logoutAll({ realm: realmName }); - adminClient.keycloak?.logout({ redirectUri: "" }); - } catch (error) { - addAlert(t("logoutAllSessionsError", { error }), AlertVariant.danger); - } - }; - - return ( - { - logoutAllSessions(); - handleModalToggle(); - }} - form="revocation-modal-form" - > - {t("realm-settings:confirm")} - , - , - ]} - > - - {t("logoutAllDescription")} - - - ); -}; diff --git a/src/sessions/SessionsSection.tsx b/src/sessions/SessionsSection.tsx index b4a6c6cbdc..10b6c326fb 100644 --- a/src/sessions/SessionsSection.tsx +++ b/src/sessions/SessionsSection.tsx @@ -6,18 +6,22 @@ import { useTranslation } from "react-i18next"; import { ViewHeader } from "../components/view-header/ViewHeader"; import { useAdminClient } from "../context/auth/AdminClient"; import helpUrls from "../help-urls"; -import { LogoutAllSessionsModal } from "./LogoutAllSessionsModal"; import { RevocationModal } from "./RevocationModal"; import SessionsTable from "./SessionsTable"; +import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; +import { useAlerts } from "../components/alert/Alerts"; +import { useRealm } from "../context/realm-context/RealmContext"; import "./SessionsSection.css"; export default function SessionsSection() { - const adminClient = useAdminClient(); const { t } = useTranslation("sessions"); + + const adminClient = useAdminClient(); + const { addError } = useAlerts(); + const { realm } = useRealm(); + const [revocationModalOpen, setRevocationModalOpen] = useState(false); - const [logoutAllSessionsModalOpen, setLogoutAllSessionsModalOpen] = - useState(false); const [activeClientDetails, setActiveClientDetails] = useState< ClientRepresentation[] >([]); @@ -27,10 +31,6 @@ export default function SessionsSection() { setRevocationModalOpen(!revocationModalOpen); }; - const handleLogoutAllSessionsModalToggle = () => { - setLogoutAllSessionsModalOpen(!logoutAllSessionsModalOpen); - }; - const loader = async () => { const activeClients = await adminClient.sessions.find(); const clientSessions = ( @@ -63,6 +63,20 @@ export default function SessionsSection() { return userSessions; }; + const [toggleLogoutDialog, LogoutConfirm] = useConfirmDialog({ + titleKey: "sessions:logoutAllSessions", + messageKey: "sessions:logoutAllDescription", + continueButtonLabel: "common:confirm", + onConfirm: async () => { + try { + await adminClient.realms.logoutAll({ realm }); + adminClient.keycloak?.logout({ redirectUri: "" }); + } catch (error) { + addError("sessions:logoutAllSessionsError", error); + } + }, + }); + const dropdownItems = [ handleLogoutAllSessionsModalToggle()} + onClick={toggleLogoutDialog} > {t("signOutAllActiveSessions")} , @@ -85,6 +99,7 @@ export default function SessionsSection() { return ( <> + )} - {logoutAllSessionsModalOpen && ( - - )} diff --git a/src/sessions/SessionsTable.tsx b/src/sessions/SessionsTable.tsx index 89af613ceb..37e8eed99a 100644 --- a/src/sessions/SessionsTable.tsx +++ b/src/sessions/SessionsTable.tsx @@ -12,6 +12,8 @@ import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import { toClient } from "../clients/routes/Client"; +import { useAlerts } from "../components/alert/Alerts"; +import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; import { ListEmptyState } from "../components/list-empty-state/ListEmptyState"; import { Field, @@ -43,8 +45,9 @@ export default function SessionsTable({ const { whoAmI } = useWhoAmI(); const { t } = useTranslation("sessions"); const adminClient = useAdminClient(); - const [key, setKey] = useState(0); + const { addError } = useAlerts(); const locale = whoAmI.getLocale(); + const [key, setKey] = useState(0); const refresh = () => setKey((value) => value + 1); const columns = useMemo(() => { @@ -98,6 +101,20 @@ export default function SessionsTable({ ); }, [realm, locale, hiddenColumns]); + const [toggleLogoutDialog, LogoutConfirm] = useConfirmDialog({ + titleKey: "sessions:logoutAllSessions", + messageKey: "sessions:logoutAllDescription", + continueButtonLabel: "common:confirm", + onConfirm: async () => { + try { + await adminClient.users.logout({ id: logoutUser! }); + refresh(); + } catch (error) { + addError("sessions:logoutAllSessionsError", error); + } + }, + }); + async function onClickSignOut(session: UserSessionRepresentation) { await adminClient.realms.deleteSession({ realm, session: session.id! }); @@ -109,42 +126,40 @@ export default function SessionsTable({ } return ( - - - - ) - } - columns={columns} - actions={[ - { - title: t("common:signOut"), - onRowClick: onClickSignOut, - }, - ]} - emptyState={ - - } - /> + <> + + + + + ) + } + columns={columns} + actions={[ + { + title: t("common:signOut"), + onRowClick: onClickSignOut, + }, + ]} + emptyState={ + + } + /> + ); }