Skip to content

Commit

Permalink
use composition for calling functions
Browse files Browse the repository at this point in the history
rel #86
  • Loading branch information
Karyum committed Jan 29, 2018
1 parent fa34bfb commit 857ac6f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
40 changes: 19 additions & 21 deletions public/event-image-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
})();
40 changes: 19 additions & 21 deletions public/place-image-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
})();

0 comments on commit 857ac6f

Please sign in to comment.