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

feature: Alert users when form has unsaved changes #3346

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions app/components/avo/views/resource_edit_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
html: {
novalidate: true
},
data: { controller: "form-exit-prompt", form_exit_prompt_target: "form" },
multipart: true do |form| %>
<%= render Avo::ReferrerParamsComponent.new back_path: back_path %>
<%= content_tag :div, class: "space-y-12" do %>
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import DateFieldController from './controllers/fields/date_field_controller'
import DateTimeFilterController from './controllers/date_time_filter_controller'
import EasyMdeController from './controllers/fields/easy_mde_controller'
import FilterController from './controllers/filter_controller'
import FormExitPromptController from './controllers/form_exit_prompt_controller'
import HiddenInputController from './controllers/hidden_input_controller'
import InputAutofocusController from './controllers/input_autofocus_controller'
import ItemSelectAllController from './controllers/item_select_all_controller'
Expand Down Expand Up @@ -56,6 +57,7 @@ application.register('copy-to-clipboard', CopyToClipboardController)
application.register('dashboard-card', DashboardCardController)
application.register('date-time-filter', DateTimeFilterController)
application.register('filter', FilterController)
application.register('form-exit-prompt', FormExitPromptController)
application.register('panel-refresh', PanelRefreshController)
application.register('hidden-input', HiddenInputController)
application.register('input-autofocus', InputAutofocusController)
Expand Down
58 changes: 58 additions & 0 deletions app/javascript/js/controllers/form_exit_prompt_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static targets = ['form']

connect() {
this.isDirty = false

// for select tags
this.formTarget.addEventListener('change', this.trackChanges.bind(this))
// for all other input fields
this.formTarget.addEventListener('input', this.trackChanges.bind(this))

// in most cases this event will be triggered because Turbo prevents full page reload on navigation
window.addEventListener(
'turbo:before-visit',
this.preventTurboNavigation.bind(this),
)
window.addEventListener('beforeunload', this.preventFullPageNavigation.bind(this))
}

disconnect() {
window.removeEventListener(
'turbo:before-visit',
this.preventTurboNavigation.bind(this),
)
window.removeEventListener(
'beforeunload',
this.preventFullPageNavigation.bind(this),
)
}

trackChanges() {
this.isDirty = true
}

preventTurboNavigation(event) {
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.isDirty = false
} else {
event.preventDefault()
}
}
}

preventFullPageNavigation(event) {
if (this.isDirty) {
event.preventDefault()

// for legacy browsers support
// see: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
event.returnValue = 'Are you sure you want to navigate away from the page? You will lose all your changes.'
}
}
}
Loading