From da435cfe7713aff878d5d3a5aa191fa26c4dae23 Mon Sep 17 00:00:00 2001 From: "Xavier BOL (xbo)" Date: Mon, 10 Oct 2022 14:07:52 +0200 Subject: [PATCH] [REF] web: review FileInput to use it with different route Before this commit, the `FileInput` component can be used with another route only if the route has the same params than the default one. For instance, the portal chatter route does not have the same params name and so, the component cannot be used. This commit refactors the component to easily override the component to be able to use it in each case. task-2947516 X-original-commit: 8fdeea2e9918c8199724b177003f858716d7d5a4 --- .../static/src/core/file_input/file_input.js | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/addons/web/static/src/core/file_input/file_input.js b/addons/web/static/src/core/file_input/file_input.js index 2ebbb395222..80ddc25b6fd 100644 --- a/addons/web/static/src/core/file_input/file_input.js +++ b/addons/web/static/src/core/file_input/file_input.js @@ -34,6 +34,30 @@ export class FileInput extends Component { }); } + get httpParams() { + const { resId, resModel } = this.props; + const params = { + csrf_token: odoo.csrf_token, + ufile: [...this.fileInputRef.el.files], + }; + if (resModel) { + params.model = resModel; + } + if (resId !== undefined) { + params.id = resId; + } + return params; + } + + async uploadFiles(params) { + const fileData = await this.http.post(this.props.route, params, "text"); + const parsedFileData = JSON.parse(fileData); + if (parsedFileData.error) { + throw new Error(parsedFileData.error); + } + return parsedFileData; + } + //-------------------------------------------------------------------------- // Handlers //-------------------------------------------------------------------------- @@ -46,22 +70,7 @@ export class FileInput extends Component { * - resId: the id of the resModel target instance */ async onFileInputChange() { - const { resId, resModel, route } = this.props; - const params = { - csrf_token: odoo.csrf_token, - ufile: [...this.fileInputRef.el.files], - }; - if (resModel) { - params.model = resModel; - } - if (resId !== undefined) { - params.id = resId; - } - const fileData = await this.http.post(route, params, "text"); - const parsedFileData = JSON.parse(fileData); - if (parsedFileData.error) { - throw new Error(parsedFileData.error); - } + const parsedFileData = await this.uploadFiles(this.httpParams); this.props.onUpload(parsedFileData); }