From d1e97e344869cb0aa8d4c130f907d8b37a75b683 Mon Sep 17 00:00:00 2001 From: Marc Durepos Date: Fri, 16 May 2025 10:57:10 -0400 Subject: [PATCH] fix confirm_many2one_create JS --- .../static/src/js/many2one_field.esm.js | 67 +++++++++++++++++-- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/confirm_many2one_create/static/src/js/many2one_field.esm.js b/confirm_many2one_create/static/src/js/many2one_field.esm.js index d231a00..97e3f24 100644 --- a/confirm_many2one_create/static/src/js/many2one_field.esm.js +++ b/confirm_many2one_create/static/src/js/many2one_field.esm.js @@ -2,11 +2,68 @@ import {Many2OneField} from "@web/views/fields/many2one/many2one_field"; import {patch} from "@web/core/utils/patch"; +import {_t} from "@web/core/l10n/translation"; +import {Dialog} from "@web/core/dialog/dialog"; +import {Component, markup} from "@odoo/owl"; + +// Create a custom confirmation dialog component +class CreateConfirmationDialog extends Component { + static template = "web.Many2OneField.CreateConfirmationDialog"; + static components = {Dialog}; + + get title() { + return _t("New: %s", this.props.name); + } + + get dialogContent() { + return markup( + _t( + "Create %s as a new %s?", + this.props.value, + this.props.name + ) + ); + } + + async onCreate() { + await this.props.create(); + this.props.close(); + } +} patch(Many2OneField.prototype, { - get Many2XAutocompleteProps() { - const props = super.Many2XAutocompleteProps; - props.quickCreate = this.openConfirmationDialog.bind(this); - return props; - }, + setup() { + // Call the original setup method + super.setup(); + + // Store the original quickCreate function + const originalQuickCreate = this.quickCreate; + + // Replace the quickCreate function with our own implementation + if (originalQuickCreate) { + // Save the original quickCreate function + this._originalQuickCreate = originalQuickCreate; + + // Override quickCreate to show a confirmation dialog + this.quickCreate = async (name) => { + this.state.isFloating = false; + + return new Promise((resolve, reject) => { + this.addDialog(CreateConfirmationDialog, { + name: this.string, + value: name, + create: async () => { + try { + // Call the original quickCreate function + await this._originalQuickCreate(name); + resolve(); + } catch (e) { + reject(e); + } + }, + }); + }); + }; + } + }, });