diff --git a/public/event-image-upload.js b/public/event-image-upload.js index f978e59..4968bfa 100644 --- a/public/event-image-upload.js +++ b/public/event-image-upload.js @@ -9,34 +9,32 @@ var fileInputFiles = fileInput.files; var file = fileInputFiles[0]; imagePreview.src && (imagePreview.src = ''); - getSignedRequest(file); spinner.classList.toggle('dn'); imagePreview.classList.add('dn'); - }; - - function getSignedRequest(file) { - file && - axios - .get('/sign-s3?file-name=' + file.name + '&file-type=' + file.type) - .then(function(res) { - uploadFile(file, res.data.signedRequest, res.data.url); - }) - .catch(function(err) { - fileErrorMessage.textContent = 'Could not upload file'; - }); - } - - function uploadFile(file, signedRequest, url) { - axios - .put(signedRequest, file) + getSignedRequest(file) .then(function(res) { + imagePreview.src = res.data.url; + hiddenFileInput.value = res.data.url; + return uploadFile(res.data.signedRequest, file); + }) + .then(function() { spinner.classList.toggle('dn'); imagePreview.classList.remove('dn'); - imagePreview.src = url; - hiddenFileInput.value = url; }) .catch(function(err) { + imagePreview.src = ''; + hiddenFileInput.value = ''; fileErrorMessage.textContent = 'Could not upload file'; }); - } + + function getSignedRequest(file) { + return axios.get( + '/sign-s3?file-name=' + file.name + '&file-type=' + file.type, + ); + } + + function uploadFile(signedRequest, file) { + return axios.put(signedRequest, file); + } + }; })(); diff --git a/public/place-image-upload.js b/public/place-image-upload.js index 87be092..16412d0 100644 --- a/public/place-image-upload.js +++ b/public/place-image-upload.js @@ -9,34 +9,32 @@ var fileInputFiles = fileInput.files; var file = fileInputFiles[0]; imagePreview.src && (imagePreview.src = ''); - getSignedRequest(file); spinner.classList.toggle('dn'); imagePreview.classList.add('dn'); - }; - - function getSignedRequest(file) { - file && - axios - .get('/sign-s3?file-name=' + file.name + '&file-type=' + file.type) - .then(function(res) { - uploadFile(file, res.data.signedRequest, res.data.url); - }) - .catch(function(err) { - fileErrorMessage.textContent = 'Could not upload file'; - }); - } - - function uploadFile(file, signedRequest, url) { - axios - .put(signedRequest, file) + getSignedRequest(file) .then(function(res) { + imagePreview.src = res.data.url; + hiddenFileInput.value = res.data.url; + return uploadFile(res.data.signedRequest, file); + }) + .then(function() { spinner.classList.toggle('dn'); imagePreview.classList.remove('dn'); - imagePreview.src = url; - hiddenFileInput.value = url; }) .catch(function(err) { + imagePreview.src = ''; + hiddenFileInput.value = ''; fileErrorMessage.textContent = 'Could not upload file'; }); - } + + function getSignedRequest(file) { + return axios.get( + '/sign-s3?file-name=' + file.name + '&file-type=' + file.type, + ); + } + + function uploadFile(signedRequest, file) { + return axios.put(signedRequest, file); + } + }; })();