[FIX] mail: auto-close attachment box when no attachments

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) <tsm@odoo.com>
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
This commit is contained in:
Alexandre Kühn
2023-09-08 09:47:09 +00:00
parent d8d71c5ce6
commit 27919caad7
2 changed files with 41 additions and 24 deletions
@@ -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) {
@@ -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": `
<form>
<sheet></sheet>
<div class="oe_chatter">
<field name="message_ids" options="{'open_attachments': True}"/>
</div>
</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": `
<form>
<sheet></sheet>
<div class="oe_chatter">
<field name="message_ids" options="{'open_attachments': True}"/>
</div>
</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 });
});