From a6395ae1763585abc79270d235d88ada6e91c1f1 Mon Sep 17 00:00:00 2001 From: Risvana Cybro Date: Tue, 30 Sep 2025 21:00:02 +0530 Subject: [PATCH] Sep 30: [FIX] Bug fixed 'auto_save_restrict' --- .../static/src/js/view_button.js | 93 +++++++------------ 1 file changed, 35 insertions(+), 58 deletions(-) diff --git a/auto_save_restrict/static/src/js/view_button.js b/auto_save_restrict/static/src/js/view_button.js index 116c5675d..c950efae3 100644 --- a/auto_save_restrict/static/src/js/view_button.js +++ b/auto_save_restrict/static/src/js/view_button.js @@ -1,3 +1,5 @@ +/** @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"; @@ -9,95 +11,70 @@ patch(ViewButton.prototype, { setup() { super.setup(...arguments); this.dialogService = useService("dialog"); + this.actionService = useService("action"); // use this instead of old DOM }, async onClick(ev) { - const proceed = await this._confirmSave(); + const model = this.props.record || this.props.model; + const hasUnsavedChanges = + model && typeof model.isDirty === "function" && (await model.isDirty()); - if (!proceed) { - return; + if (hasUnsavedChanges) { + const proceed = await this._confirmSave(model); + if (!proceed) return; } + // Normal button behavior 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; + if (this.props.clickParams) { + return this.env.onClickViewButton({ + clickParams: this.props.clickParams, + getResParams: () => + pick(model, "context", "evalContext", "resModel", "resId", "resIds"), + }); } - - this.env.onClickViewButton({ - clickParams: { - name: "cancel", - type: "object", - special: "cancel", - }, - getResParams: () => - pick(model, "context", "evalContext", "resModel", "resId", "resIds"), - }); }, - async _confirmSave() { - let _continue = true; + async _confirmSave(model) { + let proceed = 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."); + try { + await model.save(); + // use Action Service instead of onClickViewButton (avoids `el=null`) + if (this.props.clickParams) { + this.actionService.doActionButton({ + name: this.props.clickParams.name, + type: "object", + resModel: model.resModel, + resId: model.resId, + context: model.context, + }); + } + } catch (e) { + console.error("Save failed:", e); } - - _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; + if (model?.discard) await model.discard(); resolve(); }, stayHere: () => { - _continue = false; + proceed = false; resolve(); }, }); }); - return _continue; + return proceed; }, });