chore: javascript formatting

This commit is contained in:
Fu Zi Xiang 2023-10-13 14:10:06 +08:00
parent 574d2661a1
commit 4d20b49002
No known key found for this signature in database
5 changed files with 100 additions and 91 deletions

View File

@ -8,7 +8,6 @@
document document
.getElementById("usersBtn") .getElementById("usersBtn")
.addEventListener("click", function () { .addEventListener("click", function () {
// Redirect to the users page.
window.location.href = "/web/admin/users"; window.location.href = "/web/admin/users";
}); });
</script> </script>

View File

@ -1,15 +1,14 @@
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<body> <body>
<div id="content"> <div id="content">
<h1>User Management</h1> <h1>User Management</h1>
<p>Welcome, {{ email }}</p> <p>Welcome, {{ email }}</p>
</div> </div>
<div id="menu"> <div id="menu">
<button id="adminBtn">Admin</button> <button id="adminBtn">Admin</button>
<button id="logoutBtn">Logout</button> <button id="logoutBtn">Logout</button>
</div> </div>
<script> <script>
@ -18,27 +17,27 @@
.addEventListener("click", function () { .addEventListener("click", function () {
// Remove the cookie by setting its value to an empty string and its expiry date to the past. // Remove the cookie by setting its value to an empty string and its expiry date to the past.
fetch("/web-api/logout", { fetch("/web-api/logout", {
method: "POST", method: "POST",
headers: { credentials: "same-origin",
"Content-Type": "application/json",
},
credentials: 'same-origin',
}) })
.then(response => { .then((response) => {
if (!response.ok) { if (!response.ok) {
throw new Error('Network response was not ok' + response.statusText); throw new Error(
`Network response was not ok: ${response.statusText}`,
);
} }
window.location.href = "/web/login"; window.location.href = "/web/login";
}) })
.catch(error => { .catch((error) => {
console.error('There was a problem with the fetch operation:', error); console.error(
}); `There was a problem with the fetch operation: ${error}`,
);
});
}); });
document document
.getElementById("adminBtn") .getElementById("adminBtn")
.addEventListener("click", function () { .addEventListener("click", function () {
// Redirect to the admin page.
window.location.href = "/web/admin"; window.location.href = "/web/admin";
}); });
</script> </script>

View File

@ -39,20 +39,21 @@
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(data), body: JSON.stringify(data),
credentials: 'same-origin', credentials: "same-origin",
}) })
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
// If HTTP status code is not OK, throw an error with the status text // If HTTP status code is not OK, throw an error with the status text
throw Error(response.statusText); throw Error(response.statusText);
} }
window.location.href = "/web/home"; window.location.href = "/web/home";
}) })
.catch((error) => { .catch((error) => {
console.error("Error:", error); console.error(`Error:, ${error}`);
document.getElementById("response").innerText = document.getElementById(
"Login failed: " + error.message; "response",
}); ).innerText = `Login failed: ${error.message}`;
});
}); });
</script> </script>
</body> </body>

View File

@ -8,14 +8,14 @@
<button id="setPasswordBtn">Set Password</button> <button id="setPasswordBtn">Set Password</button>
</h2> </h2>
<div id="passwordModal" style="display: none;"> <div id="passwordModal" style="display: none">
<label for="newPassword">New Password:</label> <label for="newPassword">New Password:</label>
<input type="password" id="newPassword" name="newPassword"> <input type="password" id="newPassword" name="newPassword" />
<button id="submitPasswordBtn">Submit</button> <button id="submitPasswordBtn">Submit</button>
<button id="closePasswordModalBtn">Close</button> <button id="closePasswordModalBtn">Close</button>
</div> </div>
<br> <br />
<p>Phone: {{ user.phone|escape }}</p> <p>Phone: {{ user.phone|escape }}</p>
<p>Email Confirmed At: {{ user.email_confirmed_at|default("-") }}</p> <p>Email Confirmed At: {{ user.email_confirmed_at|default("-") }}</p>
@ -27,44 +27,50 @@
<a href="/web/admin/users">Back to User List</a> <a href="/web/admin/users">Back to User List</a>
<script> <script>
document.getElementById('setPasswordBtn').addEventListener('click', function() { document
document.getElementById('passwordModal').style.display = 'block'; .getElementById("setPasswordBtn")
}); .addEventListener("click", function () {
document.getElementById("passwordModal").style.display = "block";
});
document.getElementById('closePasswordModalBtn').addEventListener('click', function() { document
document.getElementById('passwordModal').style.display = 'none'; .getElementById("closePasswordModalBtn")
}); .addEventListener("click", function () {
document.getElementById("passwordModal").style.display = "none";
});
document.getElementById('submitPasswordBtn').addEventListener('click', function() { document
const newPassword = document.getElementById('newPassword').value; .getElementById("submitPasswordBtn")
let confirmed = confirm("Set new password?"); .addEventListener("click", function () {
if(confirmed && newPassword) { const newPassword = document.getElementById("newPassword").value;
fetch('/web-api/set_user_password', { let confirmed = confirm("Set new password?");
method: 'POST', if (confirmed && newPassword) {
fetch("/web-api/set_user_password", {
method: "POST",
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
userId: "{{ user.id }}", userId: "{{ user.id }}",
newPassword: newPassword, newPassword: newPassword,
}), }),
}) })
.then((response) => { .then((response) => {
if(!response.ok) { if (!response.ok) {
throw new Error('Network response was not ok'); throw new Error("Network response was not ok");
} }
return response.json(); return response.json();
}) })
.then((data) => { .then((data) => {
alert('Password set successfully!'); alert("Password set successfully!");
document.getElementById('passwordModal').style.display = 'none'; document.getElementById("passwordModal").style.display = "none";
}) })
.catch((error) => { .catch((error) => {
console.error('Error setting password:', error); console.error("Error setting password:", error);
alert('Failed to set password. Please try again later.'); alert("Failed to set password. Please try again later.");
}); });
} }
}); });
</script> </script>
</body> </body>
</html> </html>

View File

@ -25,31 +25,35 @@
</table> </table>
<script> <script>
document.getElementById('createUserBtn').addEventListener('click', function() { document
// Get the email from the user .getElementById("createUserBtn")
const email = prompt('Please enter the new user email:'); .addEventListener("click", function () {
if (email) { // Get the email from the user
fetch(`/web-api/user/${email}`, { const email = prompt("Please enter the new user email:");
method: "POST" if (email) {
}) fetch(`/web-api/user/${email}`, {
.then((response) => { method: "POST",
if (!response.ok) { })
throw new Error('Network response was not ok' + response.statusText); .then((response) => {
} if (!response.ok) {
return response.json(); throw new Error(
}) `Network response was not ok: ${response.statusText}`,
.then((data) => { );
if (data.code === 0) { }
window.location.href = "/web/admin/users/" + data.data.id; return response.json();
} else { })
throw new Error('Error creating user: ' + data.message); .then((data) => {
} if (data.code === 0) {
}) window.location.href = `/web/admin/users/${data.data.id}`;
.catch((error) => { } else {
console.error("Error:", error); throw new Error(`Error creating user: ${data.message}`);
}); }
} })
}); .catch((error) => {
console.error(`Error: ${error}`);
});
}
});
</script> </script>
</body> </body>
</html> </html>