5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-22 23:16:22 +00:00

fix: avisar antes de salir si hubo cambios en el formulario

This commit is contained in:
f 2024-06-18 15:36:13 -03:00
parent cb45d0aa6c
commit af2f97e56e
No known key found for this signature in database
4 changed files with 48 additions and 1 deletions

View 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)));
}
}

View file

@ -33,7 +33,7 @@
- dir = t("locales.#{@locale}.dir") - dir = t("locales.#{@locale}.dir")
-# Comienza el formulario -# 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 -# Botones de guardado
= render 'posts/submit', site: site, post: post = render 'posts/submit', site: site, post: post

View file

@ -814,6 +814,7 @@ en:
destroy: Delete destroy: Delete
confirm_destroy: Are you sure? confirm_destroy: Are you sure?
form: form:
confirm: "You have unsaved changes and changing pages may lose them, continue anyway?"
errors: errors:
title: There are some errors on the form title: There are some errors on the form
help: Please, verify that all values are correct. help: Please, verify that all values are correct.

View file

@ -822,6 +822,7 @@ es:
destroy: Borrar destroy: Borrar
confirm_destroy: ¿Estás segure? confirm_destroy: ¿Estás segure?
form: form:
confirm: "Tenés cambios sin guardar y cambiar de página podría perderlos, ¿querés continuar de todas formas?"
errors: errors:
title: Hay errores en el formulario title: Hay errores en el formulario
help: Por favor, verifica que todos los valores sean correctos. help: Por favor, verifica que todos los valores sean correctos.