[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
This commit is contained in:
Xavier BOL (xbo)
2022-10-10 17:03:05 +02:00
parent 6a4e3dc030
commit da435cfe77
@@ -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);
}