Skip to content

Commit

Permalink
fix: show window closing alert only when page is not saved
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucaso1 committed Feb 6, 2024
1 parent 0165aba commit 54ed828
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions web/hooks/use-reload-confirmation.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { useCallback, useEffect, useState } from "react";

const useReloadConfirmations = (message?: string) => {
const useReloadConfirmations = (message?: string, isActive = true) => {
const [showAlert, setShowAlert] = useState(false);

const handleBeforeUnload = useCallback(
(event: BeforeUnloadEvent) => {
if(!isActive) return;
event.preventDefault();
event.returnValue = "";
return message ?? "Are you sure you want to leave?";
},
[message]
[message, isActive]
);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,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: "" },
Expand Down Expand Up @@ -89,6 +87,10 @@ const PageDetailsPage: NextPageWithLayout = observer(() => {

const pageStore = usePage(pageId as string);

//TODO:fix reload confirmations, with mobx
const { setShowAlert } = useReloadConfirmations(undefined, pageStore?.isSubmitting === "submitting");


useEffect(
() => () => {
if (pageStore) {
Expand Down

0 comments on commit 54ed828

Please sign in to comment.