Browse Source

Sep 30: [FIX] Bug fixed 'auto_save_restrict'

18.0
Risvana Cybro 2 weeks ago
parent
commit
a6395ae176
  1. 93
      auto_save_restrict/static/src/js/view_button.js

93
auto_save_restrict/static/src/js/view_button.js

@ -1,3 +1,5 @@
/** @odoo-module **/
import { patch } from "@web/core/utils/patch"; import { patch } from "@web/core/utils/patch";
import { ViewButton } from "@web/views/view_button/view_button"; import { ViewButton } from "@web/views/view_button/view_button";
import { SettingsConfirmationDialog } from "@web/webclient/settings_form_view/settings_confirmation_dialog"; import { SettingsConfirmationDialog } from "@web/webclient/settings_form_view/settings_confirmation_dialog";
@ -9,95 +11,70 @@ patch(ViewButton.prototype, {
setup() { setup() {
super.setup(...arguments); super.setup(...arguments);
this.dialogService = useService("dialog"); this.dialogService = useService("dialog");
this.actionService = useService("action"); // use this instead of old DOM
}, },
async onClick(ev) { 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) { if (hasUnsavedChanges) {
return; const proceed = await this._confirmSave(model);
if (!proceed) return;
} }
// Normal button behavior
if (this.props.tag === "a") { if (this.props.tag === "a") {
ev.preventDefault(); ev.preventDefault();
} }
if (this.props.onClick) { if (this.props.onClick) {
return this.props.onClick(); return this.props.onClick();
} }
}, if (this.props.clickParams) {
return this.env.onClickViewButton({
discard() { clickParams: this.props.clickParams,
const model = this.props.record || this.props.model; getResParams: () =>
pick(model, "context", "evalContext", "resModel", "resId", "resIds"),
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() { async _confirmSave(model) {
let _continue = true; let proceed = true;
await new Promise((resolve) => { await new Promise((resolve) => {
this.dialogService.add(SettingsConfirmationDialog, { this.dialogService.add(SettingsConfirmationDialog, {
body: _t("Would you like to save your changes?"), body: _t("Would you like to save your changes?"),
confirm: async () => { confirm: async () => {
// Check if `clickParams` is available try {
const clickParams = this.clickParams || { await model.save();
name: "execute", // use Action Service instead of onClickViewButton (avoids `el=null`)
type: "object", if (this.props.clickParams) {
}; this.actionService.doActionButton({
name: this.props.clickParams.name,
const model = this.props.record || this.props.model; type: "object",
if (model) { resModel: model.resModel,
await this.env.onClickViewButton({ resId: model.resId,
clickParams, context: model.context,
getResParams: () => });
pick( }
model, } catch (e) {
"context", console.error("Save failed:", e);
"evalContext",
"resModel",
"resId",
"resIds"
),
});
} else {
console.warn("No model available to execute save.");
} }
_continue = true;
resolve(); resolve();
}, },
cancel: async () => { cancel: async () => {
const model = this.props.record || this.props.model; if (model?.discard) await model.discard();
if (model) {
await model?.discard?.();
await model?.save?.();
} else {
console.warn("No model available to discard or save.");
}
_continue = true;
resolve(); resolve();
}, },
stayHere: () => { stayHere: () => {
_continue = false; proceed = false;
resolve(); resolve();
}, },
}); });
}); });
return _continue; return proceed;
}, },
}); });

Loading…
Cancel
Save