From e8567dadbdaaec6e5be8f66f12e2b02da321f999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Tue, 4 Apr 2023 16:13:27 +0000 Subject: [PATCH] [FIX] web: upload same file twice in discuss composer Before this commit, when clicking on "Attach files" in discuss composer, then removing it, it was not possible to upload the same file again. This happens because file input of file uploader relies on `change` from the input. The `input.value` contains the filepath, so if we upload the exact same file without clearing the value, the value will be unchanged and it won't trigger uploading as a result. This commit fixes the issue by clearing the input just after the file has been uploaded, so that we can upload the same file. Cannot reproduce bug in QUnit tests because `inputFile` helper programmatically always trigger "change" on input file. closes odoo/odoo#117704 Signed-off-by: Luca Vitali --- .../static/src/views/fields/file_handler.js | 1 + .../tests/views/fields/image_field_tests.js | 40 ++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/addons/web/static/src/views/fields/file_handler.js b/addons/web/static/src/views/fields/file_handler.js index 3a1685978d3..ebaed17ceb0 100644 --- a/addons/web/static/src/views/fields/file_handler.js +++ b/addons/web/static/src/views/fields/file_handler.js @@ -62,6 +62,7 @@ export class FileUploader extends Component { }); this.state.isUploading = false; } + ev.target.value = null; if (this.props.multiUpload && this.props.onUploadComplete) { this.props.onUploadComplete({}); } diff --git a/addons/web/static/tests/views/fields/image_field_tests.js b/addons/web/static/tests/views/fields/image_field_tests.js index 17120822600..2af8034ba52 100644 --- a/addons/web/static/tests/views/fields/image_field_tests.js +++ b/addons/web/static/tests/views/fields/image_field_tests.js @@ -399,7 +399,12 @@ QUnit.module("Fields", (hooks) => { "if only the width is set to 0, the width attribute is not set on the img" ); assert.deepEqual( - [imgs[1].style.width, imgs[1].style.maxWidth, imgs[1].style.height, imgs[1].style.maxHeight], + [ + imgs[1].style.width, + imgs[1].style.maxWidth, + imgs[1].style.height, + imgs[1].style.maxHeight, + ], ["auto", "100%", "", "50px"], "the image should correctly set its attributes" ); @@ -410,7 +415,12 @@ QUnit.module("Fields", (hooks) => { "if only the height is set to 0, the height attribute is not set on the img" ); assert.deepEqual( - [imgs[2].style.width, imgs[2].style.maxWidth, imgs[2].style.height, imgs[2].style.maxHeight], + [ + imgs[2].style.width, + imgs[2].style.maxWidth, + imgs[2].style.height, + imgs[2].style.maxHeight, + ], ["", "50px", "auto", "100%"], "the image should correctly set its attributes" ); @@ -627,17 +637,35 @@ QUnit.module("Fields", (hooks) => { await nextTick(); } - assert.strictEqual(target.querySelector("input[type=file]").files.length, 0, "there shouldn't be any file"); + assert.strictEqual( + target.querySelector("img[data-alt='Binary file']").dataset.src, + "/web/static/img/placeholder.png", + "image field should not be set" + ); await setFiles(); - assert.strictEqual(target.querySelector("input[type=file]").files.length, 1, "there should be a single file"); + assert.ok( + target + .querySelector("img[data-alt='Binary file']") + .dataset.src.includes("data:image/png;base64"), + "image field should be set" + ); await clickSave(target); await click(target, ".o_form_button_create"); - assert.strictEqual(target.querySelector("input[type=file]").files.length, 0, "there shouldn't be any file"); + assert.strictEqual( + target.querySelector("img[data-alt='Binary file']").dataset.src, + "/web/static/img/placeholder.png", + "image field should be reset" + ); await setFiles(); - assert.strictEqual(target.querySelector("input[type=file]").files.length, 1, "there should be a single file"); + assert.ok( + target + .querySelector("img[data-alt='Binary file']") + .dataset.src.includes("data:image/png;base64"), + "image field should be set" + ); }); QUnit.test("unique in url doesn't change on onchange", async (assert) => {