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

@@ -33,7 +33,7 @@ func GetAllPlayers(c *gin.Context) {
// GetPlayersByClanHTML GET /players_html
func GetPlayersByClanHTML(c *gin.Context) {
var players []models.Player
clanId := c.Request.URL.Query().Get("id")
clanId := c.Request.URL.Query().Get("clan_id")
if err := models.DB.Where("clan_id = ?", utils.StringToUint(clanId)).Find(&players).Error; err != nil {
return
}

Binary file not shown.

View File

@@ -69,4 +69,24 @@ function getSelectedClan(event) {
const clanList = document.querySelector(clanListId);
return clanList.options[clanList.selectedIndex];
}
}
function loadClans() {
const homeClanList = document.getElementById('home-clan');
const oppClanList = document.getElementById('opponent-clan');
fetch('/clans_html')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => {
homeClanList.innerHTML = data;
oppClanList.innerHTML = data;
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}

View File

@@ -3,7 +3,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Infantry Skill Calculator</title>
<script src="https://unpkg.com/htmx.org@1.9.6"></script>
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
<script src="../static/index.js"></script>
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

View File

@@ -6,7 +6,7 @@
</div>
<div class="col">
<div class="input-group input-group-lg mb-3">
<select class="form-select form-control border-secondary" id="home-clan" hx-get="/players_html" hx-target="#home-player-list" hx-vals='js:{"id": getSelectedClanId("home-clan")}'>
<select class="form-select form-control border-secondary" id="home-clan" hx-get="/players_html" hx-target="#home-player-list" hx-vals='js:{"clan_id": getSelectedClanId("home-clan")}'>
<option disabled selected value>Auswählen...</option>
<!-- Options will be loaded dynamically -->
</select>
@@ -26,7 +26,6 @@
<script lang="javascript">
document.addEventListener('DOMContentLoaded', function() {
setupClanButtons('home-clan', 'home-delete', 'home-edit');
htmx.ajax('GET', '/clans_html', {target: '#home-clan'});
});
</script>

View File

@@ -6,7 +6,7 @@
</div>
<div class="col">
<div class="input-group input-group-lg mb-3">
<select class="form-select form-control border-secondary" id="opponent-clan" hx-get="/players_html" hx-target="#opponent-player-list" hx-vals='js:{"id": getSelectedClanId("opponent-clan")}'>
<select class="form-select form-control border-secondary" id="opponent-clan" hx-get="/players_html" hx-target="#opponent-player-list" hx-vals='js:{"clan_id": getSelectedClanId("opponent-clan")}'>
<option disabled selected value>Auswählen...</option>
</select>
<button class="btn btn-lg btn-outline-secondary text-danger bg-secondary-subtle" type="button" id="opponent-delete" data-bs-toggle="modal" data-bs-list="#opponent-clan" data-bs-target="#deleteClanModal" disabled>
@@ -25,7 +25,6 @@
<script lang="javascript">
document.addEventListener('DOMContentLoaded', function() {
setupClanButtons('opponent-clan', 'opponent-delete', 'opponent-edit');
htmx.ajax('GET', '/clans_html', {target: '#opponent-clan'});
});
</script>

View File

@@ -40,11 +40,12 @@
<!-- Add Player Modal -->
{{ template "add_player" . }}
<!-- Delete Player Modal -->
{{ template "delete_player" . }}
<script lang="javascript">
document.addEventListener('DOMContentLoaded', function() {
loadClans();
});
</script>

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

View File

@@ -8,7 +8,7 @@
<li><a class="dropdown-item text-primary fs-5" href="#">
<i class="bi bi-person-gear fs-4 me-2"></i>Bearbeiten
</a></li>
<li><a class="dropdown-item text-danger fs-5" href="#" data-bs-toggle="modal" data-bs-list="#home-player-list" data-bs-id="%d" data-bs-target="#deletePlayerModal">
<li><a class="dropdown-item text-danger fs-5" href="#" data-bs-toggle="modal" data-bs-id="%d" data-bs-target="#deletePlayerModal">
<i class="bi bi-person-dash fs-4 me-2"></i>Löschen
</a></li>
</ul>