Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show warning before navigating away without submitting #33

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions components/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,33 @@ export function useUser () {
}
}

// export function useUser({ redirectTo, redirectIfFound } = {}) {
// const { data, error } = useSWR('/api/user', fetcher)
// const user = data?.user
// const finished = Boolean(data)
// const hasUser = Boolean(user)

// useEffect(() => {
// if (!redirectTo || !finished) return
// if (
// // If redirectTo is set, redirect if the user was not found.
// (redirectTo && !redirectIfFound && !hasUser) ||
// // If redirectIfFound is also set, redirect if the user was found
// (redirectIfFound && hasUser)
// ) {
// Router.push(redirectTo)
// }
// }, [redirectTo, redirectIfFound, finished, hasUser])

// return error ? null : user
// }
export function useWarnBeforeUnload (active, promptText) {
const router = useRouter()
useEffect(() => {
const handleWindowClose = (e) => {
if (!active) return
e.preventDefault()
return (e.returnValue = promptText)
}

// const handleRouterChange = (url, obj) => {
// if (!active) return
// if (window.confirm(promptText)) return
// // Since back button changes the URL, we reset it
// if (router.asPath !== window.location.pathname) {
// window.history.pushState(null, null, router.asPath)
// }
// const errMessage = 'routeChange aborted.'
// throw errMessage
// }

window.addEventListener('beforeunload', handleWindowClose)
// router.events.on('routeChangeStart', handleRouterChange)

return () => {
window.removeEventListener('beforeunload', handleWindowClose)
// router.events.off('routeChangeStart', handleRouterChange)
}
}, [active, promptText, router, router.events])
return true
}
5 changes: 5 additions & 0 deletions components/submit/UrlList.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import SubmitButton from './SubmitButton'
import { getPrettyErrorMessage } from '../lib/translateErrors'
import { SubmissionContext } from './SubmissionContext'
import { useNotifier } from '../lib/notifier'
import { useWarnBeforeUnload } from '../lib/hooks'

const promptText = 'You have made some changes that have not been submitted. Are you sure you want to ?'

// Does these
// * Decides what data to pass down to the table
Expand Down Expand Up @@ -44,6 +47,8 @@ const UrlList = ({ cc }) => {

const { submissionState, mutate: mutateSubmissionState } = useContext(SubmissionContext)

useWarnBeforeUnload(submissionState === 'IN_PROGRESS', promptText)

const entryToEdit = useMemo(() => {
let entry = {}
if (data) {
Expand Down