From e4862f26f44657d901ea922653984d2c54819f75 Mon Sep 17 00:00:00 2001 From: FrancoisGe Date: Wed, 19 Oct 2022 08:56:47 +0000 Subject: [PATCH] [FIX] web: reload record when you click on attach_document MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The goal of this commit is to restore the behaviour of the legacy attach_document widget. How to reproduce : - clicking on the attach_document button and selecting a file In legacy: We have a full reload of the view (Render and reload the record). Before this commit: Render the view without reloading the record. After this commit: We reload the model which causes a rendering of the view and a reload of the record. Why do we have to reload the record ? If the action linked to the attach_document button modifies the record, we want this modification to be applied in the view and not only on the server side. To do this, we need to reload the record data to ensure that all the changes are displayed in the view. closes odoo/odoo#103891 X-original-commit: 82d4f0cd2f0265393cf66cd7be1a0a714584400c Signed-off-by: Jorge Pinna Puissant (jpp) Signed-off-by: Georis François (fge) --- .../attach_document/attach_document.js | 5 +- .../views/widgets/attach_document_tests.js | 64 ++++++++++++++++++- 2 files changed, 64 insertions(+), 5 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 7ce0b9ca480..975ee5d83df 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 @@ -14,12 +14,13 @@ class AttachDocumentWidget extends Component { } async onFileUploaded(files) { - const { action } = this.props; + const { action, record } = this.props; if (action) { - const { model, resId, resModel } = this.props.record; + const { model, resId, resModel } = record; await this.orm.call(resModel, action, [resId], { attachment_ids: files.map((file) => file.id), }); + await record.load(); model.notify(); } } diff --git a/addons/web/static/tests/views/widgets/attach_document_tests.js b/addons/web/static/tests/views/widgets/attach_document_tests.js index c76eb9a9d34..892e8df4741 100644 --- a/addons/web/static/tests/views/widgets/attach_document_tests.js +++ b/addons/web/static/tests/views/widgets/attach_document_tests.js @@ -53,17 +53,19 @@ QUnit.module("Widgets", (hooks) => { serverData, resId: 1, mockRPC(route, args) { + assert.step(args.method); if (args.method === "my_action") { - assert.step("my_action"); assert.deepEqual(args.model, "partner"); assert.deepEqual(args.args, [1]); assert.deepEqual(args.kwargs.attachment_ids, [5, 2]); return true; } if (args.method === "write") { - assert.step("write"); assert.deepEqual(args.args[1], { display_name: "yop" }); } + if (args.method === "read") { + assert.deepEqual(args.args[0], [1]); + } }, arch: `
@@ -71,6 +73,7 @@ QUnit.module("Widgets", (hooks) => { `, }); + assert.verifySteps(["get_views", "read"]); await editInput(target, "[name='display_name'] input", "yop"); await click(target, ".o_attach_document"); @@ -81,6 +84,61 @@ QUnit.module("Widgets", (hooks) => { {}, { skipVisibilityCheck: true } ); - assert.verifySteps(["write", "post", "my_action"]); + assert.verifySteps(["write", "read", "post", "my_action", "read"]); }); + + QUnit.test( + "attach document widget calls action with attachment ids on a new record", + async function (assert) { + serviceRegistry.add("http", { + start: () => ({ + post: (route, params) => { + assert.step("post"); + assert.strictEqual(route, "/web/binary/upload_attachment"); + assert.strictEqual(params.model, "partner"); + assert.strictEqual(params.id, 2); + return '[{ "id": 5 }, { "id": 2 }]'; + }, + }), + }); + + await makeView({ + type: "form", + resModel: "partner", + serverData, + mockRPC(route, args) { + assert.step(args.method); + if (args.method === "my_action") { + assert.deepEqual(args.model, "partner"); + assert.deepEqual(args.args, [2]); + assert.deepEqual(args.kwargs.attachment_ids, [5, 2]); + return true; + } + if (args.method === "create") { + assert.deepEqual(args.args[0], { display_name: "yop" }); + } + if (args.method === "read") { + assert.deepEqual(args.args[0], [2]); + } + }, + arch: ` +
+ + + `, + }); + assert.verifySteps(["get_views", "onchange"]); + + await editInput(target, "[name='display_name'] input", "yop"); + await click(target, ".o_attach_document"); + await triggerEvent( + target, + ".o_file_input input", + "change", + {}, + { skipVisibilityCheck: true } + ); + assert.verifySteps(["create", "read", "post", "my_action", "read"]); + } + ); });