Add input validation and error handling for clan+player modals.
This commit is contained in:
@@ -9,18 +9,18 @@
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-floating mb-3">
|
||||
<input type="text" class="form-control form-control-lg" id="editClanName" placeholder="Clan-Name">
|
||||
<input type="text" class="form-control form-control-lg" id="editClanName" placeholder="Clan-Name" minlength="2" maxlength="30" required>
|
||||
<label for="editClanName">Clan-Name</label>
|
||||
</div>
|
||||
<div class="form-floating">
|
||||
<input type="text" class="form-control form-control-lg" id="editClanTag" placeholder="Clan-Tag">
|
||||
<input type="text" class="form-control form-control-lg" id="editClanTag" placeholder="Clan-Tag" minlength="2" maxlength="30" required>
|
||||
<label for="editClanTag">Clan-Tag</label>
|
||||
</div>
|
||||
<div class="form-check form-check-inline mt-3 fs-5">
|
||||
<input class="form-check-input" type="checkbox" id="editKeepUpdated" value="keepUpdated">
|
||||
<label class="form-check-label" for="editKeepUpdated">Immer aktuell halten</label>
|
||||
</div>
|
||||
<input type="hidden" id="editClanId" value="">
|
||||
<div class="error-message text-danger fs-5 badge" style="display: none;"></div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="submit" class="btn btn-lg btn-primary">Speichern</button>
|
||||
@@ -34,15 +34,101 @@
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const editClanModal = document.getElementById('editClanModal');
|
||||
const editClanModalBS = new bootstrap.Modal('#editClanModal');
|
||||
const submitButton = editClanModal.querySelector('button[name="submit"]');
|
||||
let submitClanHandler = null;
|
||||
let clanList = null;
|
||||
let otherClanList = null;
|
||||
let clanId = null;
|
||||
|
||||
const clanName = editClanModal.querySelector('#editClanName');
|
||||
const clanTag = editClanModal.querySelector('#editClanTag');
|
||||
const keepUpdated = editClanModal.querySelector('#editKeepUpdated');
|
||||
const errorDiv = editClanModal.querySelector('.error-message');
|
||||
|
||||
function validateInputs() {
|
||||
if (clanName.value.length < 2 || clanName.value.length > 30) {
|
||||
clanName.classList.add('is-invalid');
|
||||
return false;
|
||||
} else {
|
||||
clanName.classList.remove('is-invalid');
|
||||
}
|
||||
|
||||
if (clanTag.value.length < 2 || clanTag.value.length > 30) {
|
||||
clanTag.classList.add('is-invalid');
|
||||
return false;
|
||||
} else {
|
||||
clanTag.classList.remove('is-invalid');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function updateClanLists(clanList, otherClanList) {
|
||||
fetch('/clans_html')
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
return response.text();
|
||||
})
|
||||
.then(data => {
|
||||
const lastIndex = clanList.selectedIndex;
|
||||
const otherLastIndex = otherClanList.selectedIndex;
|
||||
|
||||
clanList.innerHTML = data;
|
||||
otherClanList.innerHTML = data;
|
||||
|
||||
clanList.selectedIndex = lastIndex;
|
||||
otherClanList.selectedIndex = otherLastIndex;
|
||||
})
|
||||
.catch(error => {
|
||||
alert('Fehler beim Aktualisieren der Clan-Liste: ' + error.message);
|
||||
});
|
||||
}
|
||||
|
||||
function createSubmitClanHandler() {
|
||||
return function submitClanHandler(e) {
|
||||
if (!validateInputs())
|
||||
return;
|
||||
|
||||
fetch("/clan/" + clanId, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({
|
||||
name: clanName.value,
|
||||
tag: clanTag.value,
|
||||
keep_updated: keepUpdated.checked
|
||||
}),
|
||||
headers: {
|
||||
"Content-type": "application/json; charset=UTF-8"
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
if (response.status === 400) {
|
||||
throw new Error('Clan oder Tag existiert bereits!');
|
||||
}
|
||||
throw new Error('Server error');
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
updateClanLists(clanList, otherClanList);
|
||||
|
||||
editClanModalBS.hide();
|
||||
})
|
||||
.catch((error) => {
|
||||
errorDiv.innerText = error.message;
|
||||
errorDiv.style.display = 'block';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (editClanModal) {
|
||||
editClanModal.addEventListener('show.bs.modal', event => {
|
||||
const [clanList, otherClanList] = getClanLists(event);
|
||||
submitClanHandler = createSubmitClanHandler();
|
||||
submitButton.addEventListener('click', submitClanHandler);
|
||||
|
||||
const clanName = editClanModal.querySelector('#editClanName');
|
||||
const clanTag = editClanModal.querySelector('#editClanTag');
|
||||
const keepUpdated = editClanModal.querySelector('#editKeepUpdated');
|
||||
const clanId = parseInt(clanList.value);
|
||||
const submitButton = editClanModal.querySelector('button[name="submit"]');
|
||||
[clanList, otherClanList] = getClanLists(event);
|
||||
clanId = parseInt(clanList.value);
|
||||
|
||||
fetch("/clan/" + clanId, {
|
||||
method: "GET",
|
||||
@@ -59,52 +145,20 @@
|
||||
clanTag.value = result['tag'];
|
||||
})
|
||||
.catch((error) => {
|
||||
throw new Error(error);
|
||||
alert('Fehler beim Laden des Clans: ' + error.message);
|
||||
});
|
||||
});
|
||||
|
||||
submitButton.addEventListener('click', function () {
|
||||
fetch("/clan/" + clanId, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({
|
||||
name: clanName.value,
|
||||
tag: clanTag.value,
|
||||
keep_updated: keepUpdated.checked
|
||||
}),
|
||||
headers: {
|
||||
"Content-type": "application/json; charset=UTF-8"
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
const lastIndex = clanList.selectedIndex;
|
||||
const otherLastIndex = otherClanList.selectedIndex;
|
||||
editClanModal.addEventListener('hidden.bs.modal', event => {
|
||||
submitButton.removeEventListener('click', submitClanHandler);
|
||||
|
||||
fetch('/clans_html')
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
return response.text();
|
||||
})
|
||||
.then(data => {
|
||||
clanList.innerHTML = data;
|
||||
otherClanList.innerHTML = data;
|
||||
|
||||
clanList.selectedIndex = lastIndex;
|
||||
otherClanList.selectedIndex = otherLastIndex;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('There has been a problem with your fetch operation:', error);
|
||||
});
|
||||
|
||||
editClanModalBS.hide();
|
||||
clanName.value = "";
|
||||
clanTag.value = "";
|
||||
keepUpdated.checked = false;
|
||||
})
|
||||
.catch((error) => {
|
||||
throw new Error(error)
|
||||
});
|
||||
}, { once: true });
|
||||
clanName.value = "";
|
||||
clanTag.value = "";
|
||||
errorDiv.innerText = "";
|
||||
errorDiv.style.display = 'none';
|
||||
keepUpdated.checked = false;
|
||||
clanName.classList.remove('is-invalid');
|
||||
clanTag.classList.remove('is-invalid');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user