mirror of
https://github.com/makeplane/plane.git
synced 2026-07-14 14:31:37 +02:00
Merge branch 'sync/ce-ee' of github.com:makeplane/plane-ee into develop
This commit is contained in:
@@ -1,26 +1,41 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useRouter } from "next/router";
|
||||
|
||||
const useReloadConfirmations = (message?: string) => {
|
||||
//TODO: remove temp flag isActive later and use showAlert as the source of truth
|
||||
const useReloadConfirmations = (isActive = true) => {
|
||||
const [showAlert, setShowAlert] = useState(false);
|
||||
const router = useRouter();
|
||||
|
||||
const handleBeforeUnload = useCallback(
|
||||
(event: BeforeUnloadEvent) => {
|
||||
if (!isActive || !showAlert) return;
|
||||
event.preventDefault();
|
||||
event.returnValue = "";
|
||||
return message ?? "Are you sure you want to leave?";
|
||||
},
|
||||
[message]
|
||||
[isActive, showAlert]
|
||||
);
|
||||
|
||||
const handleRouteChangeStart = useCallback(
|
||||
(url: string) => {
|
||||
if (!isActive || !showAlert) return;
|
||||
const leave = confirm("Are you sure you want to leave? Changes you made may not be saved.");
|
||||
if (!leave) {
|
||||
router.events.emit("routeChangeError");
|
||||
throw `Route change to "${url}" was aborted (this error can be safely ignored).`;
|
||||
}
|
||||
},
|
||||
[isActive, showAlert, router.events]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!showAlert) {
|
||||
window.removeEventListener("beforeunload", handleBeforeUnload);
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener("beforeunload", handleBeforeUnload);
|
||||
return () => window.removeEventListener("beforeunload", handleBeforeUnload);
|
||||
}, [handleBeforeUnload, showAlert]);
|
||||
router.events.on("routeChangeStart", handleRouteChangeStart);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("beforeunload", handleBeforeUnload);
|
||||
router.events.off("routeChangeStart", handleRouteChangeStart);
|
||||
};
|
||||
}, [handleBeforeUnload, handleRouteChangeStart, router.events]);
|
||||
|
||||
return { setShowAlert };
|
||||
};
|
||||
|
||||
@@ -57,9 +57,6 @@ const PageDetailsPage: NextPageWithLayout = observer(() => {
|
||||
// toast alert
|
||||
const { setToastAlert } = useToast();
|
||||
|
||||
//TODO:fix reload confirmations, with mobx
|
||||
const { setShowAlert } = useReloadConfirmations();
|
||||
|
||||
const { handleSubmit, setValue, watch, getValues, control, reset } = useForm<IPage>({
|
||||
defaultValues: { name: "", description_html: "" },
|
||||
});
|
||||
@@ -92,6 +89,8 @@ const PageDetailsPage: NextPageWithLayout = observer(() => {
|
||||
|
||||
const pageStore = usePage(pageId as string);
|
||||
|
||||
const { setShowAlert } = useReloadConfirmations(pageStore?.isSubmitting === "submitting");
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (pageStore) {
|
||||
|
||||
Reference in New Issue
Block a user