Skip to content

Commit

Permalink
Move flashing error message to JS util function
Browse files Browse the repository at this point in the history
A number of endpoints (i.e. tool submissions) have a standard set of error fields they might return, so this pulls that all together.  Also means tool submissions now scroll to the top when an error message appears, which is nice.
  • Loading branch information
vlagrassa committed Apr 29, 2024
1 parent 76a5696 commit 2a7f27e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,7 @@
.fail((error) => {
console.error(error)
$('#{{ offcanvas_id }} .btn-close').click()
$('html').animate({
scrollTop: $('body').offset().top
}, 100);
if (error.responseJSON && error.responseJSON.message) {
$('.alert').remove()
flash_message(error.responseJSON.message)
}
flashErrorResponse(error.responseJSON, 'There was a problem retrieving this trait. Please try again later.')
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,7 @@ <h3 class="h5 text-dark">Full Description</h3>
.fail((error) => {
console.error(error)
$('#{{ offcanvas_id }} .btn-close').click()
$('html').animate({
scrollTop: $('body').offset().top
}, 100);
if (error.responseJSON && error.responseJSON.message) {
$('.alert').remove()
flash_message(error.responseJSON.message)
}
flashErrorResponse(error.responseJSON, 'There was a problem retrieving this trait. Please try again later.')
})
})

Expand Down
21 changes: 21 additions & 0 deletions src/modules/site-v2/templates/_scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,27 @@ function flash_message(message, full_msg_link=null, full_msg_body=null) {
}


function flashErrorResponse(responseJSON, backupMessage, scrollToTop = true) {

// Optionally scroll to the top of the page to highlight the error message
if (scrollToTop) {
$('html').animate({
scrollTop: $('body').offset().top,
}, 100);
}

// If the response JSON contains a message, flash all the details it has
if (responseJSON && responseJSON.message) {
flash_message(responseJSON.message, responseJSON.full_msg_link, responseJSON.full_msg_body);
}

// Otherwise, flash a backup message
else {
flash_message(backupMessage);
}
}


/* Format a date as YYYY-MM-DD.
*/
function formatDate(d) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ <h2 class="mb-4 text-center">Example Data</h2>
{% from "_scripts/submit-job.js" import def_submit_job %}
{% from "_includes/macros.html" import update_species_field %}
<script>
{% include '_scripts/utils.js' %} {#/* defines: flash_message */#}
{% include '_scripts/utils.js' %} {#/* defines: flashErrorResponse */#}

{{ def_submit_job('genetic_mapping', true) }}

Expand Down Expand Up @@ -173,10 +173,7 @@ <h2 class="mb-4 text-center">Example Data</h2>
})
// If file validation fails: flash the error, reset the file input, and re-enable the submit button
.fail((error) => {
const resp = error.responseJSON
if (resp && resp.message) {
flash_message(resp.message, resp.full_msg_link, resp.full_msg_body);
}
flashErrorResponse(error.responseJSON, 'There was a problem with your submission. Please try again later.');
document.getElementById('{{ form.file.id }}').value = '';
enableButton();
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ <h2 class="mb-4 text-center">Example Data</h2>
{% from "_scripts/submit-job.js" import def_submit_job %}
{% from "_includes/macros.html" import update_species_field %}
<script>
{% include '_scripts/utils.js' %} {#/* defines: flash_message */#}
{% include '_scripts/utils.js' %} {#/* defines: flashErrorResponse */#}

{{ def_submit_job('heritability_calculator', true) }}

Expand Down Expand Up @@ -171,10 +171,7 @@ <h2 class="mb-4 text-center">Example Data</h2>
})
// If file validation fails: flash the error, reset the file input, and re-enable the submit button
.fail((error) => {
const resp = error.responseJSON
if (resp && resp.message) {
flash_message(resp.message, resp.full_msg_link, resp.full_msg_body);
}
flashErrorResponse(error.responseJSON, 'There was a problem with your submission. Please try again later.');
document.getElementById('{{ form.file.id }}').value = '';
enableButton();
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ <h5 class="modal-title" id="confirmationModalLabel">Thank you</h5>
.fail((error) => {
// If an error message was received, display it, otherwise use a fallback message
console.error(error);
const resp = error.responseJSON
if (resp && resp.message && false) {
flash_message(resp.message, resp.full_msg_link, resp.full_msg_body);
} else{
flash_message(`Failed to retrieve metadata for trait ${trait}`);
}
flashErrorResponse(error.responseJSON, `Failed to retrieve metadata for trait ${trait}.`)

// Close the modal so the user can see the error message
$('#phenotypeDBModal').modal('hide');
Expand Down Expand Up @@ -249,10 +244,7 @@ <h5 class="modal-title" id="confirmationModalLabel">Thank you</h5>
})
// If validation fails, just flash the error
.fail((error) => {
const resp = error.responseJSON
if (resp && resp.message) {
flash_message(resp.message, resp.full_msg_link, resp.full_msg_body);
}
flashErrorResponse(error.responseJSON, 'There was a problem with your submission. Please try again later.')
})
}
</script>
Expand Down

0 comments on commit 2a7f27e

Please sign in to comment.