demarches-normaliennes/app/javascript/shared/activestorage/ujs.js

59 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-02-13 14:16:22 +01:00
import ProgressBar from './progress-bar';
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';
function addUploadEventListener(type, handler) {
addEventListener(type, event => {
// 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);
});
addUploadEventListener(START_EVENT, ({ target, detail: { id } }) => {
2019-02-13 14:16:22 +01:00
ProgressBar.start(id);
// 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) {
button.removeAttribute('data-confirm');
2019-03-06 11:40:09 +01:00
}
2019-02-13 14:16:22 +01:00
});
addUploadEventListener(PROGRESS_EVENT, ({ detail: { id, progress } }) => {
2019-02-13 14:16:22 +01:00
ProgressBar.progress(id, progress);
});
addUploadEventListener(ERROR_EVENT, event => {
// Display an error message
alert(
`Nous sommes désolés, une erreur sest produite lors de lenvoi du fichier.
(${event.detail.error})`
);
// Prevent ActiveStorage from displaying its own error message
event.preventDefault();
ProgressBar.error(event.detail.id, event.detail.error);
fire(document, 'sentry:capture-exception', new Error(event.detail.error));
2019-02-13 14:16:22 +01:00
});
addUploadEventListener(END_EVENT, ({ detail: { id } }) => {
2019-02-13 14:16:22 +01:00
ProgressBar.end(id);
});