Padel-Tournaments-System/frontend/normal_user/_main page/pojedynczy_turniej/zapisy na turniej/app.js
2022-06-19 15:01:32 +02:00

157 lines
4.8 KiB
JavaScript

function pickpayment(e){
document.getElementById("dropbtn").innerText=e.target.innerText;
}
document.getElementById("paymentMethodCash").addEventListener("click",pickpayment);
document.getElementById("paymentMethodTransfer").addEventListener("click",pickpayment);
document.getElementById("paymentMethodBitcoin").addEventListener("click",pickpayment);
function checkRole() {
fetch('https://dragonmaster.pl/inz/user', {
headers: {
Authorization: ("Bearer " + localStorage.getItem("token"))
}
})
.then(res => res.json())
.then(json => {
if (json['role'] == '1') {
window.location.href = '../index.html'
}
else {
window.location.href = '../../panel_organizatora/dodawanie turnieju/index.html'
}
})
}
function login() {
localStorage.clear()
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!"))
checkRole();
})
.catch(error => console.log(error))
}
var invoiceid="";
var paymentid="";
function tournamentSave() {
if(document.getElementById("dropbtn").innerText =="Przelew"){
alert("not implemented yet :(");
return;
}
fetch('https://dragonmaster.pl/inz/registration', {
method: "PUT",
headers: {
Authorization: ("Bearer " + localStorage.getItem("token"))
},
body: JSON.stringify(
{
"paymentmethod": document.getElementById("dropbtn").innerText =="Gotówka"?"cash":"btc",
"tournament": localStorage.getItem('tournamentId'),
"partner": document.getElementById("ID_input").value
}
)
}
)
.then(res => {
if (res.ok) {
alert("Udało się zapisać!");
if (document.getElementById("dropbtn").innerText =="Bitcoin") {
}else{
window.location.href = '../index.html';
}
}
else { console.log("Coś poszło nie tak!") }
return res
})
.then(res => res.json())
.then(res => {
if (document.getElementById("dropbtn").innerText =="Bitcoin") {
paymentid=res.id;
var splited=res.url.split("/");
invoiceid=splited[splited.length-1];
window.btcpay.showInvoice(invoiceid);
window.btcpay.onModalWillLeave(process);
}
})
.catch(error => console.log(error))
}
var counter = 0;
function process(){
document.getElementById("loader").style.display="block";
counter = 0;
setTimeout(check, 1000);
}
function check(){
fetch('https://dragonmaster.pl/inz/registration/paymentstatus?id='+paymentid, {
method: "GET",
headers: {
Authorization: ("Bearer " + localStorage.getItem("token"))
}
}
)
.then(res => {
if (res.ok) {
}
else { alert("something not work :("); }
return res
})
.then(res => res.json())
.then(res => {
if (res.status == "DONE" || res.status == "PROCESSING") {
document.getElementById("loader").style.display="none";
document.getElementById("msg").style.display="block";
document.getElementById("msg").innerText="payed";
window.location.href = '../index.html';
}else{
if (res.status == "PENDING" ) {
if(counter==5){
document.getElementById("loader").style.display="none";
document.getElementById("msg").style.display="block";
document.getElementById("msg").innerText="you probably closed popup";
document.getElementById("reopen").style.display="block";
}else{
setTimeout(check, 1000);
}
counter ++;
}else{
document.getElementById("msg").style.display="block";
document.getElementById("msg").innerText=res.status;
document.getElementById("reopen").style.display="block";
}
}
})
.catch(error => console.log(error))
}
function reopen(){
document.getElementById("msg").style.display="none";
document.getElementById("reopen").style.display="none";
window.btcpay.showInvoice(invoiceid);
}
document.getElementById("reopen").addEventListener("click",reopen);