From 27919caad7bd5778b25abea601778367ca1c3ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Wed, 6 Sep 2023 12:33:42 +0000 Subject: [PATCH] [FIX] mail: auto-close attachment box when no attachments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, when opening a record with some attachments and attachment box is open, switching to a record without any attachment shows the attachment box open. This is a problem because the attachment box is designed to only be open when there are some attachments. The toggler for showing attachment box is instead an "upload file" button when the record has no attachment. This means that the open attachment box in those records without any attachment cannot be toggled off. This commit fixes the issue by automatically closing attachment box when the record has no attachments. Task-3493374 closes odoo/odoo#134771 X-original-commit: ab678ea7618ca1aee5a6ab31b2de59c87fcce32c Signed-off-by: Matthieu Stockbauer (tsm) Signed-off-by: Alexandre Kühn (aku) --- addons/mail/static/src/core/web/chatter.js | 8 +++ .../static/tests/web/attachment_box_tests.js | 57 +++++++++++-------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/addons/mail/static/src/core/web/chatter.js b/addons/mail/static/src/core/web/chatter.js index 0294e22744f..b02cfc86457 100644 --- a/addons/mail/static/src/core/web/chatter.js +++ b/addons/mail/static/src/core/web/chatter.js @@ -165,6 +165,14 @@ export class Chatter extends Component { } } }); + useEffect( + () => { + if (this.attachments.length === 0) { + this.state.isAttachmentBoxOpened = false; + } + }, + () => [this.attachments] + ); useEffect( (opened) => { if (opened) { diff --git a/addons/mail/static/tests/web/attachment_box_tests.js b/addons/mail/static/tests/web/attachment_box_tests.js index 28489b58e19..ad41557278d 100644 --- a/addons/mail/static/tests/web/attachment_box_tests.js +++ b/addons/mail/static/tests/web/attachment_box_tests.js @@ -5,30 +5,6 @@ import { click, contains, scroll, start, startServer } from "@mail/../tests/help QUnit.module("attachment box"); -QUnit.test("base empty rendering", async () => { - const pyEnv = await startServer(); - const partnerId = pyEnv["res.partner"].create({}); - const views = { - "res.partner,false,form": ` -
- -
- -
-
- `, - }; - const { openView } = await start({ serverData: { views } }); - openView({ - res_id: partnerId, - res_model: "res.partner", - views: [[false, "form"]], - }); - await contains(".o-mail-AttachmentBox"); - await contains("button", { text: "Attach files" }); - await contains(".o-mail-Chatter .o-mail-AttachmentImage", { count: 0 }); -}); - QUnit.test("base non-empty rendering", async () => { const pyEnv = await startServer(); const partnerId = pyEnv["res.partner"].create({}); @@ -200,3 +176,36 @@ QUnit.test("attachment box should order attachments from newest to oldest", asyn await contains(".o-mail-AttachmentCard:eq(1) div", { text: "B.txt" }); await contains(".o-mail-AttachmentCard:eq(2) div", { text: "A.txt" }); }); + +QUnit.test("attachment box auto-closed on switch to record wih no attachments", async (assert) => { + const pyEnv = await startServer(); + const [partnerId_1, partnerId_2] = pyEnv["res.partner"].create([ + { display_name: "first partner" }, + { display_name: "second partner" }, + ]); + pyEnv["ir.attachment"].create([ + { + mimetype: "text/plain", + name: "Blah.txt", + res_id: partnerId_1, + res_model: "res.partner", + }, + ]); + const views = { + "res.partner,false,form": ` +
+ +
+ +
+
+ `, + }; + const { openFormView } = await start({ serverData: { views } }); + await openFormView("res.partner", partnerId_1, { + props: { resIds: [partnerId_1, partnerId_2] }, + }); + await contains(".o-mail-AttachmentBox"); + await click(".o_pager_next"); + await contains(".o-mail-AttachmentBox", { count: 0 }); +});