From 8d21036ab3b8d90004d8d7e6da1d07a869695e80 Mon Sep 17 00:00:00 2001 From: Cybrosys Technologies Date: Sat, 31 May 2025 17:49:25 +0530 Subject: [PATCH] May 31: [FIX] Bug Fixed 'auto_save_restrict' --- auto_save_restrict/__manifest__.py | 5 +- auto_save_restrict/doc/RELEASE_NOTES.md | 5 + .../static/src/js/form_controller.js | 65 +++++++---- .../static/src/js/view_button.js | 103 ++++++++++++++++++ 4 files changed, 152 insertions(+), 26 deletions(-) create mode 100644 auto_save_restrict/static/src/js/view_button.js diff --git a/auto_save_restrict/__manifest__.py b/auto_save_restrict/__manifest__.py index 3873103ba..bd76fda16 100644 --- a/auto_save_restrict/__manifest__.py +++ b/auto_save_restrict/__manifest__.py @@ -21,7 +21,7 @@ ############################################################################# { 'name': 'Auto Save Restrict', - 'version': '17.0.1.0.0', + 'version': '17.0.1.0.3', 'category': 'Extra Tools', 'summary': """Restrict auto save in all models""", 'description': 'Using this module,we can restrict' @@ -36,7 +36,8 @@ 'assets': { 'web.assets_backend': [ 'auto_save_restrict/static/src/js/form_controller.js', - 'auto_save_restrict/static/src/js/list_controller.js' + 'auto_save_restrict/static/src/js/list_controller.js', + 'auto_save_restrict/static/src/js/view_button.js' ], }, 'images': ['static/description/banner.png'], diff --git a/auto_save_restrict/doc/RELEASE_NOTES.md b/auto_save_restrict/doc/RELEASE_NOTES.md index 6b03b687d..edd9d0fc6 100644 --- a/auto_save_restrict/doc/RELEASE_NOTES.md +++ b/auto_save_restrict/doc/RELEASE_NOTES.md @@ -15,3 +15,8 @@ #### Version 17.0.1.0.2 ##### BUG FIX - Fixed the issue in the form where the popup appeared repeatedly + +#### 31.05.2025 +#### Version 17.0.1.0.3 +##### BUG FIX +- Fixed the issue in the smart button where the changes save automatically. diff --git a/auto_save_restrict/static/src/js/form_controller.js b/auto_save_restrict/static/src/js/form_controller.js index b70a7e8d8..f414af71c 100644 --- a/auto_save_restrict/static/src/js/form_controller.js +++ b/auto_save_restrict/static/src/js/form_controller.js @@ -1,33 +1,50 @@ /** @odoo-module */ import { FormController } from "@web/views/form/form_controller"; import { patch } from "@web/core/utils/patch"; -import { useSetupView } from "@web/views/view_hook"; -patch(FormController.prototype, { +import { useSetupAction } from "@web/webclient/actions/action_hook"; +import { _t } from "@web/core/l10n/translation"; +import { SettingsConfirmationDialog } from "@web/webclient/settings_form_view/settings_confirmation_dialog"; + /* Patch FormController to restrict auto save in form views */ + +patch(FormController.prototype, { setup(){ super.setup(...arguments); - this.beforeLeaveHook = false - useSetupView({ - beforeLeave: () => this.beforeLeave(), - beforeUnload: (ev) => this.beforeUnload(ev), - }); }, + async beforeLeave() { - /* function will work before leave the form */ - if(this.model.root.isDirty && this.beforeLeaveHook == false){ - if (confirm("Do you want to save changes before leaving?")) { - this.beforeLeaveHook = true - await this.model.root.save({ - reload: false, - onError: this.onSaveError.bind(this), - }); - } else { - this.beforeLeaveHook = true - this.model.root.discard(); - } - } - }, - beforeUnload: async (ev) => { - ev.preventDefault(); - } + const dirty = await this.model.root.isDirty(); + if (dirty) { + return this._confirmSave(); + } + }, + + beforeUnload() {}, + + async _confirmSave() { + let _continue = true; + await new Promise((resolve) => { + this.dialogService.add(SettingsConfirmationDialog, { + body: _t("Would you like to save your changes?"), + confirm: async () => { + await this.save(); + // It doesn't make sense to do the action of the button + // as the res.config.settings `execute` method will trigger a reload. + _continue = true; + resolve(); + }, + cancel: async () => { + await this.model.root.discard(); + await this.model.root.save(); + _continue = true; + resolve(); + }, + stayHere: () => { + _continue = false; + resolve(); + }, + }); + }); + return _continue; + } }); diff --git a/auto_save_restrict/static/src/js/view_button.js b/auto_save_restrict/static/src/js/view_button.js new file mode 100644 index 000000000..05d180567 --- /dev/null +++ b/auto_save_restrict/static/src/js/view_button.js @@ -0,0 +1,103 @@ +/** @odoo-module */ +import { patch } from "@web/core/utils/patch"; +import { ViewButton } from "@web/views/view_button/view_button"; +import { SettingsConfirmationDialog } from "@web/webclient/settings_form_view/settings_confirmation_dialog"; +import { useService } from "@web/core/utils/hooks"; +import { _t } from "@web/core/l10n/translation"; +import { pick } from "@web/core/utils/objects"; + +patch(ViewButton.prototype, { + setup() { + super.setup(...arguments); + this.dialogService = useService("dialog"); + }, + + async onClick(ev) { + const proceed = await this._confirmSave(); + + if (!proceed) { + return; + } + + if (this.props.tag === "a") { + ev.preventDefault(); + } + + if (this.props.onClick) { + return this.props.onClick(); + } + }, + + discard() { + const model = this.props.record || this.props.model; + + if (!model) { + console.warn("No model available to discard or save."); + return; + } + + this.env.onClickViewButton({ + clickParams: { + name: "cancel", + type: "object", + special: "cancel", + }, + getResParams: () => + pick(model, "context", "evalContext", "resModel", "resId", "resIds"), + }); + }, + + async _confirmSave() { + let _continue = true; + await new Promise((resolve) => { + this.dialogService.add(SettingsConfirmationDialog, { + body: _t("Would you like to save your changes?"), + confirm: async () => { + // Check if `clickParams` is available + const clickParams = this.clickParams || { + name: "execute", + type: "object", + }; + + const model = this.props.record || this.props.model; + if (model) { + await this.env.onClickViewButton({ + clickParams, + getResParams: () => + pick( + model, + "context", + "evalContext", + "resModel", + "resId", + "resIds" + ), + }); + } else { + console.warn("No model available to execute save."); + } + + _continue = true; + resolve(); + }, + cancel: async () => { + const model = this.props.record || this.props.model; + if (model) { + await model?.discard?.(); + await model?.save?.(); + } else { + console.warn("No model available to discard or save."); + } + _continue = true; + resolve(); + }, + stayHere: () => { + _continue = false; + resolve(); + }, + }); + }); + + return _continue; + }, +}); \ No newline at end of file