Optimize all modals (fetching). Implement add-player modal.

This commit is contained in:
MaxJa4
2024-01-16 20:19:40 +01:00
parent 8c4ed63359
commit fc084108ac
13 changed files with 134 additions and 91 deletions

View File

@@ -26,44 +26,39 @@
const deletePlayerModalBS = new bootstrap.Modal('#deletePlayerModal');
if (deletePlayerModal) {
deletePlayerModal.addEventListener('show.bs.modal', event => {
return;
const [clanList, otherClanList] = getClanLists(event);
const button = event.relatedTarget;
const playerId = parseInt(button.getAttribute('data-bs-id'));
const selectedClan = clanList.options[clanList.selectedIndex].text;
const modalBodyInput = deletePlayerModal.querySelector('#clan');
modalBodyInput.innerText = selectedClan;
const selectedPlayer = button.closest('.input-group').querySelector('span').innerText;
const modalBodyInput = deletePlayerModal.querySelector('#player');
modalBodyInput.innerText = selectedPlayer;
const playerListId = button.closest('ul').parentElement.parentElement.id;
const homeClanListIndex = document.getElementById('home-clan').selectedIndex;
const oppClanListIndex = document.getElementById('opponent-clan').selectedIndex;
const submitButton = deletePlayerModal.querySelector('button[name="submit"]');
submitButton.addEventListener('click', function (e) {
e.preventDefault();
submitButton.onclick = function () {}
const clanId = parseInt(clanList.value);
fetch("/clan/" + clanId, {
fetch("/player/" + playerId, {
method: "DELETE",
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then((response) => {
if (response.ok) {
const clanToRemove = clanList.children[clanList.selectedIndex];
const clanIndex = clanToRemove.index;
clanList.removeChild(clanToRemove);
clanList.selectedIndex = 0;
clanList.dispatchEvent(new Event('change'));
.then(() => {
const sameClan = homeClanListIndex === oppClanListIndex;
if (playerListId === 'home-player-list' || sameClan)
htmx.ajax('GET', '/players_html', {target: '#home-player-list', values: {"clan_id": getSelectedClanId("home-clan")}});
if (playerListId === 'opponent-player-list' || sameClan)
htmx.ajax('GET', '/players_html', {target: '#opponent-player-list', values: {"clan_id": getSelectedClanId("opponent-clan")}});
const lastOtherIndex = otherClanList.selectedIndex;
otherClanList.children.item(clanIndex).remove();
if (lastOtherIndex === clanIndex) {
otherClanList.selectedIndex = 0;
otherClanList.dispatchEvent(new Event('change'));
}
deletePlayerModalBS.hide();
} else
throw new Error(response.error)
deletePlayerModalBS.hide();
})
.catch((error) => {
throw new Error(error)
});
})
});