Admin page html + js finished

This commit is contained in:
asyncnomi 2023-01-02 23:33:43 +01:00
parent e8cca14021
commit 54c58ab6ea
2 changed files with 309 additions and 69 deletions

View file

@ -316,21 +316,25 @@ fastify.post('/get', async (request, reply) => {
}
break;
case "treasure":
let treasureData = JSON.parse(JSON.stringify(TreasureData));
for (treasure in treasureData) {
treasureData[treasure].activity = ActivityData[treasureData[treasure].activity];
}
if (AdminUsersUid.includes(content.uid)) {
return {
sucess: true,
prankData: TreasureData
treasureData: treasureData
}
} else {
let treasureData = {};
for (treasure in TreasureData) {
if (TreasureData[treasure].creator == content.uid) {
treasureData[treasure] = TreasureData[treasure];
let treasureDataUser = {};
for (treasure in treasureData) {
if (treasureData[treasure].creator == content.uid) {
treasureDataUser[treasure] = treasureData[treasure];
}
}
return {
success: true,
treasureData: treasureData
treasureData: treasureDataUser
}
}
break;
@ -477,6 +481,7 @@ fastify.post('/sendTreasure', async (request, reply) => {
let imageUid = makeid(128);
fs.writeFileSync("static/images/"+imageUid, content.image);
TreasureData[treasureUid] = {
date: new Date(),
creator: content.uid,
image: imageUid,
desc: content.desc,

View file

@ -62,7 +62,7 @@ $("#logo").click(function () {
if (localStorage.getItem('isAdmin')) {
show_page('#admin-page');
// Load prank by default
get_admin("prank");
}
else {
show_page('#demande-page');
@ -136,10 +136,97 @@ function get_admin(type) {
`);
break;
case: "treasure":
let treasureData = [];
for (treasureUid in data.treasureData) {
data.treasureData[treasureUid].treasureUid = treasureUid;
treasureData.push(data.treasureData[treasureUid]);
}
treasureData.sort((a, b) => Date(a.date) - Date(b.date));
for (entry in treasureData) {
if (treasureData[entry].state == "pending") {
$("#admin-content").insertAdjacentHTML("beforeend", `
<span id="${treasureData[entry].treasureUid}" class="treasure">
<div class="treasure-title">Demande de vérification par ${treasureData[entry].creator}</div>
<div class="treasure-activity">Activité: ${treasureData[entry].activity.title}</div>
<div class="treasure-desc">Description: ${treasureData[entry].desc}</div>
<div class="treasure-image">Image: <a href="${base_url}static/images/${treasureData[entry].image}" target="_blank">lien</a></div>
<span class="treasure-footer">
<div class="treasure-uid">UID: ${treasureData[entry].treasureUid}</div>
<div class="treasure-date">UID: ${treasureData[entry].date}</div>
</span>
<span class="treasure-btn">
<div class="treasure-btn-accept">Accepter</div>
<div class="treasure-btn-refused">Refuser</div>
</span>
</span>
`)
}
}
break;
case: "activity":
$("#admin-content").insertAdjacentHTML("beforeend", `
<span id="addActivity" class="activity">
<label for="activityType">Choisir une option:</label>
<select name="type" id="activityType">
<option value="event">Event</option>
<option value="treasure">Trésor</option>
</select>
<div class="activity-title">Titre: <input type="text"/></div>
<div class="activity-desc">Description: <input type="text"/></div>
<div class="activity-where">Lieu: <input type="text"/></div>
<div class="activity-start">Début: <input type="datetime-local"/></div>
<div class="activity-end">Fin: <input type="datetime-local"/></div>
<span class="activity-btn">
<div id="activity-btn-add">Ajouter une activité</div>
</span>
</span>
`)
let activityData = [];
for (activityUid in data.activityData) {
data.activityData[activityUid].activityUid = activityUid;
activityData.push(data.activityData[activityUid]);
}
activityData.sort((a, b) => Date(a.start) - Date(b.start));
for (entry in activityData) {
if (activityData[entry].type = "event") {
$("#admin-content").insertAdjacentHTML("beforeend", `
<span id="${activityData[entry].activityUid}" class="activity">
<div class="activity-title">Event: <input type="text" value="${activityData[entry].title}"/></div>
<div class="activity-desc">Description: <input type="text" value="${activityData[entry].desc}"/></div>
<div class="activity-where">Lieu: <input type="text" value="${activityData[entry].where}"/></div>
<div class="activity-start">Début: <input type="datetime-local" value="${toIsoString(new Date(activityData[entry].start)}"/></div>
<div id="activity-end-add" class="activity-end">Fin: <input type="datetime-local" value="${toIsoString(new Date(activityData[entry].end)}"/></div>
<span class="activity-footer">
<div class="activity-uid">UID: ${activityData[entry].activityUid}</div>
</span>
<span class="activity-btn">
<div class="activity-btn-update">Update</div>
<div class="activity-btn-delete">Supprimer</div>
</span>
</span>
`)
} else if (activityData[entry].type = "treasure") {
$("#admin-content").insertAdjacentHTML("beforebegin", `
<span id="${activityData[entry].activityUid}" class="activity">
<div class="activity-title">Trésor: <input type="text" value="${activityData[entry].title}"/></div>
<div class="activity-desc">Description: <input type="text" value="${activityData[entry].desc}"/></div>
<div class="activity-where">Lieu: <input type="text" value="${activityData[entry].where}"/></div>
<div class="activity-start">Début: <input type="datetime-local" value="${toIsoString(new Date(activityData[entry].start)}"/></div>
<div class="activity-state">Status: ${activityData[entry].treasureState}</div>
<span class="activity-footer">
<div class="activity-uid">UID: ${activityData[entry].activityUid}</div>
</span>
<span class="activity-btn">
<div class="activity-btn-update">Update</div>
<div class="activity-btn-delete">Supprimer</div>
</span>
</span>
`)
}
}
break;
}
refreshListener();
} else {
alert(data.why);
}
@ -147,77 +234,210 @@ function get_admin(type) {
});
}
$(".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",
function refreshListener() {
$(".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);
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",
$(".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);
success: function (data) {
if(data.success) {
$("#"+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",
$(".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);
success: function (data) {
if(data.success) {
$("#"+uid).remove();
$("#prankDone").innerHTML = parseInt($("#prankDone").innerHTML) + 1;
} else {
alert(data.why);
}
}
});
})
$(".treasure-btn-accept").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "acceptTreasure",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
treasureUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
$("#"+uid).remove();
} else {
alert(data.why);
}
}
});
})
$(".treasure-btn-refused").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "refuseTreasure",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
treasureUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
$("#"+uid).remove();
} else {
alert(data.why);
}
}
});
})
$(".activity-btn-update").click((e) => {
let uid = e.target.parentNode.parentNode.id;
let children = e.target.parentNode.parentNode.children();
$.ajax({
type: "POST",
url: base_url + "refuseTreasure",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
activityUid: uid,
type: (children[0].innerHTML.substring(0, 5) == "Event") ? "event" : "treasure",
title: children[0].children()[0].value,
desc: children[1].children()[0].value,
where: children[2].children()[0].value,
start: new Date(children[3].children()[0].value).toString(),
end: (children[3].children().length == 0) ? null : new Date(children[4].children()[0].value).toString(),
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
alert("Update réussie")
} else {
alert(data.why);
}
}
});
})
$(".activity-btn-delete").click((e) => {
let uid = e.target.parentNode.parentNode.id
$.ajax({
type: "POST",
url: base_url + "delActivity",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
activityUid: uid,
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
$("#"+uid).remove();
} else {
alert(data.why);
}
}
});
})
$("activityType").on("change", function() {
if (this.value == "treasure") {
$("#activity-end-add").style.display = "none";
} else if (this.value == "event") {
$("#activity-end-add").style.display = "flex";
}
});
})
})
$(".activity-btn-add").click((e) => {
let children = e.target.parentNode.parentNode.children();
$.ajax({
type: "POST",
url: base_url + "refuseTreasure",
data: JSON.stringify({
uid: localStorage.getItem('user'),
token: localStorage.getItem('token'),
type: $("#activityType").val(),
title: children[2].children()[0].value,
desc: children[3].children()[0].value,
where: children[4].children()[0].value,
start: new Date(children[5].children()[0].value).toString(),
end: ($("#activityType").val() == "treasure") ? null : new Date(children[6].children()[0].value).toString(),
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if(data.success) {
get_admin("activity");
} else {
alert(data.why);
}
}
});
})
}
$(window).on("load", function() {
@ -255,3 +475,18 @@ $(window).on("load", function() {
}));
}, 1000);
});
function toIsoString(date) {
var tzo = -date.getTimezoneOffset(),
dif = tzo >= 0 ? '+' : '-',
pad = function(num) {
return (num < 10 ? '0' : '') + num;
};
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds());
}