Implement edit-player. Optimizations.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/isc_rest.log
|
/isc_rest.log
|
||||||
|
/isc_data.db
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func GetPlayersByClanHTML(c *gin.Context) {
|
|||||||
|
|
||||||
var htmlOptions string
|
var htmlOptions string
|
||||||
for _, player := range players {
|
for _, player := range players {
|
||||||
htmlOptions += fmt.Sprintf(playerItem, player.Name, player.ID)
|
htmlOptions += fmt.Sprintf(playerItem, player.Name, player.ID, player.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Header("Content-Type", "text/html")
|
c.Header("Content-Type", "text/html")
|
||||||
@@ -124,13 +124,13 @@ func UpdatePlayerByID(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := "Updating player '" + player.Name + "'#" + strconv.FormatUint(uint64(player.ID), 10)
|
msg := "Updating player '" + player.Name + "' #" + strconv.FormatUint(uint64(player.ID), 10)
|
||||||
if player.Name != input.Name {
|
if player.Name != input.Name {
|
||||||
msg += " (new: '" + input.Name + "')"
|
msg += " (new: '" + input.Name + "')"
|
||||||
}
|
}
|
||||||
msg += " with clan #" + utils.UintToString(player.ClanID)
|
msg += " with clan #" + utils.UintToString(player.ClanID)
|
||||||
if player.ClanID != input.ClanID {
|
if player.ClanID != input.ClanID {
|
||||||
msg += " (new: ä" + utils.UintToString(input.ClanID) + ")"
|
msg += " (new: #" + utils.UintToString(input.ClanID) + ")"
|
||||||
}
|
}
|
||||||
_, err := fmt.Fprintf(utils.GinWriter, msg+"\n")
|
_, err := fmt.Fprintf(utils.GinWriter, msg+"\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
BIN
isc_data.db
BIN
isc_data.db
Binary file not shown.
1
main.go
1
main.go
@@ -40,6 +40,7 @@ func main() {
|
|||||||
"./templates/modals/edit_clan.html",
|
"./templates/modals/edit_clan.html",
|
||||||
"./templates/modals/add_player.html",
|
"./templates/modals/add_player.html",
|
||||||
"./templates/modals/delete_player.html",
|
"./templates/modals/delete_player.html",
|
||||||
|
"./templates/modals/edit_player.html",
|
||||||
"./templates/components/header.html",
|
"./templates/components/header.html",
|
||||||
}
|
}
|
||||||
tmpl, err := template.ParseFiles(files...)
|
tmpl, err := template.ParseFiles(files...)
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ type Player struct {
|
|||||||
ID uint `json:"id" gorm:"primary_key"`
|
ID uint `json:"id" gorm:"primary_key"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ClanID uint `json:"clan_id"`
|
ClanID uint `json:"clan_id"`
|
||||||
|
Clan Clan `gorm:"references:ID"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,9 @@ function getClanLists(event) {
|
|||||||
|
|
||||||
function getPlayerLists(event) {
|
function getPlayerLists(event) {
|
||||||
const button = event.relatedTarget;
|
const button = event.relatedTarget;
|
||||||
const playerListId = button.getAttribute('data-bs-list');
|
let playerListId = button.getAttribute('data-bs-list');
|
||||||
|
if (playerListId === null)
|
||||||
|
playerListId = '#' + button.closest('ul').parentElement.parentElement.id;
|
||||||
const playerList = document.querySelector(playerListId);
|
const playerList = document.querySelector(playerListId);
|
||||||
|
|
||||||
let otherPlayerListId;
|
let otherPlayerListId;
|
||||||
@@ -59,7 +61,9 @@ function getPlayerLists(event) {
|
|||||||
|
|
||||||
function getSelectedClan(event) {
|
function getSelectedClan(event) {
|
||||||
const button = event.relatedTarget;
|
const button = event.relatedTarget;
|
||||||
const playerListId = button.getAttribute('data-bs-list');
|
let playerListId = button.getAttribute('data-bs-list');
|
||||||
|
if (playerListId === null)
|
||||||
|
playerListId = '#' + button.closest('ul').parentElement.parentElement.id;
|
||||||
|
|
||||||
let clanListId;
|
let clanListId;
|
||||||
if (playerListId === '#home-player-list')
|
if (playerListId === '#home-player-list')
|
||||||
|
|||||||
@@ -42,6 +42,8 @@
|
|||||||
{{ template "add_player" . }}
|
{{ template "add_player" . }}
|
||||||
<!-- Delete Player Modal -->
|
<!-- Delete Player Modal -->
|
||||||
{{ template "delete_player" . }}
|
{{ template "delete_player" . }}
|
||||||
|
<!-- Edit Player Modal -->
|
||||||
|
{{ template "edit_player" . }}
|
||||||
|
|
||||||
<script lang="javascript">
|
<script lang="javascript">
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
|||||||
@@ -36,10 +36,7 @@
|
|||||||
if (addClanModal) {
|
if (addClanModal) {
|
||||||
addClanModal.addEventListener('shown.bs.modal', event => {
|
addClanModal.addEventListener('shown.bs.modal', event => {
|
||||||
const submitButton = addClanModal.querySelector('button[name="submit"]');
|
const submitButton = addClanModal.querySelector('button[name="submit"]');
|
||||||
submitButton.addEventListener('click', function (e) {
|
submitButton.addEventListener('click', function () {
|
||||||
e.preventDefault();
|
|
||||||
submitButton.onclick = function () {}
|
|
||||||
|
|
||||||
const [clanList, otherClanList] = getClanLists(event);
|
const [clanList, otherClanList] = getClanLists(event);
|
||||||
|
|
||||||
const clanName = addClanModal.querySelector('#clanName');
|
const clanName = addClanModal.querySelector('#clanName');
|
||||||
@@ -89,7 +86,7 @@
|
|||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
throw new Error(error)
|
throw new Error(error)
|
||||||
});
|
});
|
||||||
})
|
}, { once: true });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -41,10 +41,7 @@
|
|||||||
const oppClanListIndex = document.getElementById('opponent-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.preventDefault();
|
|
||||||
submitButton.onclick = function () {}
|
|
||||||
|
|
||||||
fetch("/player", {
|
fetch("/player", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
|||||||
@@ -33,10 +33,7 @@
|
|||||||
modalBodyInput.innerText = selectedClan;
|
modalBodyInput.innerText = selectedClan;
|
||||||
|
|
||||||
const submitButton = deleteClanModal.querySelector('button[name="submit"]');
|
const submitButton = deleteClanModal.querySelector('button[name="submit"]');
|
||||||
submitButton.addEventListener('click', function (e) {
|
submitButton.addEventListener('click', function () {
|
||||||
e.preventDefault();
|
|
||||||
submitButton.onclick = function () {}
|
|
||||||
|
|
||||||
const clanId = parseInt(clanList.value);
|
const clanId = parseInt(clanList.value);
|
||||||
|
|
||||||
fetch("/clan/" + clanId, {
|
fetch("/clan/" + clanId, {
|
||||||
@@ -69,7 +66,7 @@
|
|||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
throw new Error(error)
|
throw new Error(error)
|
||||||
});
|
});
|
||||||
})
|
}, { once: true });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -38,10 +38,7 @@
|
|||||||
const oppClanListIndex = document.getElementById('opponent-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.preventDefault();
|
|
||||||
submitButton.onclick = function () {}
|
|
||||||
|
|
||||||
fetch("/player/" + playerId, {
|
fetch("/player/" + playerId, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -60,7 +57,7 @@
|
|||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
throw new Error(error)
|
throw new Error(error)
|
||||||
});
|
});
|
||||||
})
|
}, { once: true });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<div class="modal-dialog modal-dialog-centered">
|
<div class="modal-dialog modal-dialog-centered">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<h1 class="modal-title fs-3 text-success fw-bold" id="editClanModalLabel">Clan bearbeiten</h1>
|
<h1 class="modal-title fs-3 text-primary fw-bold" id="editClanModalLabel">Clan bearbeiten</h1>
|
||||||
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
@@ -32,17 +32,12 @@
|
|||||||
|
|
||||||
<script lang="javascript">
|
<script lang="javascript">
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
|
||||||
const editClanModal = document.getElementById('editClanModal');
|
const editClanModal = document.getElementById('editClanModal');
|
||||||
const editClanModalBS = new bootstrap.Modal('#editClanModal');
|
const editClanModalBS = new bootstrap.Modal('#editClanModal');
|
||||||
if (editClanModal) {
|
if (editClanModal) {
|
||||||
editClanModal.addEventListener('show.bs.modal', event => {
|
editClanModal.addEventListener('show.bs.modal', event => {
|
||||||
const [clanList, otherClanList] = getClanLists(event);
|
const [clanList, otherClanList] = getClanLists(event);
|
||||||
|
|
||||||
const selectedClanIndex = clanList.selectedIndex;
|
|
||||||
const selectedClanItem = clanList.options[selectedClanIndex];
|
|
||||||
const otherSelClanItem = otherClanList.options[selectedClanIndex];
|
|
||||||
|
|
||||||
const clanName = editClanModal.querySelector('#editClanName');
|
const clanName = editClanModal.querySelector('#editClanName');
|
||||||
const clanTag = editClanModal.querySelector('#editClanTag');
|
const clanTag = editClanModal.querySelector('#editClanTag');
|
||||||
const keepUpdated = editClanModal.querySelector('#editKeepUpdated');
|
const keepUpdated = editClanModal.querySelector('#editKeepUpdated');
|
||||||
@@ -67,9 +62,7 @@
|
|||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
submitButton.addEventListener('click', function (e) {
|
submitButton.addEventListener('click', function () {
|
||||||
e.preventDefault();
|
|
||||||
submitButton.onclick = function () {}
|
|
||||||
fetch("/clan/" + clanId, {
|
fetch("/clan/" + clanId, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -111,7 +104,7 @@
|
|||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
throw new Error(error)
|
throw new Error(error)
|
||||||
});
|
});
|
||||||
})
|
}, { once: true });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
75
templates/modals/edit_player.html
Normal file
75
templates/modals/edit_player.html
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
{{ define "edit_player" }}
|
||||||
|
|
||||||
|
<div class="modal fade" id="editPlayerModal" tabindex="-1">
|
||||||
|
<div class="modal-dialog modal-dialog-centered">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h1 class="modal-title fs-3 text-primary fw-bold" id="editPlayerModalLabel">Spieler bearbeiten</h1>
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<div class="form-floating mb-3">
|
||||||
|
<input type="text" class="form-control form-control-lg" id="editPlayerName" placeholder="Spieler-Name">
|
||||||
|
<label for="editPlayerName">Spieler-Name</label>
|
||||||
|
</div>
|
||||||
|
<input type="hidden" id="editPlayerId" value="">
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="submit" name="submit" class="btn btn-lg btn-primary">Speichern</button>
|
||||||
|
<button type="button" class="btn btn-lg btn-secondary" data-bs-dismiss="modal">Abbrechen</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script lang="javascript">
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
const editPlayerModal = document.getElementById('editPlayerModal');
|
||||||
|
const editPlayerModalBS = new bootstrap.Modal('#editPlayerModal');
|
||||||
|
if (editPlayerModal) {
|
||||||
|
editPlayerModal.addEventListener('show.bs.modal', event => {
|
||||||
|
const [playerList, otherPlayerList] = getPlayerLists(event);
|
||||||
|
const selectedClan = getSelectedClan(event);
|
||||||
|
const playerName = editPlayerModal.querySelector('#editPlayerName');
|
||||||
|
const clanId = parseInt(selectedClan.value);
|
||||||
|
const homeClanListIndex = document.getElementById('home-clan').selectedIndex;
|
||||||
|
const oppClanListIndex = document.getElementById('opponent-clan').selectedIndex;
|
||||||
|
|
||||||
|
const button = event.relatedTarget;
|
||||||
|
const playerId = parseInt(button.getAttribute('data-bs-id'));
|
||||||
|
playerName.value = button.closest('.input-group').querySelector('span').innerText;
|
||||||
|
|
||||||
|
const submitButton = editPlayerModal.querySelector('button[name="submit"]');
|
||||||
|
submitButton.addEventListener('click', function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
submitButton.onclick = function () {}
|
||||||
|
|
||||||
|
fetch("/player/" + playerId, {
|
||||||
|
method: "PATCH",
|
||||||
|
body: JSON.stringify({
|
||||||
|
name: playerName.value,
|
||||||
|
clan_id: clanId
|
||||||
|
}),
|
||||||
|
headers: {
|
||||||
|
"Content-type": "application/json; charset=UTF-8"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.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")}});
|
||||||
|
|
||||||
|
editPlayerModalBS.hide();
|
||||||
|
playerName.value = "";
|
||||||
|
}).catch((error) => {
|
||||||
|
throw new Error(error)
|
||||||
|
});
|
||||||
|
}, { once: true });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{{ end }}
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
<span class="form-control py-1 px-2">%s</span>
|
<span class="form-control py-1 px-2">%s</span>
|
||||||
<button class="btn btn-outline-secondary text-secondary-emphasis dropdown-toggle py-1" type="button" data-bs-toggle="dropdown"></button>
|
<button class="btn btn-outline-secondary text-secondary-emphasis dropdown-toggle py-1" type="button" data-bs-toggle="dropdown"></button>
|
||||||
<ul class="dropdown-menu dropdown-menu-end">
|
<ul class="dropdown-menu dropdown-menu-end">
|
||||||
<li><a class="dropdown-item text-primary fs-5" href="#">
|
<li><a class="dropdown-item text-primary fs-5" href="#" data-bs-toggle="modal" data-bs-id="%d" data-bs-target="#editPlayerModal">
|
||||||
<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-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">
|
||||||
|
|||||||
Reference in New Issue
Block a user