This extracts the unsaved changes warning code from course-planner (seas-computing/course-planner#370) into a custom hook that can be reused in other projects. The hook returns two functions: markAsChanged can be called with a boolean to indicate when the data has changed (true) or not (false), and confirmDiscard will show a confirm dialog and return the user's response as a boolean if they want to discard their changes (true) or return to working (false).
I've added test for the confirmDiscard functionality, but I wasn't able to find a reasonable to test the onbeforeunload handler that blocks navigation events; we're using jsdom to render our components, and it doesn't support simulating navigation.
Types of changes
[ ] Bug fix (non-breaking change which fixes an issue)
[x] New feature (non-breaking change which adds functionality)
[ ] Breaking change (fix or feature that would cause existing functionality
to change)
Checklist:
[x] I have run eslint on the code
[x] I have added JSDoc for all of my code (where applicable)
Describe your changes
This extracts the unsaved changes warning code from course-planner (seas-computing/course-planner#370) into a custom hook that can be reused in other projects. The hook returns two functions:
markAsChanged
can be called with a boolean to indicate when the data has changed (true
) or not (false
), andconfirmDiscard
will show a confirm dialog and return the user's response as a boolean if they want to discard their changes (true
) or return to working (false
).I've added test for the
confirmDiscard
functionality, but I wasn't able to find a reasonable to test theonbeforeunload
handler that blocks navigation events; we're usingjsdom
to render our components, and it doesn't support simulating navigation.Types of changes
Checklist:
eslint
on the codePriority:
Related Issues:
Fixes seas-computing/course-planner#419