2019-02-13 14:16:22 +01:00
|
|
|
|
import ProgressBar from './progress-bar';
|
2020-04-15 16:35:39 +02:00
|
|
|
|
import {
|
|
|
|
|
errorFromDirectUploadMessage,
|
2020-05-14 15:52:43 +02:00
|
|
|
|
ERROR_CODE_READ,
|
|
|
|
|
FAILURE_CLIENT
|
2020-04-15 16:35:39 +02:00
|
|
|
|
} from './file-upload-error';
|
2019-10-24 15:54:08 +02:00
|
|
|
|
import { fire } from '@utils';
|
2019-02-13 14:16:22 +01:00
|
|
|
|
|
|
|
|
|
const INITIALIZE_EVENT = 'direct-upload:initialize';
|
|
|
|
|
const START_EVENT = 'direct-upload:start';
|
|
|
|
|
const PROGRESS_EVENT = 'direct-upload:progress';
|
|
|
|
|
const ERROR_EVENT = 'direct-upload:error';
|
|
|
|
|
const END_EVENT = 'direct-upload:end';
|
|
|
|
|
|
2019-06-05 17:29:06 +02:00
|
|
|
|
function addUploadEventListener(type, handler) {
|
2020-04-30 15:42:29 +02:00
|
|
|
|
addEventListener(type, (event) => {
|
2019-06-05 17:29:06 +02:00
|
|
|
|
// Internet Explorer and Edge will sometime replay Javascript events
|
|
|
|
|
// that were dispatched just before a page navigation (!), but without
|
|
|
|
|
// the event payload.
|
|
|
|
|
//
|
|
|
|
|
// Ignore these replayed events.
|
|
|
|
|
const isEventValid = event && event.detail && event.detail.id != undefined;
|
|
|
|
|
if (!isEventValid) return;
|
|
|
|
|
|
|
|
|
|
handler(event);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addUploadEventListener(INITIALIZE_EVENT, ({ target, detail: { id, file } }) => {
|
2019-02-13 14:16:22 +01:00
|
|
|
|
ProgressBar.init(target, id, file);
|
|
|
|
|
});
|
|
|
|
|
|
2019-06-05 17:29:06 +02:00
|
|
|
|
addUploadEventListener(START_EVENT, ({ target, detail: { id } }) => {
|
2019-02-13 14:16:22 +01:00
|
|
|
|
ProgressBar.start(id);
|
2020-01-08 18:15:03 +01:00
|
|
|
|
// At the end of the upload, the form will be submitted again.
|
|
|
|
|
// Avoid the confirm dialog to be presented again then.
|
2019-03-06 11:40:09 +01:00
|
|
|
|
const button = target.form.querySelector('button.primary');
|
|
|
|
|
if (button) {
|
2019-05-14 17:12:27 +02:00
|
|
|
|
button.removeAttribute('data-confirm');
|
2019-03-06 11:40:09 +01:00
|
|
|
|
}
|
2019-02-13 14:16:22 +01:00
|
|
|
|
});
|
|
|
|
|
|
2019-06-05 17:29:06 +02:00
|
|
|
|
addUploadEventListener(PROGRESS_EVENT, ({ detail: { id, progress } }) => {
|
2019-02-13 14:16:22 +01:00
|
|
|
|
ProgressBar.progress(id, progress);
|
|
|
|
|
});
|
|
|
|
|
|
2020-04-30 15:42:29 +02:00
|
|
|
|
addUploadEventListener(ERROR_EVENT, (event) => {
|
2020-04-09 16:45:31 +02:00
|
|
|
|
let id = event.detail.id;
|
|
|
|
|
let errorMsg = event.detail.error;
|
|
|
|
|
|
2019-10-24 16:03:48 +02:00
|
|
|
|
// Display an error message
|
|
|
|
|
alert(
|
|
|
|
|
`Nous sommes désolés, une erreur s’est produite lors de l’envoi du fichier.
|
|
|
|
|
|
2020-04-09 16:45:31 +02:00
|
|
|
|
(${errorMsg})`
|
2019-10-24 16:03:48 +02:00
|
|
|
|
);
|
|
|
|
|
// Prevent ActiveStorage from displaying its own error message
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
2020-04-09 16:45:31 +02:00
|
|
|
|
ProgressBar.error(id, errorMsg);
|
|
|
|
|
|
2020-05-14 15:52:43 +02:00
|
|
|
|
// Report unexpected client errors to Sentry.
|
|
|
|
|
// (But ignore usual client errors, or errors we can monitor better on the server side.)
|
2020-04-09 16:45:31 +02:00
|
|
|
|
let error = errorFromDirectUploadMessage(errorMsg);
|
2020-05-14 15:52:43 +02:00
|
|
|
|
if (error.failureReason == FAILURE_CLIENT && error.code != ERROR_CODE_READ) {
|
2020-04-15 16:35:39 +02:00
|
|
|
|
fire(document, 'sentry:capture-exception', error);
|
|
|
|
|
}
|
2019-02-13 14:16:22 +01:00
|
|
|
|
});
|
|
|
|
|
|
2019-06-05 17:29:06 +02:00
|
|
|
|
addUploadEventListener(END_EVENT, ({ detail: { id } }) => {
|
2019-02-13 14:16:22 +01:00
|
|
|
|
ProgressBar.end(id);
|
|
|
|
|
});
|