login-template/web/adminpanel.html

137 lines
4.5 KiB
HTML
Raw Normal View History

2024-02-13 12:18:04 -08:00
<!DOCTYPE html>
<html lang="en">
<!--
struct ToggleRole {
perm: String,
value: bool,
username: String
}
-->
<head>
<title>perms toggle</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/css/style.css" rel="stylesheet">
</head>
<body>
<div id="data">
2024-02-13 14:55:21 -08:00
Welcome, {{username}}. Please select a user to modify.
2024-02-13 12:18:04 -08:00
</div>
<form action="/toggleperms" method="post" class="permtoggle-form" id="permtoggle-form">
<div id="username-form">
<label for="name">Username: </label>
<input type="text" name="username" id="username" required />
</div>
<div id="load-button">
<input type="button" value="Load user" id="load-user" />
</div>
<div class="toggle-form">
<label for="admin-toggle">Admin: </label>
<input type="checkbox" value="admin-toggle" id="admin-toggle" />
<br>
<label for="make-post-toggle">Make posts: </label>
<input type="checkbox" value="make-post-toggle" id="make-post-toggle" />
<br>
<label for="comment-toggle">Comment: </label>
<input type="checkbox" value="comment-toggle" id="comment-toggle" />
</div>
</form>
</body>
<script defer>
document.getElementById("permtoggle-form").addEventListener("click", async (event) => {
event.preventDefault();
switch (event.srcElement.id) {
case "load-user": {
let username = document.getElementById("username").value;
const user = JSON.parse(await (await fetch(`/api/perms/${username}`)).text()).perms
if (user.Ok) {
2024-02-13 14:55:21 -08:00
// set checked
document.getElementById("admin-toggle").checked = user.Ok.admin
document.getElementById("make-post-toggle").checked = user.Ok.make_posts
document.getElementById("comment-toggle").checked = user.Ok.comment
// toggle views
2024-02-13 12:18:04 -08:00
document.querySelector(".toggle-form").style.display = "block"
document.getElementById("username-form").style.display = "none"
document.getElementById("load-button").style.display = "none"
2024-02-13 14:55:21 -08:00
// update text at top
document.getElementById("data").innerHTML = `You're logged in as {{username}}. You're modifying ${username}.`
2024-02-13 12:18:04 -08:00
} else {
alert("User doesn't exist.")
}
break
}
case "admin-toggle": {
// admin toggle
2024-02-13 14:55:21 -08:00
let username = document.getElementById("username").value;
const update = await (await fetch("/toggleperms", {
method: "POST",
header: {"Content-Type": "application/json"},
body: JSON.stringify(
{
username: username,
perm: "admin",
value: document.getElementById("admin-toggle").checked
}
)
})).text()
if (update == "Done") {document.getElementById("admin-toggle").checked = !document.getElementById("admin-toggle").checked}
alert(update)
2024-02-13 12:18:04 -08:00
break
}
case "make-post-toggle": {
// make post toggle
2024-02-13 14:55:21 -08:00
let username = document.getElementById("username").value;
const update = await (await fetch("/toggleperms", {
method: "POST",
header: {"Content-Type": "application/json"},
body: JSON.stringify(
{
username: username,
perm: "make_posts",
value: document.getElementById("make-post-toggle").checked
}
)
})).text()
if (update == "Done") {document.getElementById("make-post-toggle").checked = !document.getElementById("make-post-toggle").checked}
alert(update)
2024-02-13 12:18:04 -08:00
break
}
case "comment-toggle": {
// comment toggle
2024-02-13 14:55:21 -08:00
let username = document.getElementById("username").value;
const update = await (await fetch("/toggleperms", {
method: "POST",
header: {"Content-Type": "application/json"},
body: JSON.stringify(
{
username: username,
perm: "comment",
value: document.getElementById("comment-toggle").checked
}
)
})).text()
if (update == "Done") {document.getElementById("comment-toggle").checked = !document.getElementById("comment-toggle").checked}
alert(update)
2024-02-13 12:18:04 -08:00
break
}
}
// const user = await fetch("/api/perms/ ")
// const token = await fetch("/login", {
// method: "POST",
// header: {"Content-Type": "application/json"},
// body: JSON.stringify({username, password})
// })
// alert(await token.text());
});
</script>
</html>