From adf891bcbac5ba7c5f47587b2daa9eed2f0cf620 Mon Sep 17 00:00:00 2001 From: rahulramesha <71900764+rahulramesha@users.noreply.github.com> Date: Fri, 23 Aug 2024 18:00:15 +0530 Subject: [PATCH] [WEB-2150] fix: issue selection redirect alert (#5406) * fix issue selection redirect alert * change message content for user prompt --- web/core/hooks/use-multiple-select.ts | 11 +++++++++++ web/core/hooks/use-reload-confirmation.tsx | 12 ++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/web/core/hooks/use-multiple-select.ts b/web/core/hooks/use-multiple-select.ts index 278304c4f..1e997bbfe 100644 --- a/web/core/hooks/use-multiple-select.ts +++ b/web/core/hooks/use-multiple-select.ts @@ -3,6 +3,8 @@ import { useCallback, useEffect, useMemo } from "react"; // hooks import { useMultipleSelectStore } from "@/hooks/store"; +// +import useReloadConfirmations from "./use-reload-confirmation"; export type TEntityDetails = { entityID: string; @@ -52,6 +54,15 @@ export const useMultipleSelect = (props: Props) => { getEntityDetailsFromEntityID, } = useMultipleSelectStore(); + useReloadConfirmations( + selectedEntityIds && selectedEntityIds.length > 0, + "Are you sure you want to leave? Your current bulk operation selections will be lost.", + true, + () => { + clearSelection(); + } + ); + const groups = useMemo(() => Object.keys(entities), [entities]); const entitiesList: TEntityDetails[] = useMemo( diff --git a/web/core/hooks/use-reload-confirmation.tsx b/web/core/hooks/use-reload-confirmation.tsx index 998c5845d..6ba324d14 100644 --- a/web/core/hooks/use-reload-confirmation.tsx +++ b/web/core/hooks/use-reload-confirmation.tsx @@ -1,8 +1,10 @@ import { useCallback, useEffect, useState } from "react"; //TODO: remove temp flag isActive later and use showAlert as the source of truth -const useReloadConfirmations = (isActive = true) => { - const [showAlert, setShowAlert] = useState(false); +const useReloadConfirmations = (isActive = true, message?: string, defaultShowAlert = false, onLeave?: () => void) => { + const [showAlert, setShowAlert] = useState(defaultShowAlert); + + const alertMessage = message ?? "Are you sure you want to leave? Changes you made may not be saved."; const handleBeforeUnload = useCallback( (event: BeforeUnloadEvent) => { @@ -28,8 +30,10 @@ const useReloadConfirmations = (isActive = true) => { const isAnchorTargetBlank = anchorElement.getAttribute("target") === "_blank"; if (isAnchorTargetBlank) return; // show confirm dialog - const leave = confirm("Are you sure you want to leave? Changes you made may not be saved."); - if (!leave) { + const isLeaving = confirm(alertMessage); + if (isLeaving) { + onLeave && onLeave(); + } else { event.preventDefault(); event.stopPropagation(); }