From 38b0bd645c4999db6188d6046933ea7676e54dd5 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 8 Apr 2020 12:41:02 +0200 Subject: [PATCH 1/3] javascript: fix clicking on the icon of the auto-upload Retry button When clicking on the icon, `event target` would be the icon, not the button. However delegates configures `this` to be the requested event target. --- app/javascript/new_design/dossiers/auto-upload.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/javascript/new_design/dossiers/auto-upload.js b/app/javascript/new_design/dossiers/auto-upload.js index 627a61000..784fdd314 100644 --- a/app/javascript/new_design/dossiers/auto-upload.js +++ b/app/javascript/new_design/dossiers/auto-upload.js @@ -16,8 +16,8 @@ delegate('change', fileInputSelector, event => { }); const retryButtonSelector = `button.attachment-error-retry`; -delegate('click', retryButtonSelector, event => { - const inputSelector = event.target.dataset.inputTarget; +delegate('click', retryButtonSelector, function() { + const inputSelector = this.dataset.inputTarget; const input = document.querySelector(inputSelector); startUpload(input); }); From 444732b1173b64acdfdd03b7dc28c1a08f5dbd82 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 8 Apr 2020 12:44:31 +0200 Subject: [PATCH 2/3] javascript: don't allow to retry on direct upload 422 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the authenticity token is invalid, the creation of the blob before the direct upload returns a 422. In that case, the token will never become valid again, and it is useless to try again. Don’t show the "Retry" button in this case. NB: of course the real fix is to understand why the authenticity token is so often invalid – but this will be for later. --- .../new_design/dossiers/auto-upload-controller.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/javascript/new_design/dossiers/auto-upload-controller.js b/app/javascript/new_design/dossiers/auto-upload-controller.js index b26601468..ad4eec39c 100644 --- a/app/javascript/new_design/dossiers/auto-upload-controller.js +++ b/app/javascript/new_design/dossiers/auto-upload-controller.js @@ -93,7 +93,18 @@ export default class AutoUploadController { this.input.disabled = false; } + _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; + } + _messageFromError(error) { + let allowRetry = !this._isError422(error); + if ( error.xhr && error.xhr.status == 422 && @@ -104,13 +115,13 @@ export default class AutoUploadController { return { title: error.response.errors[0], description: '', - retry: false + retry: allowRetry }; } else { return { title: 'Une erreur s’est produite pendant l’envoi du fichier.', description: error.message || error.toString(), - retry: true + retry: allowRetry }; } } From 72d003b62c89d5ddf5a829ce6e28075bbeadd375 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 8 Apr 2020 16:53:46 +0200 Subject: [PATCH 3/3] javascript: fix missign argument to catch Some browers complain about this. --- app/views/users/feedbacks/create.js.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/users/feedbacks/create.js.erb b/app/views/users/feedbacks/create.js.erb index 3923caada..3e7422464 100644 --- a/app/views/users/feedbacks/create.js.erb +++ b/app/views/users/feedbacks/create.js.erb @@ -1,6 +1,6 @@ try { window.scroll({ top: 0, left: 0, behavior: 'smooth' }); -} catch { +} catch(e) { window.scroll(0, 0); } <%= remove_element('#user-satisfaction') %>