Skip to content

Commit

Permalink
Make code climate happy for the cognitive threshold issue in preventT…
Browse files Browse the repository at this point in the history
…urboNavigation function
  • Loading branch information
coolprobn committed Nov 15, 2024
1 parent c9af2cc commit 744a3eb
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions app/javascript/js/controllers/form_exit_prompt_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ export default class extends Controller {
this.currentFormState = {}
}

handleDirtyFormNavigation(event) {
const message = 'Are you sure you want to navigate away from the page? You will lose all your changes.'

if (window.confirm(message)) {
this.resetState()
} else {
event.preventDefault()
}
}

preventTurboNavigation(event) {
// don't intercept if URL doesn't change e.g. modals OR when form is submitting
if (event.detail.url === this.currentLocationUrl || this.isFormSubmitting) {
Expand All @@ -102,13 +112,7 @@ export default class extends Controller {
this.evaluateFormState()

if (this.isDirty) {
const message = 'Are you sure you want to navigate away from the page? You will lose all your changes.'

if (window.confirm(message)) {
this.resetState()
} else {
event.preventDefault()
}
this.handleDirtyFormNavigation(event)
}
}

Expand Down

0 comments on commit 744a3eb

Please sign in to comment.