From c5e324cbe45d4820f2d6b51fd2a5a4198c77e841 Mon Sep 17 00:00:00 2001 From: "Hubert Van de Walle (huvw)" Date: Thu, 22 Dec 2022 18:04:30 +0000 Subject: [PATCH] [FIX] web: attach_document widget now works when triggered from a dropdown Steps to reproduce ================== - Use a mobile viewport - Go to Expenses - Create a record - Enter a description and a category - Click on Action - Click on Attach Receipt -> Nothing happens Cause of the issue ================== When the button is clicked, the dropdown is closed. This means that the AttachDocumentWidget and FileInput Components are both destroyed. Solution ======== Instead of using the FileInput Component, use an input element outside the DOM. We also use `this.env.services.orm` instead of `useService("orm")` because the last one does nothing when the widget is destroyed opw-3065590 closes odoo/odoo#108897 X-original-commit: 84d50b40f150f251a81e128fa9645d7fcc729dab Signed-off-by: Samuel Degueldre --- .../attach_document/attach_document.js | 36 ++++++++++++-- .../attach_document/attach_document.xml | 14 +----- .../tests/mobile/views/form_view_tests.js | 49 +++++++++++++++++++ .../views/widgets/attach_document_tests.js | 43 ++++++++++------ 4 files changed, 112 insertions(+), 30 deletions(-) diff --git a/addons/web/static/src/views/widgets/attach_document/attach_document.js b/addons/web/static/src/views/widgets/attach_document/attach_document.js index e28cbf06ce2..3735e850aec 100644 --- a/addons/web/static/src/views/widgets/attach_document/attach_document.js +++ b/addons/web/static/src/views/widgets/attach_document/attach_document.js @@ -7,17 +7,47 @@ import { standardWidgetProps } from "@web/views/widgets/standard_widget_props"; import { Component } from "@odoo/owl"; -class AttachDocumentWidget extends Component { +export class AttachDocumentWidget extends Component { setup() { - this.orm = useService("orm"); + this.http = useService("http"); this.notification = useService("notification"); + + this.fileInput = document.createElement("input"); + this.fileInput.type = "file"; + this.fileInput.accept = "*"; + this.fileInput.multiple = true; + this.fileInput.onchange = this.onInputChange.bind(this); + } + + async onInputChange() { + const fileData = await this.http.post( + "/web/binary/upload_attachment", + { + csrf_token: odoo.csrf_token, + ufile: [...this.fileInput.files], + model: this.props.record.resModel, + id: this.props.record.resId, + }, + "text" + ); + const parsedFileData = JSON.parse(fileData); + if (parsedFileData.error) { + throw new Error(parsedFileData.error); + } + await this.onFileUploaded(parsedFileData); + } + + async triggerUpload() { + if (await this.beforeOpen()) { + this.fileInput.click(); + } } async onFileUploaded(files) { const { action, record } = this.props; if (action) { const { model, resId, resModel } = record; - await this.orm.call(resModel, action, [resId], { + await this.env.services.orm.call(resModel, action, [resId], { attachment_ids: files.map((file) => file.id), }); await record.load(); diff --git a/addons/web/static/src/views/widgets/attach_document/attach_document.xml b/addons/web/static/src/views/widgets/attach_document/attach_document.xml index c8528900425..d0fc0dac767 100644 --- a/addons/web/static/src/views/widgets/attach_document/attach_document.xml +++ b/addons/web/static/src/views/widgets/attach_document/attach_document.xml @@ -2,18 +2,8 @@