2020-03-30 15:34:56 +02:00
|
|
|
|
import Uploader from '../../shared/activestorage/uploader';
|
2020-04-15 16:20:31 +02:00
|
|
|
|
import { show, hide, toggle } from '@utils';
|
2020-03-30 15:34:56 +02:00
|
|
|
|
|
|
|
|
|
// Given a file input in a champ with a selected file, upload a file,
|
|
|
|
|
// then attach it to the dossier.
|
|
|
|
|
//
|
|
|
|
|
// On success, the champ is replaced by an HTML fragment describing the attachment.
|
|
|
|
|
// On error, a error message is displayed above the input.
|
|
|
|
|
export default class AutoUploadController {
|
|
|
|
|
constructor(input, file) {
|
|
|
|
|
this.input = input;
|
|
|
|
|
this.file = file;
|
2020-04-15 16:20:31 +02:00
|
|
|
|
this.uploader = new Uploader(
|
|
|
|
|
input,
|
|
|
|
|
file,
|
|
|
|
|
input.dataset.directUploadUrl,
|
|
|
|
|
input.dataset.autoAttachUrl
|
|
|
|
|
);
|
2020-03-30 15:34:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
|
try {
|
|
|
|
|
this._begin();
|
2020-04-15 16:20:31 +02:00
|
|
|
|
await this.uploader.start();
|
|
|
|
|
this._succeeded();
|
2020-03-30 15:34:56 +02:00
|
|
|
|
} catch (error) {
|
|
|
|
|
this._failed(error);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
this._done();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_begin() {
|
|
|
|
|
this.input.disabled = true;
|
|
|
|
|
this._hideErrorMessage();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 16:20:31 +02:00
|
|
|
|
_succeeded() {
|
|
|
|
|
this.input.value = null;
|
2020-03-30 15:34:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_failed(error) {
|
|
|
|
|
if (!document.body.contains(this.input)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 16:20:31 +02:00
|
|
|
|
this.uploader.progressBar.destroy();
|
2020-03-30 15:34:56 +02:00
|
|
|
|
|
2020-04-15 16:20:31 +02:00
|
|
|
|
let message = this._messageFromError(error);
|
|
|
|
|
this._displayErrorMessage(message);
|
2020-03-30 15:34:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_done() {
|
|
|
|
|
this.input.disabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 12:44:31 +02:00
|
|
|
|
_isError422(error) {
|
|
|
|
|
// Ajax errors have an xhr attribute
|
|
|
|
|
if (error && error.xhr && error.xhr.status == 422) return true;
|
|
|
|
|
// Rails DirectUpload errors are returned as a String, e.g. 'Error creating Blob for "Demain.txt". Status: 422'
|
|
|
|
|
if (error && error.toString().includes('422')) return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 15:34:56 +02:00
|
|
|
|
_messageFromError(error) {
|
2020-04-08 12:44:31 +02:00
|
|
|
|
let allowRetry = !this._isError422(error);
|
|
|
|
|
|
2020-03-30 15:34:56 +02:00
|
|
|
|
if (
|
|
|
|
|
error.xhr &&
|
|
|
|
|
error.xhr.status == 422 &&
|
|
|
|
|
error.response &&
|
|
|
|
|
error.response.errors &&
|
|
|
|
|
error.response.errors[0]
|
|
|
|
|
) {
|
|
|
|
|
return {
|
|
|
|
|
title: error.response.errors[0],
|
|
|
|
|
description: '',
|
2020-04-08 12:44:31 +02:00
|
|
|
|
retry: allowRetry
|
2020-03-30 15:34:56 +02:00
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
title: 'Une erreur s’est produite pendant l’envoi du fichier.',
|
|
|
|
|
description: error.message || error.toString(),
|
2020-04-08 12:44:31 +02:00
|
|
|
|
retry: allowRetry
|
2020-03-30 15:34:56 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 16:20:31 +02:00
|
|
|
|
_displayErrorMessage(message) {
|
2020-03-30 15:34:56 +02:00
|
|
|
|
let errorNode = this.input.parentElement.querySelector('.attachment-error');
|
|
|
|
|
if (errorNode) {
|
|
|
|
|
show(errorNode);
|
|
|
|
|
errorNode.querySelector('.attachment-error-title').textContent =
|
|
|
|
|
message.title || '';
|
|
|
|
|
errorNode.querySelector('.attachment-error-description').textContent =
|
|
|
|
|
message.description || '';
|
|
|
|
|
toggle(errorNode.querySelector('.attachment-error-retry'), message.retry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_hideErrorMessage() {
|
|
|
|
|
let errorElement = this.input.parentElement.querySelector(
|
|
|
|
|
'.attachment-error'
|
|
|
|
|
);
|
|
|
|
|
if (errorElement) {
|
|
|
|
|
hide(errorElement);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|