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

@@ -57,26 +57,37 @@
"Content-type": "application/json; charset=UTF-8"
}
})
.then((response) => {
if (response.ok) {
const opt = document.createElement('option');
opt.innerText = "[" + clanTag.value + "]" + " " + clanName.value;
opt.value = response.json()['ID'];
.then(() => {
const lastIndex = otherClanList.selectedIndex;
clanList.appendChild(opt.cloneNode(true));
clanList.selectedIndex = clanList.children.length - 1;
clanList.dispatchEvent(new Event('change'));
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;
const lastIndex = otherClanList.selectedIndex;
otherClanList.appendChild(opt);
otherClanList.selectedIndex = lastIndex;
clanList.selectedIndex = clanList.children.length - 1;
otherClanList.selectedIndex = lastIndex;
addClanModalBS.hide();
clanName.value = "";
clanTag.value = "";
keepUpdated.checked = false;
} else
throw new Error(response.error)
clanList.dispatchEvent(new Event('change'));
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
addClanModalBS.hide();
clanName.value = "";
clanTag.value = "";
keepUpdated.checked = false;
})
.catch((error) => {
throw new Error(error)
});
})
});

View File

@@ -37,6 +37,8 @@
const clanName = addPlayerModal.querySelector('#playerClanName');
clanName.value = selectedClan.innerText;
const clanId = parseInt(selectedClan.value);
const homeClanListIndex = document.getElementById('home-clan').selectedIndex;
const oppClanListIndex = document.getElementById('opponent-clan').selectedIndex;
const submitButton = addPlayerModal.querySelector('button[name="submit"]');
submitButton.addEventListener('click', function (e) {
@@ -53,18 +55,12 @@
"Content-type": "application/json; charset=UTF-8"
}
})
.then((response) => response.json())
.then((json) => {
const opt = document.createElement('option');
opt.innerText = playerName.value;
opt.value = json['ID'];
playerList.appendChild(opt.cloneNode(true));
playerList.selectedIndex = playerList.children.length - 1;
playerList.dispatchEvent(new Event('change'));
if (document.getElementById('home-clan').selectedIndex === document.getElementById('opponent-clan').selectedIndex)
otherPlayerList.appendChild(opt);
.then(() => {
const sameClan = homeClanListIndex === oppClanListIndex;
if (playerList.id === 'home-player-list' || sameClan)
htmx.ajax('GET', '/players_html', {target: '#home-player-list', values: {"clan_id": getSelectedClanId("home-clan")}});
if (playerList.id === 'opponent-player-list' || sameClan)
htmx.ajax('GET', '/players_html', {target: '#opponent-player-list', values: {"clan_id": getSelectedClanId("opponent-clan")}});
addPlayerModalBS.hide();
playerName.value = "";
@@ -72,7 +68,7 @@
}).catch((error) => {
throw new Error(error)
});
})
}, { once: true });
});
}
});

View File

@@ -45,24 +45,29 @@
"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(() => {
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;
const lastOtherIndex = otherClanList.selectedIndex;
otherClanList.children.item(clanIndex).remove();
if (lastOtherIndex === clanIndex) {
otherClanList.selectedIndex = 0;
clanList.dispatchEvent(new Event('change'));
otherClanList.dispatchEvent(new Event('change'));
}
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
deleteClanModalBS.hide();
} else
throw new Error(response.error)
deleteClanModalBS.hide();
})
.catch((error) => {
throw new Error(error)
});
})
});

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)
});
})
});

View File

@@ -81,18 +81,35 @@
"Content-type": "application/json; charset=UTF-8"
}
})
.then((response) => {
if (response.ok) {
const newName = "[" + clanTag.value + "]" + " " + clanName.value;
selectedClanItem.innerText = newName;
otherSelClanItem.innerText = newName;
.then(() => {
const lastIndex = clanList.selectedIndex;
const otherLastIndex = otherClanList.selectedIndex;
editClanModalBS.hide();
clanName.value = "";
clanTag.value = "";
keepUpdated.checked = false;
} else
throw new Error(response.error)
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)
});
})
});