feat: Connected with api

workign login, registration, tournaments get
This commit is contained in:
Arek 2022-06-10 00:56:06 +02:00
parent 2cbd9c29d7
commit d3961bee19
6 changed files with 139 additions and 80 deletions

View File

@ -1,29 +1,57 @@
document.getElementById("tournamentSubmit").addEventListener("click", function () {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
alert("Dodano turniej");
} else if (this.readyState == 4) {
alert("something not work :( " + this.status);
// document.getElementById("tournamentSubmit").addEventListener("click", function () {
// var xhttp = new XMLHttpRequest();
// xhttp.onreadystatechange = function () {
// if (this.readyState == 4 && this.status == 200) {
// alert("Dodano turniej");
// } else if (this.readyState == 4) {
// alert("something not work :( " + this.status);
// }
// }
// xhttp.open("PUT", "dragonmaster.pl/inz/tournament");
// xhttp.setRequestHeader("Authorization", "Bearer " + token);
// xhttp.send('{"name":"' + document.getElementById("tournamentName").value +
// '","typeOfLadder":"' + document.getElementById("ladderType").value +
// '","pointsForTournament":"' + document.getElementById("tournamentPoints").value +
// '","places":"' + document.getElementById("tournamentPlace").value +
// '","roles":"' + document.getElementById("tournamentRole").value +
// '","ranked":"' + document.getElementById("isRanked").value +
// '","place":"' + document.getElementById("tournamentPlace").value +
// '","from":"' + document.getElementById("tournamentFrom").value +
// '","to":"' + document.getElementById("tournamentTo").value +
// '","rang":"' + document.getElementById("tournamentRang").value +
// '","entyFee":"' + document.getElementById("tournamentEntyFee").value +
// '","director":"' + document.getElementById("tournamentDirector").value +
// '","phone":"' + document.getElementById("directorphone").value +
// '","entriesTo":"' + document.getElementById("tournamentEntriesTo").value +
// '","additionalInformations":"' + document.getElementById("tournamentAddInfo").value +
// '","category":"' + "cat1" +
// '","}');
// });
function createTournament() {
fetch('https://dragonmaster.pl/inz/tournament', {
method: "PUT",
headers: {
'Content-type': 'application/json',
Authorization: ("Bearer " + localStorage.getItem("token"))
},
body: JSON.stringify(
{
"name": document.getElementById("name").value,
"place": document.getElementById("place").value,
"ranked": document.getElementById("rank").value,
"roles": document.getElementById("category").value,
"phone": document.getElementById("phone").value,
"director": document.getElementById("director").value,
"phone": document.getElementById("regphone").value,
"entryFee": document.getElementById("entry-fee").value,
"additionalInformations": document.getElementById("add-info").value
}
)
}
)
.then(res => res.json())
.then(data => console.log(data))
.catch(error => console.log(error))
}
xhttp.open("PUT", "dragonmaster.pl/inz/tournament");
xhttp.setRequestHeader("Authorization", "Bearer " + token);
xhttp.send('{"name":"' + document.getElementById("tournamentName").value +
'","typeOfLadder":"' + document.getElementById("ladderType").value +
'","pointsForTournament":"' + document.getElementById("tournamentPoints").value +
'","places":"' + document.getElementById("tournamentPlace").value +
'","roles":"' + document.getElementById("tournamentRole").value +
'","ranked":"' + document.getElementById("isRanked").value +
'","place":"' + document.getElementById("tournamentPlace").value +
'","from":"' + document.getElementById("tournamentFrom").value +
'","to":"' + document.getElementById("tournamentTo").value +
'","rang":"' + document.getElementById("tournamentRang").value +
'","entyFee":"' + document.getElementById("tournamentEntyFee").value +
'","director":"' + document.getElementById("tournamentDirector").value +
'","phone":"' + document.getElementById("directorphone").value +
'","entriesTo":"' + document.getElementById("tournamentEntriesTo").value +
'","additionalInformations":"' + document.getElementById("tournamentAddInfo").value +
'","category":"' + "cat1" +
'","}');
});

View File

@ -31,57 +31,57 @@
<div class="form">
<h3 class="descriptor">Nazwa Turnieju</h3>
<label>
<input>
<input id="name">
</label>
<h3 class="descriptor">Termin</h3>
<label>
<input>
<input id="term">
</label>
<h3 class="descriptor">Miejsce</h3>
<label>
<input>
<input id="place">
</label>
<h3 class="descriptor">Kategorie</h3>
<label>
<input>
<input id="category">
</label>
<h3 class="descriptor">Ranga</h3>
<label>
<input>
<input id="rank">
</label>
<h3 class="descriptor">Wpisowe</h3>
<label>
<input>
<input id="entry-fee">
</label>
<h3 class="descriptor">Dyrektor Turnieju</h3>
<label>
<input>
<input id="director">
</label>
<h3 class="descriptor">Telefon</h3>
<label>
<input>
<input id="phone">
</label>
<h3 class="descriptor">Zapisy</h3>
<label>
<input>
<input id="entries">
</label>
<h3 class="descriptor">Dodatkowe informacje</h3>
<label>
<textarea name="Text1" cols="40" rows="5"></textarea>
<textarea name="Text1" cols="40" rows="5" id="add-info"></textarea>
</label>
</br>
<button class="myButton" type="submit" id="tournamentsubmit">
<button class="myButton" type="submit" id="tournamentsubmit" onclick="createTournament()">
Załóż turniej
</button>

View File

@ -1,18 +1,66 @@
document.getElementById("loginsubmit").addEventListener("click", function () {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
alert("logged in");
console.log("dziala");
} else if (this.readyState == 4) {
alert("something not work :( " + this.status);
console.log("nie dziala");
function login() {
fetch('https://dragonmaster.pl/inz/user/login', {
method: "POST",
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(
{
"username": document.getElementById("loginusername").value,
"password": document.getElementById("loginpassword").value
}
)
}
)
.then(res => {
if (res.ok) {
// alert("Teraz mozesz się zalogować!")
}
else { console.log("Coś poszło nie tak!") }
return res
})
.then(res => res.json())
.then(data => console.log(data['token']?.length > 0 ? localStorage.setItem("token", data['token']) : alert("Błędne dane!")))
.catch(error => console.log(error))
checkRole();
}
xhttp.open("POST", "https://dragonmaster.pl/inz/user/login");
xhttp.send('{"username":"' + document.getElementById("loginusername").value + '","password":"' + document.getElementById("loginpassword").value + '"}');
});
function submit() {
("form").submit(function () { return false; })
// function checkRole() {
// fetch('https://dragonmaster.pl/inz/user', {
// method: "GET",
// headers: {
// 'Content-type': 'application/json'
// },
// body: JSON.stringify(
// {
// "token": document.getElementById("loginusername").value,
// "password": document.getElementById("loginpassword").value
// }
// )
// }
// )
// .then(res => {
// if (res.ok) {
// alert("Teraz mozesz się zalogować!")
// }
// else { console.log("Coś poszło nie tak!") }
// return res
// })
// .then(res => res.json())
// .then(data => console.log(data['token']?.length > 0 ? localStorage.setItem("token", data['token']) : 'niedziaua'))
// .catch(error => console.log(error))
// .then(token1 => token = token1)
// }
function checkRole() {
fetch('https://dragonmaster.pl/inz/user', {
headers: {
Authorization: ("Bearer " + localStorage.getItem("token"))
}
})
.then(res => res.json())
.then(json => console.log(json['role']?.length == 1 ? window.location.href = '../index.html' : window.location.href = '../dodawanie-turnieju/index.html '))
}

View File

@ -20,10 +20,9 @@
<br>
<!-- trzeba dać cssy buttonowi bo zmieniłem z form na button -->
<button>
<input class="myButton" type="submit" value="Zaloguj" id="loginsubmit" />
<button class="myButton" id="loginsubmit" onclick="login()">
Zaloguj
</button>
<br>
<a href="#">Zapomniałeś hasła?</a>
<br>

View File

@ -11,6 +11,7 @@
</head>
<body>
<div class="container">
<div class="form">
<div class="register">
@ -42,9 +43,10 @@
<input placeholder="Potwierdź hasło" type="password" id="password2" name="password">
<i class="bi bi-eye-slash" id="togglePassword2"></i>
<form action="reg" onclick="register()">
<input class="myButton" type="submit" value="Zarejestruj" id="regsubmit" />
</form>
<button class="myButton" id="regsubmit" onclick="register()">
Zajerestruj
</button>
<br>
<a href="#">Zapomniałeś hasła?</a>

View File

@ -11,28 +11,10 @@ togglePassword.addEventListener("click", function () {
this.classList.toggle("bi-eye");
});
// prevent form submit
const form = document.querySelector("form");
form.addEventListener('submit', function (e) {
e.preventDefault();
});
document.getElementById("regsubmit").addEventListener("click", function () {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
alert("200 now you can login");
} else if (this.readyState == 4) {
alert("something not work :( " + this.status);
}
}
xhttp.open("PUT", "https://dragonmaster.pl/inz/user/create");
xhttp.send('{"username":"' + document.getElementById("regusername").value + '","name":"' + document.getElementById("regname").value + '","surname":"' + document.getElementById("regsurname").value + '","password":"' + document.getElementById("regpassword").value + '","mail":"' + document.getElementById("regmail").value + '","phone":"' + document.getElementById("regphone").value + '"}');
});
function register() {
fetch('https://dragonmaster.pl/inz/user/create', {
method: "PSOT",
method: "PUT",
headers: {
'Content-type': 'application/json'
},
@ -51,7 +33,7 @@ function register() {
.then(res => {
if (res.ok) {
alert("Teraz mozesz się zalogować!")
window.open("../panel_organizatora/logowanie/index.html");
window.location.href = '../logowanie/index.html'
}
else { console.log("Coś poszło nie tak!") }
return res