From b1944916c3daff3e2a9d5fc8035b6d798bf91fb8 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 22 Aug 2022 13:16:30 +0530 Subject: [PATCH] fix: show discard dialog only if form is dirty --- frappe/public/js/frappe/web_form/web_form.js | 32 +++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/frappe/public/js/frappe/web_form/web_form.js b/frappe/public/js/frappe/web_form/web_form.js index 8a4a562ead..f01bd7d54f 100644 --- a/frappe/public/js/frappe/web_form/web_form.js +++ b/frappe/public/js/frappe/web_form/web_form.js @@ -166,20 +166,30 @@ export default class WebForm extends frappe.ui.FieldGroup { } setup_discard_action() { - $(".web-form-footer .discard-btn").on("click", () => this.discard_form()); + $(".web-form-footer .discard-btn").on("click", (e) => { + setTimeout(() => { + e.stopPropagation(); + this.discard_form(); + }, 200); + return false; + }); } discard_form() { - frappe.warn( - __("Discard?"), - __("Are you sure you want to discard the changes?"), - () => { - let path = window.location.href; - // remove new or edit after last / from url - window.location.href = path.substring(0, path.lastIndexOf("/")); - }, - __("Discard") - ); + let path = window.location.href; + // remove new or edit after last / from url + path = path.substring(0, path.lastIndexOf("/")); + + if (frappe.form_dirty) { + frappe.warn( + __("Discard?"), + __("Are you sure you want to discard the changes?"), + () => (window.location.href = path), + __("Discard") + ); + } else { + window.location.href = path; + } return false; }