change to updateUI

This commit is contained in:
Ryan
2025-02-22 11:31:29 -05:00
committed by GitHub
parent 717b4d45c2
commit e1018b137e

View File

@@ -1,238 +1,265 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multi File Upload & Edit</title> <title>Multi File Upload & Edit</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="auth.js"></script> <script src="auth.js"></script>
<script src="upload.js"></script> <script src="upload.js"></script>
<script src="displayFileList.js"></script> <script src="displayFileList.js"></script>
<style> <style>
.container { margin-top: 10px; } .container { margin-top: 10px; }
#fileListContainer, #uploadForm, #addUserModal { display: none; } #fileListContainer, #uploadForm, #addUserModal { display: none; }
.logout-container { .logout-container {
position: absolute; position: absolute;
top: 10px; top: 10px;
right: 10px; right: 10px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: flex-end; align-items: flex-end;
gap: 5px; gap: 5px;
} }
@media (max-width: 768px) { @media (max-width: 768px) {
.logout-container { .logout-container {
position: static; position: static;
align-items: flex-end; align-items: flex-end;
text-align: right; text-align: right;
margin-top: 10px; margin-top: 10px;
} }
.logout-container button {
width: auto;
min-width: 120px;
}
}
.logout-container button { .modal {
width: auto; display: none;
min-width: 120px; position: fixed;
} top: 50%;
} left: 50%;
transform: translate(-50%, -50%);
.modal { background: white;
display: none; padding: 20px;
position: fixed; border: 1px solid black;
top: 50%; box-shadow: 0px 4px 6px rgba(0,0,0,0.1);
left: 50%; z-index: 1000;
transform: translate(-50%, -50%); width: 350px;
background: white; height: auto;
padding: 20px; display: flex;
border: 1px solid black; flex-direction: column;
box-shadow: 0px 4px 6px rgba(0,0,0,0.1); gap: 10px;
z-index: 1000; }
width: 350px; </style>
height: auto;
display: flex;
flex-direction: column;
gap: 10px;
}
</style>
</head> </head>
<body> <body>
<header class="text-center" style="position: relative;"> <header class="text-center" style="position: relative;">
<h1>Multi File Upload & Edit</h1> <h1>Multi File Upload & Edit</h1>
<div class="logout-container"> <div class="logout-container">
<button id="logoutBtn" class="btn btn-danger" style="display: none;">Logout</button> <button id="logoutBtn" class="btn btn-danger" style="display: none;">Logout</button>
<button id="addUserBtn" class="btn btn-success" style="display: none;">Add User</button> <button id="addUserBtn" class="btn btn-success" style="display: none;">Add User</button>
</div> </div>
</header> </header>
<div class="container"> <div class="container">
<div class="row" id="loginForm"> <div class="row" id="loginForm">
<div class="col-md-6"> <div class="col-md-6">
<form id="authForm" method="post"> <form id="authForm" method="post">
<div class="form-group"> <div class="form-group">
<label for="loginUsername">User:</label> <label for="loginUsername">User:</label>
<input type="text" class="form-control" id="loginUsername" name="username" required> <input type="text" class="form-control" id="loginUsername" name="username" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="loginPassword">Password:</label> <label for="loginPassword">Password:</label>
<input type="password" class="form-control" id="loginPassword" name="password" required> <input type="password" class="form-control" id="loginPassword" name="password" required>
</div> </div>
<button type="submit" class="btn btn-primary">Login</button> <button type="submit" class="btn btn-primary">Login</button>
</form> </form>
</div> </div>
</div>
<div class="row" id="uploadForm">
<div class="col-md-12">
<form id="uploadFileForm" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="file"></label>
<input type="file" id="file" name="file[]" class="form-control-file" multiple required style="width: 768px;">
</div>
<button type="submit" id="uploadBtn" class="btn btn-primary" disabled>Upload</button>
<div id="statusMessage"></div>
</form>
</div>
</div>
<div id="fileListContainer">
<h2>Uploaded Files</h2>
<button id="deleteSelectedBtn" class="btn btn-danger" style="margin-bottom: 10px; display: none;">Delete Selected</button>
<div id="fileList"></div>
</div>
<!-- Add User Modal -->
<div id="addUserModal" class="modal">
<h3>Create New User</h3>
<label for="newUsername">Username:</label>
<input type="text" id="newUsername" class="form-control">
<label for="newPassword">Password:</label>
<input type="password" id="newPassword" class="form-control">
<div>
<input type="checkbox" id="isAdmin">
<label for="isAdmin">Grant Admin Access</label>
</div>
<button id="saveUserBtn" class="btn btn-primary">Save User</button>
<button id="cancelUserBtn" class="btn btn-secondary">Cancel</button>
</div>
</div> </div>
<script> <div class="row" id="uploadForm">
<div class="col-md-12">
<form id="uploadFileForm" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="file"></label>
<input type="file" id="file" name="file[]" class="form-control-file" multiple required style="width: 768px;">
</div>
<button type="submit" id="uploadBtn" class="btn btn-primary" disabled>Upload</button>
<div id="statusMessage"></div>
</form>
</div>
</div>
<div id="fileListContainer">
<h2>Uploaded Files</h2>
<button id="deleteSelectedBtn" class="btn btn-danger" style="margin-bottom: 10px; display: none;">Delete Selected</button>
<div id="fileList"></div>
</div>
<!-- Add User Modal -->
<div id="addUserModal" class="modal">
<h3>Create New User</h3>
<label for="newUsername">Username:</label>
<input type="text" id="newUsername" class="form-control">
<label for="newPassword">Password:</label>
<input type="password" id="newPassword" class="form-control">
<div id="adminCheckboxContainer">
<input type="checkbox" id="isAdmin">
<label for="isAdmin">Grant Admin Access</label>
</div>
<button id="saveUserBtn" class="btn btn-primary">Save User</button>
<button id="cancelUserBtn" class="btn btn-secondary">Cancel</button>
</div>
</div>
<script>
// Global flag to determine if we are in setup mode.
window.setupMode = false;
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
checkAuthentication(); checkAuthentication();
function updateUI(data) { function updateUI(data) {
if (data.authenticated) { if (data.setup) {
document.getElementById("loginForm").style.display = "none"; // In setup mode, show the Add User Modal and hide other UI elements.
document.getElementById("uploadForm").style.display = "block"; document.getElementById("loginForm").style.display = "none";
document.getElementById("fileListContainer").style.display = "block"; document.getElementById("uploadForm").style.display = "none";
document.getElementById("logoutBtn").style.display = "block"; document.getElementById("fileListContainer").style.display = "none";
document.getElementById("logoutBtn").style.display = "none";
if (data.isAdmin) { document.getElementById("addUserBtn").style.display = "none";
document.getElementById("addUserBtn").style.display = "block"; // Pre-check and disable the admin checkbox.
} else { document.getElementById("isAdmin").checked = true;
document.getElementById("addUserBtn").style.display = "none"; document.getElementById("isAdmin").disabled = true;
} // Optionally hide the container (since it's always admin in setup mode).
document.getElementById("adminCheckboxContainer").style.display = "none";
loadFileList(); document.getElementById("addUserModal").style.display = "block";
} else { window.setupMode = true;
document.getElementById("loginForm").style.display = "block"; return;
document.getElementById("uploadForm").style.display = "none";
document.getElementById("fileListContainer").style.display = "none";
document.getElementById("logoutBtn").style.display = "none";
document.getElementById("addUserBtn").style.display = "none";
}
} }
function checkAuthentication() { // Normal authenticated user flow.
fetch("checkAuth.php") if (data.authenticated) {
.then(response => response.json()) document.getElementById("loginForm").style.display = "none";
.then(updateUI) document.getElementById("uploadForm").style.display = "block";
.catch(error => console.error("Error checking authentication:", error)); document.getElementById("fileListContainer").style.display = "block";
document.getElementById("logoutBtn").style.display = "block";
if (data.isAdmin) {
document.getElementById("addUserBtn").style.display = "block";
} else {
document.getElementById("addUserBtn").style.display = "none";
}
loadFileList();
} else {
document.getElementById("loginForm").style.display = "block";
document.getElementById("uploadForm").style.display = "none";
document.getElementById("fileListContainer").style.display = "none";
document.getElementById("logoutBtn").style.display = "none";
document.getElementById("addUserBtn").style.display = "none";
}
}
function checkAuthentication() {
fetch("checkAuth.php")
.then(response => response.json())
.then(updateUI)
.catch(error => console.error("Error checking authentication:", error));
}
document.getElementById("authForm").addEventListener("submit", function (event) {
event.preventDefault();
const formData = {
username: document.getElementById("loginUsername").value.trim(),
password: document.getElementById("loginPassword").value.trim()
};
fetch("auth.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(formData)
})
.then(response => response.json())
.then(data => {
if (data.success) {
updateUI({ authenticated: true, isAdmin: data.isAdmin });
} else {
alert("Login failed: " + (data.error || "Unknown error"));
}
})
.catch(error => console.error("Error logging in:", error));
});
document.getElementById("logoutBtn").addEventListener("click", function () {
fetch("logout.php", { method: "POST" })
.then(() => window.location.reload(true))
.catch(error => console.error("Logout error:", error));
});
document.getElementById("addUserBtn").addEventListener("click", function () {
resetUserForm();
document.getElementById("addUserModal").style.display = "block";
});
document.getElementById("saveUserBtn").addEventListener("click", function () {
const newUsername = document.getElementById("newUsername").value.trim();
const newPassword = document.getElementById("newPassword").value.trim();
// In setup mode, ignore the checkbox value (always admin)
const isAdmin = window.setupMode ? true : document.getElementById("isAdmin").checked;
if (!newUsername || !newPassword) {
alert("Username and password are required!");
return;
} }
document.getElementById("authForm").addEventListener("submit", function (event) { let url = "addUser.php";
event.preventDefault(); if (window.setupMode) {
url += "?setup=1";
}
const formData = { fetch(url, {
username: document.getElementById("loginUsername").value.trim(), method: "POST",
password: document.getElementById("loginPassword").value.trim() headers: { "Content-Type": "application/json" },
}; body: JSON.stringify({ username: newUsername, password: newPassword, isAdmin })
})
fetch("auth.php", { .then(response => response.json())
method: "POST", .then(data => {
headers: { "Content-Type": "application/json" }, if (data.success) {
body: JSON.stringify(formData) alert("User added successfully!");
})
.then(response => response.json())
.then(data => {
if (data.success) {
updateUI({ authenticated: true, isAdmin: data.isAdmin }); // Update UI immediately
} else {
alert("Login failed: " + (data.error || "Unknown error"));
}
})
.catch(error => console.error("Error logging in:", error));
});
document.getElementById("logoutBtn").addEventListener("click", function () {
fetch("logout.php", { method: "POST" })
.then(() => window.location.reload(true))
.catch(error => console.error("Logout error:", error));
});
document.getElementById("addUserBtn").addEventListener("click", function () {
resetUserForm();
document.getElementById("addUserModal").style.display = "block";
});
document.getElementById("saveUserBtn").addEventListener("click", function () {
const newUsername = document.getElementById("newUsername").value.trim();
const newPassword = document.getElementById("newPassword").value.trim();
const isAdmin = document.getElementById("isAdmin").checked;
if (!newUsername || !newPassword) {
alert("Username and password are required!");
return;
}
fetch("addUser.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: newUsername, password: newPassword, isAdmin })
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert("User added successfully!");
closeAddUserModal();
checkAuthentication(); // Refresh UI after adding user
} else {
alert("Error: " + (data.error || "Could not add user"));
}
})
.catch(error => console.error("Error adding user:", error));
});
document.getElementById("cancelUserBtn").addEventListener("click", function () {
closeAddUserModal(); closeAddUserModal();
}); // If this was the setup, reload the page to show the login form.
window.location.reload(true);
} else {
alert("Error: " + (data.error || "Could not add user"));
}
})
.catch(error => console.error("Error adding user:", error));
});
function closeAddUserModal() { document.getElementById("cancelUserBtn").addEventListener("click", function () {
document.getElementById("addUserModal").style.display = "none"; closeAddUserModal();
resetUserForm(); });
}
function resetUserForm() { function closeAddUserModal() {
document.getElementById("newUsername").value = ""; document.getElementById("addUserModal").style.display = "none";
document.getElementById("newPassword").value = ""; resetUserForm();
document.getElementById("isAdmin").checked = false; }
}
function resetUserForm() {
document.getElementById("newUsername").value = "";
document.getElementById("newPassword").value = "";
document.getElementById("isAdmin").checked = false;
document.getElementById("isAdmin").disabled = false;
document.getElementById("adminCheckboxContainer").style.display = "block";
}
}); });
</script> </script>
</body> </body>
</html> </html>