mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-22 22:56:21 +00:00
fix: avisar antes de salir si hubo cambios en el formulario
This commit is contained in:
parent
cb45d0aa6c
commit
af2f97e56e
4 changed files with 48 additions and 1 deletions
45
app/javascript/controllers/unsaved_changes_controller.js
Normal file
45
app/javascript/controllers/unsaved_changes_controller.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
import { Controller } from "stimulus";
|
||||
|
||||
export default class extends Controller {
|
||||
connect() {
|
||||
this.originalFormData = new FormData(this.element);
|
||||
this.originalFormDataSerialized = this.serializeFormData(this.originalFormData);
|
||||
this.submitting = false;
|
||||
}
|
||||
|
||||
submit(event) {
|
||||
this.submitting = true;
|
||||
}
|
||||
|
||||
unsaved(event) {
|
||||
if (this.submitting) return;
|
||||
if (!this.hasChanged()) return;
|
||||
|
||||
this.submitting = false;
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
event.returnValue = true;
|
||||
}
|
||||
|
||||
unsavedTurbolinks(event) {
|
||||
if (this.submitting) return;
|
||||
if (!this.hasChanged()) return;
|
||||
|
||||
this.submitting = false;
|
||||
|
||||
if (window.confirm(this.element.dataset.unsavedChangesConfirmValue)) return;
|
||||
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
serializeFormData(formData) {
|
||||
formData.delete("authenticity_token");
|
||||
|
||||
return (new URLSearchParams(formData)).toString();;
|
||||
}
|
||||
|
||||
hasChanged() {
|
||||
return (this.originalFormDataSerialized !== this.serializeFormData(new FormData(this.element)));
|
||||
}
|
||||
}
|
|
@ -33,7 +33,7 @@
|
|||
- dir = t("locales.#{@locale}.dir")
|
||||
|
||||
-# Comienza el formulario
|
||||
= form_tag url, method: method, class: "form post #{extra_class}", multipart: true do
|
||||
= form_tag url, method: method, class: "form post #{extra_class}", multipart: true, data: { controller: 'unsaved-changes', action: 'unsaved-changes#submit beforeunload@window->unsaved-changes#unsaved turbolinks:before-visit@window->unsaved-changes#unsavedTurbolinks', 'unsaved-changes-confirm-value': t('.confirm') } do
|
||||
-# Botones de guardado
|
||||
= render 'posts/submit', site: site, post: post
|
||||
|
||||
|
|
|
@ -814,6 +814,7 @@ en:
|
|||
destroy: Delete
|
||||
confirm_destroy: Are you sure?
|
||||
form:
|
||||
confirm: "You have unsaved changes and changing pages may lose them, continue anyway?"
|
||||
errors:
|
||||
title: There are some errors on the form
|
||||
help: Please, verify that all values are correct.
|
||||
|
|
|
@ -822,6 +822,7 @@ es:
|
|||
destroy: Borrar
|
||||
confirm_destroy: ¿Estás segure?
|
||||
form:
|
||||
confirm: "Tenés cambios sin guardar y cambiar de página podría perderlos, ¿querés continuar de todas formas?"
|
||||
errors:
|
||||
title: Hay errores en el formulario
|
||||
help: Por favor, verifica que todos los valores sean correctos.
|
||||
|
|
Loading…
Reference in a new issue