-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make confirmation before flashcard result saved
- Loading branch information
1 parent
5d7cd23
commit 09cb850
Showing
4 changed files
with
239 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useEffect } from "react"; | ||
import Router from "next/router"; | ||
import { useBeforeUnload } from "react-use"; | ||
|
||
export const useLeavePageConfirmation = ( | ||
showAlert = true, | ||
message?: string | ||
) => { | ||
useBeforeUnload(showAlert, message); | ||
|
||
useEffect(() => { | ||
const handler = () => { | ||
if (showAlert && !window.confirm(message)) { | ||
throw "cancel"; | ||
} | ||
}; | ||
|
||
Router.events.on("routeChangeStart", handler); | ||
|
||
return () => { | ||
Router.events.off("routeChangeStart", handler); | ||
}; | ||
}, [showAlert, message]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.