Padel-Tournaments-System/frontend/normal_user/app.js
2022-06-19 09:34:23 +02:00

164 lines
4.5 KiB
JavaScript

async function showTournaments() {
let url = 'https://dragonmaster.pl/inz/tournaments';
try {
let res = await fetch(url);
return await res.json();
} catch (error) {
console.log(error);
}
}
function saveIdTournament(id) {
localStorage.setItem('tournamentId', id);
}
function checkRoleCreator() {
fetch('https://dragonmaster.pl/inz/user', {
headers: {
Authorization: ("Bearer " + localStorage.getItem("token"))
}
})
.then(res => res.json())
.then(json => {
let organiser = (json['id'])
renderTournaments(organiser)
})
}
function tournamentDelete(id) {
console.log(id)
let textId = id.toString()
console.log(textId)
fetch('https://dragonmaster.pl/inz/tournament', {
method: "DELETE",
headers: {
'Content-type': 'application/json',
Authorization: ("Bearer " + localStorage.getItem("token"))
},
body: JSON.stringify(
{
"id": textId
}
)
}
)
.then(res => res.json())
.then(data => {
console.log(data)
location.reload()
})
.catch(error => console.log(error))
}
async function renderTournaments(organiserTournament) {
let tournaments = await showTournaments();
let html = '';
tournaments.forEach(tournament => {
// console.log(test)
organiser = `${tournament.creator}`;
// console.log(organiser)
if (organiser == organiserTournament) {
let htmlSegment = `<div class = "tournamentBox" onclick="window.location.href='../panel_organizatora/edycja turnieju/index.html'; saveIdTournament(${tournament.id});">
<div class="tournamentIconBox">
<img class="tournamentIcon" src="_main%20page/cup.svg" alt="">
</div>
<div class="tournamentDataBox">
<div class="tournamentData">
<h4>${tournament.name}</h4>
${tournament.place}
${tournament.from} do ${tournament.to}
<button class="tournamentOpen">OPEN</button>
</div>
</div>
<input type="image" src="src/trash.svg" class="dots" onclick="tournamentDelete(${tournament.id}); event.stopPropagation();"/>
</div>
`
html += htmlSegment;
}
else {
let htmlSegment = `<div class = "tournamentBox" onclick="window.location.href='_main page/pojedynczy_turniej/index.html'; saveIdTournament(${tournament.id});">
<div class="tournamentIconBox">
<img class="tournamentIcon" src="_main%20page/cup.svg" alt="">
</div>
<div class="tournamentDataBox">
<div class="tournamentData">
<h4>${tournament.name}</h4>
${tournament.place}
${tournament.from} do ${tournament.to}
<button class="tournamentOpen">OPEN</button>
</div>
</div>
</div>
`;
html += htmlSegment;
}
});
let container = document.querySelector('.tournament-list');
container.innerHTML = html;
}
function logout() {
localStorage.clear();
location.reload();
}
function logoutVisibility() {
if (localStorage.getItem("token") !== null) {
document.getElementById("logout").style.display = "";
}
else {
document.getElementById("logout").style.display = "none";
}
}
function loginVisibility() {
if (localStorage.getItem("token") !== null) {
document.getElementById("login").style.display = "none";
}
else {
document.getElementById("login").style.display = "";
}
}
function checkRole() {
if (localStorage.getItem("token") !== null) {
fetch('https://dragonmaster.pl/inz/user', {
headers: {
Authorization: ("Bearer " + localStorage.getItem("token"))
}
})
.then(res => res.json())
.then(json => {
if (json['role'] == '1') {
document.getElementById("creator").style.display = "none";
}
else {
document.getElementById("creator").style.display = '';
}
})
}
else {
document.getElementById("creator").style.display = "none";
}
}