Optimize all modals (fetching). Implement add-player modal.
This commit is contained in:
@@ -33,7 +33,7 @@ func GetAllPlayers(c *gin.Context) {
|
|||||||
// GetPlayersByClanHTML GET /players_html
|
// GetPlayersByClanHTML GET /players_html
|
||||||
func GetPlayersByClanHTML(c *gin.Context) {
|
func GetPlayersByClanHTML(c *gin.Context) {
|
||||||
var players []models.Player
|
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 {
|
if err := models.DB.Where("clan_id = ?", utils.StringToUint(clanId)).Find(&players).Error; err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
isc_data.db
BIN
isc_data.db
Binary file not shown.
@@ -69,4 +69,24 @@ function getSelectedClan(event) {
|
|||||||
const clanList = document.querySelector(clanListId);
|
const clanList = document.querySelector(clanListId);
|
||||||
|
|
||||||
return clanList.options[clanList.selectedIndex];
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Infantry Skill Calculator</title>
|
<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>
|
<script src="../static/index.js"></script>
|
||||||
<!-- Bootstrap 5 CSS -->
|
<!-- Bootstrap 5 CSS -->
|
||||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<div class="input-group input-group-lg mb-3">
|
<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>
|
<option disabled selected value>Auswählen...</option>
|
||||||
<!-- Options will be loaded dynamically -->
|
<!-- Options will be loaded dynamically -->
|
||||||
</select>
|
</select>
|
||||||
@@ -26,7 +26,6 @@
|
|||||||
<script lang="javascript">
|
<script lang="javascript">
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
setupClanButtons('home-clan', 'home-delete', 'home-edit');
|
setupClanButtons('home-clan', 'home-delete', 'home-edit');
|
||||||
htmx.ajax('GET', '/clans_html', {target: '#home-clan'});
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<div class="input-group input-group-lg mb-3">
|
<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>
|
<option disabled selected value>Auswählen...</option>
|
||||||
</select>
|
</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>
|
<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">
|
<script lang="javascript">
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
setupClanButtons('opponent-clan', 'opponent-delete', 'opponent-edit');
|
setupClanButtons('opponent-clan', 'opponent-delete', 'opponent-edit');
|
||||||
htmx.ajax('GET', '/clans_html', {target: '#opponent-clan'});
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -40,11 +40,12 @@
|
|||||||
|
|
||||||
<!-- Add Player Modal -->
|
<!-- Add Player Modal -->
|
||||||
{{ template "add_player" . }}
|
{{ template "add_player" . }}
|
||||||
|
<!-- Delete Player Modal -->
|
||||||
|
{{ template "delete_player" . }}
|
||||||
|
|
||||||
<script lang="javascript">
|
<script lang="javascript">
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
loadClans();
|
||||||
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -57,26 +57,37 @@
|
|||||||
"Content-type": "application/json; charset=UTF-8"
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then(() => {
|
||||||
if (response.ok) {
|
const lastIndex = otherClanList.selectedIndex;
|
||||||
const opt = document.createElement('option');
|
|
||||||
opt.innerText = "[" + clanTag.value + "]" + " " + clanName.value;
|
|
||||||
opt.value = response.json()['ID'];
|
|
||||||
|
|
||||||
clanList.appendChild(opt.cloneNode(true));
|
fetch('/clans_html')
|
||||||
clanList.selectedIndex = clanList.children.length - 1;
|
.then(response => {
|
||||||
clanList.dispatchEvent(new Event('change'));
|
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;
|
clanList.selectedIndex = clanList.children.length - 1;
|
||||||
otherClanList.appendChild(opt);
|
otherClanList.selectedIndex = lastIndex;
|
||||||
otherClanList.selectedIndex = lastIndex;
|
|
||||||
|
|
||||||
addClanModalBS.hide();
|
clanList.dispatchEvent(new Event('change'));
|
||||||
clanName.value = "";
|
})
|
||||||
clanTag.value = "";
|
.catch(error => {
|
||||||
keepUpdated.checked = false;
|
console.error('There has been a problem with your fetch operation:', error);
|
||||||
} else
|
});
|
||||||
throw new Error(response.error)
|
|
||||||
|
|
||||||
|
addClanModalBS.hide();
|
||||||
|
clanName.value = "";
|
||||||
|
clanTag.value = "";
|
||||||
|
keepUpdated.checked = false;
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
throw new Error(error)
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -37,6 +37,8 @@
|
|||||||
const clanName = addPlayerModal.querySelector('#playerClanName');
|
const clanName = addPlayerModal.querySelector('#playerClanName');
|
||||||
clanName.value = selectedClan.innerText;
|
clanName.value = selectedClan.innerText;
|
||||||
const clanId = parseInt(selectedClan.value);
|
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"]');
|
const submitButton = addPlayerModal.querySelector('button[name="submit"]');
|
||||||
submitButton.addEventListener('click', function (e) {
|
submitButton.addEventListener('click', function (e) {
|
||||||
@@ -53,18 +55,12 @@
|
|||||||
"Content-type": "application/json; charset=UTF-8"
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then((response) => response.json())
|
.then(() => {
|
||||||
.then((json) => {
|
const sameClan = homeClanListIndex === oppClanListIndex;
|
||||||
const opt = document.createElement('option');
|
if (playerList.id === 'home-player-list' || sameClan)
|
||||||
opt.innerText = playerName.value;
|
htmx.ajax('GET', '/players_html', {target: '#home-player-list', values: {"clan_id": getSelectedClanId("home-clan")}});
|
||||||
opt.value = json['ID'];
|
if (playerList.id === 'opponent-player-list' || sameClan)
|
||||||
|
htmx.ajax('GET', '/players_html', {target: '#opponent-player-list', values: {"clan_id": getSelectedClanId("opponent-clan")}});
|
||||||
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);
|
|
||||||
|
|
||||||
addPlayerModalBS.hide();
|
addPlayerModalBS.hide();
|
||||||
playerName.value = "";
|
playerName.value = "";
|
||||||
@@ -72,7 +68,7 @@
|
|||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
throw new Error(error)
|
throw new Error(error)
|
||||||
});
|
});
|
||||||
})
|
}, { once: true });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -45,24 +45,29 @@
|
|||||||
"Content-type": "application/json; charset=UTF-8"
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then(() => {
|
||||||
if (response.ok) {
|
fetch('/clans_html')
|
||||||
const clanToRemove = clanList.children[clanList.selectedIndex];
|
.then(response => {
|
||||||
const clanIndex = clanToRemove.index;
|
if (!response.ok) {
|
||||||
clanList.removeChild(clanToRemove);
|
throw new Error('Network response was not ok');
|
||||||
clanList.selectedIndex = 0;
|
}
|
||||||
clanList.dispatchEvent(new Event('change'));
|
return response.text();
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
clanList.innerHTML = data;
|
||||||
|
otherClanList.innerHTML = data;
|
||||||
|
|
||||||
const lastOtherIndex = otherClanList.selectedIndex;
|
clanList.dispatchEvent(new Event('change'));
|
||||||
otherClanList.children.item(clanIndex).remove();
|
|
||||||
if (lastOtherIndex === clanIndex) {
|
|
||||||
otherClanList.selectedIndex = 0;
|
|
||||||
otherClanList.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();
|
deleteClanModalBS.hide();
|
||||||
} else
|
})
|
||||||
throw new Error(response.error)
|
.catch((error) => {
|
||||||
|
throw new Error(error)
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -26,44 +26,39 @@
|
|||||||
const deletePlayerModalBS = new bootstrap.Modal('#deletePlayerModal');
|
const deletePlayerModalBS = new bootstrap.Modal('#deletePlayerModal');
|
||||||
if (deletePlayerModal) {
|
if (deletePlayerModal) {
|
||||||
deletePlayerModal.addEventListener('show.bs.modal', event => {
|
deletePlayerModal.addEventListener('show.bs.modal', event => {
|
||||||
return;
|
const button = event.relatedTarget;
|
||||||
const [clanList, otherClanList] = getClanLists(event);
|
const playerId = parseInt(button.getAttribute('data-bs-id'));
|
||||||
|
|
||||||
const selectedClan = clanList.options[clanList.selectedIndex].text;
|
const selectedPlayer = button.closest('.input-group').querySelector('span').innerText;
|
||||||
const modalBodyInput = deletePlayerModal.querySelector('#clan');
|
const modalBodyInput = deletePlayerModal.querySelector('#player');
|
||||||
modalBodyInput.innerText = selectedClan;
|
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"]');
|
const submitButton = deletePlayerModal.querySelector('button[name="submit"]');
|
||||||
submitButton.addEventListener('click', function (e) {
|
submitButton.addEventListener('click', function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
submitButton.onclick = function () {}
|
submitButton.onclick = function () {}
|
||||||
|
|
||||||
const clanId = parseInt(clanList.value);
|
fetch("/player/" + playerId, {
|
||||||
|
|
||||||
fetch("/clan/" + clanId, {
|
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-type": "application/json; charset=UTF-8"
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then(() => {
|
||||||
if (response.ok) {
|
const sameClan = homeClanListIndex === oppClanListIndex;
|
||||||
const clanToRemove = clanList.children[clanList.selectedIndex];
|
if (playerListId === 'home-player-list' || sameClan)
|
||||||
const clanIndex = clanToRemove.index;
|
htmx.ajax('GET', '/players_html', {target: '#home-player-list', values: {"clan_id": getSelectedClanId("home-clan")}});
|
||||||
clanList.removeChild(clanToRemove);
|
if (playerListId === 'opponent-player-list' || sameClan)
|
||||||
clanList.selectedIndex = 0;
|
htmx.ajax('GET', '/players_html', {target: '#opponent-player-list', values: {"clan_id": getSelectedClanId("opponent-clan")}});
|
||||||
clanList.dispatchEvent(new Event('change'));
|
|
||||||
|
|
||||||
const lastOtherIndex = otherClanList.selectedIndex;
|
deletePlayerModalBS.hide();
|
||||||
otherClanList.children.item(clanIndex).remove();
|
})
|
||||||
if (lastOtherIndex === clanIndex) {
|
.catch((error) => {
|
||||||
otherClanList.selectedIndex = 0;
|
throw new Error(error)
|
||||||
otherClanList.dispatchEvent(new Event('change'));
|
|
||||||
}
|
|
||||||
|
|
||||||
deletePlayerModalBS.hide();
|
|
||||||
} else
|
|
||||||
throw new Error(response.error)
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -81,18 +81,35 @@
|
|||||||
"Content-type": "application/json; charset=UTF-8"
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then(() => {
|
||||||
if (response.ok) {
|
const lastIndex = clanList.selectedIndex;
|
||||||
const newName = "[" + clanTag.value + "]" + " " + clanName.value;
|
const otherLastIndex = otherClanList.selectedIndex;
|
||||||
selectedClanItem.innerText = newName;
|
|
||||||
otherSelClanItem.innerText = newName;
|
|
||||||
|
|
||||||
editClanModalBS.hide();
|
fetch('/clans_html')
|
||||||
clanName.value = "";
|
.then(response => {
|
||||||
clanTag.value = "";
|
if (!response.ok) {
|
||||||
keepUpdated.checked = false;
|
throw new Error('Network response was not ok');
|
||||||
} else
|
}
|
||||||
throw new Error(response.error)
|
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)
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
<li><a class="dropdown-item text-primary fs-5" href="#">
|
<li><a class="dropdown-item text-primary fs-5" href="#">
|
||||||
<i class="bi bi-person-gear fs-4 me-2"></i>Bearbeiten
|
<i class="bi bi-person-gear fs-4 me-2"></i>Bearbeiten
|
||||||
</a></li>
|
</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
|
<i class="bi bi-person-dash fs-4 me-2"></i>Löschen
|
||||||
</a></li>
|
</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
Reference in New Issue
Block a user