Fix fields were not enabled again after an error response + refactor

This commit is contained in:
Gabriel Augendre 2018-06-16 09:58:24 +02:00
parent 6fe21af805
commit 979bc005d1

View file

@ -1,38 +1,65 @@
document.addEventListener("DOMContentLoaded", function (event) { document.addEventListener("DOMContentLoaded", function (event) {
var isbnButton = document.querySelector('#id_isbn_button'); var isbnButton = document.querySelector('#id_isbn_button');
var isbn = document.querySelector('#id_isbn'); var isbn = document.querySelector('#id_isbn');
isbnButton.addEventListener('click', function (event) { var title = document.querySelector('#id_title');
var authors = document.querySelector('#id_authors');
var year = document.querySelector('#id_publication_year');
var price = document.querySelector('#id_price');
var editor = document.querySelector('#id_editor');
var otherEditor = document.querySelector('#id_other_editor');
var spinner = document.querySelector('#id_isbn_spinner');
var feedback = document.querySelector('#id_isbn_invalid_feedback');
function enableFields() {
isbn.removeAttribute('disabled');
isbnButton.removeAttribute('disabled');
document.querySelector('#id_title').removeAttribute('disabled');
authors.removeAttribute('disabled');
year.removeAttribute('disabled');
price.removeAttribute('disabled');
editor.removeAttribute('disabled');
otherEditor.removeAttribute('disabled');
spinner.setAttribute('hidden', 'hidden');
}
function disableFields() {
isbn.setAttribute('disabled', 'disabled'); isbn.setAttribute('disabled', 'disabled');
isbnButton.setAttribute('disabled', 'disabled'); isbnButton.setAttribute('disabled', 'disabled');
document.querySelector('#id_title').setAttribute('disabled', 'disabled'); title.setAttribute('disabled', 'disabled');
document.querySelector('#id_authors').setAttribute('disabled', 'disabled'); authors.setAttribute('disabled', 'disabled');
document.querySelector('#id_publication_year').setAttribute('disabled', 'disabled'); year.setAttribute('disabled', 'disabled');
document.querySelector('#id_price').setAttribute('disabled', 'disabled'); price.setAttribute('disabled', 'disabled');
document.querySelector('#id_editor').setAttribute('disabled', 'disabled'); editor.setAttribute('disabled', 'disabled');
document.querySelector('#id_other_editor').setAttribute('disabled', 'disabled'); otherEditor.setAttribute('disabled', 'disabled');
document.querySelector('#id_isbn_spinner').removeAttribute('hidden'); spinner.removeAttribute('hidden');
}
isbnButton.addEventListener('click', function (event) {
disableFields();
$.get("/isbn_api/" + isbn.value, {}, function (data, status, xhr) { $.get("/isbn_api/" + isbn.value, {}, function (data, status, xhr) {
if (data.error) { if (data.error) {
isbn.classList.add('is-invalid'); isbn.classList.add('is-invalid');
isbn.classList.remove('is-valid'); isbn.classList.remove('is-valid');
document.querySelector('#id_isbn_invalid_feedback').style.display = 'block'; feedback.style.display = 'block';
document.querySelector('#id_isbn_invalid_feedback').textContent = data.error; feedback.textContent = data.error;
enableFields();
return; return;
} }
isbn.classList.remove('is-invalid'); isbn.classList.remove('is-invalid');
isbn.classList.add('is-valid'); isbn.classList.add('is-valid');
document.querySelector('#id_isbn_invalid_feedback').style.display = 'none'; feedback.style.display = 'none';
document.querySelector('#id_isbn_invalid_feedback').textContent = ''; feedback.textContent = '';
document.querySelector('#id_title').value = data.title; title.value = data.title;
document.querySelector('#id_title').classList.add('is-valid'); title.classList.add('is-valid');
document.querySelector('#id_authors').value = data.authors; authors.value = data.authors;
document.querySelector('#id_authors').classList.add('is-valid'); authors.classList.add('is-valid');
document.querySelector('#id_publication_year').value = data.year; year.value = data.year;
document.querySelector('#id_publication_year').classList.add('is-valid'); year.classList.add('is-valid');
document.querySelector('#id_price').value = data.price; price.value = data.price;
document.querySelector('#id_price').classList.add('is-valid'); price.classList.add('is-valid');
var editorValue = ""; var editorValue = "";
var editorIsOther = false; var editorIsOther = false;
@ -46,27 +73,18 @@ document.addEventListener("DOMContentLoaded", function (event) {
editorIsOther = false; editorIsOther = false;
} }
} }
document.querySelector('#id_editor').value = editorValue; editor.value = editorValue;
document.querySelector('#id_editor').classList.add('is-valid'); editor.classList.add('is-valid');
event = document.createEvent("HTMLEvents"); event = document.createEvent("HTMLEvents");
event.initEvent("change", true, true); event.initEvent("change", true, true);
event.eventName = "change"; event.eventName = "change";
document.querySelector('#id_editor').dispatchEvent(event); editor.dispatchEvent(event);
if (editorIsOther) { if (editorIsOther) {
document.querySelector('#id_other_editor').value = data.editor; otherEditor.value = data.editor;
document.querySelector('#id_other_editor').classList.add('is-valid'); otherEditor.classList.add('is-valid');
} }
enableFields();
isbn.removeAttribute('disabled');
isbnButton.removeAttribute('disabled');
document.querySelector('#id_title').removeAttribute('disabled');
document.querySelector('#id_authors').removeAttribute('disabled');
document.querySelector('#id_publication_year').removeAttribute('disabled');
document.querySelector('#id_price').removeAttribute('disabled');
document.querySelector('#id_editor').removeAttribute('disabled');
document.querySelector('#id_other_editor').removeAttribute('disabled');
document.querySelector('#id_isbn_spinner').setAttribute('hidden', 'hidden');
}); });
}); });
}); });