bde-liste/static/js/main.js

258 lines
8.8 KiB
JavaScript
Raw Normal View History

2022-12-19 22:26:51 +00:00
const base_url = "https://oss110metz.rezo-rm.fr/"
2022-12-21 21:58:48 +00:00
var page = ['#main-page', '#admin-page', '#login-page', '#demande-page'];
2022-12-21 21:57:49 +00:00
window.onpopstate = history.onpushstate = function(e) {
2022-12-21 22:22:56 +00:00
if (e.state) {
show_page(e.state, true);
} else {
history.back();
}
2022-12-21 21:57:49 +00:00
};
history.pushState("#main-page", "", "")
2022-12-21 22:25:34 +00:00
function show_page(id, historyPush) {
2022-12-21 21:58:48 +00:00
for(i in page) {
$(page[i]).hide();
}
2022-12-19 22:26:51 +00:00
$(id).show();
2022-12-21 22:22:56 +00:00
if (!historyPush) {
history.pushState(id, "", "")
}
2022-12-19 22:26:51 +00:00
}
2022-12-21 21:20:48 +00:00
$("#login-button").click(function (e) {
var data = JSON.stringify({
user: $("#login").val(),
password: $("#password").val()
});
$("#password").val('');
$.ajax({
type: "POST",
url: base_url + "login",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
localStorage.setItem("token", data.token);
localStorage.setItem("user", data.user);
2022-12-21 22:42:53 +00:00
localStorage.setItem("isAdmin", data.isAdmin);
2022-12-21 21:20:48 +00:00
show_page('#demande-page');
} else {
alert(data.why);
2022-12-21 21:20:48 +00:00
}
}
});
});
$("#prank-button").click(function () {
2022-12-21 22:42:53 +00:00
if (localStorage.getItem('token')) {
2022-12-21 21:20:48 +00:00
show_page('#demande-page');
}
else {
show_page('#login-page');
}
});
2022-12-21 22:42:53 +00:00
$("#logo").click(function () {
if (localStorage.getItem('token')) {
if (localStorage.getItem('isAdmin')) {
show_page('#admin-page');
2023-01-01 20:40:58 +00:00
// Load prank by default
2022-12-21 22:42:53 +00:00
}
else {
show_page('#demande-page');
}
}
else {
show_page('#login-page');
}
});
2023-01-01 20:40:58 +00:00
function get_admin(type) {
$("#admin-content").innerHTML = "";
$.ajax({
type: "POST",
url: base_url + "login",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
type: type,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
switch (type) {
case: "prank":
let doneCnt = 0;
let prankData = [];
for (prankUid in data.prankData) {
data.prankData[prankUid].prankUid = prankUid;
prankData.push(data.prankData[prankUid]);
}
prankData.sort((a, b) => Date(a.date) - Date(b.date));
for (entry in prankData) {
switch (prankData[entry].type) {
case: "crêpe":
if (prankData[entry].state == "pending"
|| prankData[entry].state == "accepted") {
$("#admin-content").insertAdjacentHTML("beforeend", `
<span id="${prankData[entry].prankUid}" class="prank">
<div class="prank-title">Demande de crêpe par ${prankData[entry].creator}</div>
<div class="prank-amount">Quantité: ${prankData[entry].amount}</div>
<div class="prank-supplement">Garniture: ${prankData[entry].supplement}</div>
<div class="prank-where">Livraison: ${prankData[entry].where}</div>
<div class="prank-note">Livraison: ${prankData[entry].note}</div>
<span class="prank-footer">
<div class="prank-uid">UID: ${prankData[entry].prankUid}</div>
<div class="prank-date">UID: ${prankData[entry].date}</div>
</span>
<span class="prank-btn">
<div class="prank-btn-accept" style="display: ${(prankData[entry].state == "accepted" && prankData[entry].manageBy == localStorage.getItem("user")) ? "none" : "flex"};">${(prankData[entry].state == "pending") ? "Accepter" : "Récupérer (accepté par: "+prankData[entry].manageBy+")"}</div>
2023-01-01 20:40:58 +00:00
<div class="prank-btn-refused">Refuser</div>
<div class="prank-btn-done" style="display: ${(prankData[entry].state == "accepted" && prankData[entry].manageBy == localStorage.getItem("user")) ? "flex" : "none"};">Done</div>
2023-01-01 20:40:58 +00:00
</span>
</span>
`)
} else if(prankData[entry].state == "done") {
doneCnt++;
}
break;
case: "kidnap":
// Not Handle
2023-01-01 20:40:58 +00:00
break;
}
}
$("#admin-content").insertAdjacentHTML("beforeend", `
<span class="prank">
<div id=prankDone>Nombre totale de mission achevées: ${doneCnt}</div>
</span>
`);
2023-01-01 20:40:58 +00:00
break;
case: "treasure":
break;
case: "activity":
break;
}
} else {
alert(data.why);
}
}
});
}
$(".prank-btn-accept").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "acceptPrank",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
prankUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
let btnElems = $("#"+uid).children()[6];
btnElems.children()[0].style.display = "none";
btnElems.children()[2].style.display = "flex";
} else {
alert(data.why);
}
}
});
})
$(".prank-btn-refused").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "refusePrank",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
prankUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
let btnElems = $("#"+uid).remove();
} else {
alert(data.why);
}
}
});
})
$(".prank-btn-done").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "donePrank",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
prankUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
let btnElems = $("#"+uid).remove();
$("#prankDone").innerHTML = parseInt($("#prankDone").innerHTML) + 1;
} else {
alert(data.why);
}
}
});
})
2022-12-15 13:30:34 +00:00
$(window).on("load", function() {
2022-12-20 20:23:31 +00:00
/* Register the Service Worker */
if ('serviceWorker' in navigator) {
2022-12-21 21:20:48 +00:00
navigator.serviceWorker.register('./js/sw.js');
2022-12-20 20:23:31 +00:00
}
2022-12-21 21:58:48 +00:00
for(i in page) {
$(page[i]).css('z-index', i);
}
2022-12-15 13:30:34 +00:00
/* load timer */
window.setInterval(function() {
var time = $("#timer").text();
var t = time.split(":").map(t => parseInt(t));
t[2] -= 1;
if (t[2] < 0) {
t[2] = 59;
t[1] -= 1;
}
if (t[1] < 0) {
t[1] = 59;
t[0] -= 1;
}
if (t[0] < 0) {
t[0] = 0;
t[1] = 0;
t[2] = 0;
}
2022-12-15 16:14:41 +00:00
$("#timer").text((new Date('December 17, 1995 ' + t.join(':'))).toLocaleTimeString('default', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}));
2022-12-15 13:30:34 +00:00
}, 1000);
2022-12-19 22:26:51 +00:00
});