393 lines
12 KiB
HTML
393 lines
12 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Firebase CRUD App</title>
|
|
<style>
|
|
body{
|
|
font-family: 'Inter', sans-serif;
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
align-items: center;
|
|
min-height: 100vh;
|
|
background-color: #f0f2f5;
|
|
margin: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
h1{
|
|
color: #333;
|
|
margin-bottom: 30px;
|
|
font-size: 2.5em;
|
|
text-align: center;
|
|
}
|
|
|
|
form{
|
|
background-color: #ffffff;
|
|
padding: 40px;
|
|
border-radius: 15px;
|
|
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 20px;
|
|
width: 100%;
|
|
max-width: 450px;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
label{
|
|
font-size: 1.1em;
|
|
color: #555;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 8px;
|
|
}
|
|
|
|
input[type="text"],
|
|
input[type="email"]{
|
|
padding: 12px 15px;
|
|
border: 1px solid #ccc;
|
|
border-radius: 8px;
|
|
font-size: 1em;
|
|
width: 100%;
|
|
box-sizing: border-box;
|
|
transition: border-color 0.3s ease;
|
|
}
|
|
|
|
input[type="text"]:focus,
|
|
input[type="email"]:focus{
|
|
border-color: #007bff;
|
|
outline: none;
|
|
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
|
|
}
|
|
|
|
button{
|
|
background-color: #007bff;
|
|
color: white;
|
|
padding: 15px 25px;
|
|
border: none;
|
|
border-radius: 10px;
|
|
font-size: 1.2em;
|
|
cursor: pointer;
|
|
transition: background-color 0.3s ease, transform 0.2s ease;
|
|
box-shadow: 0 4px 10px rgba(0, 123, 255, 0.3);
|
|
}
|
|
|
|
button:hover{
|
|
background-color: #0056b3;
|
|
transform: translateY(-2px);
|
|
}
|
|
|
|
button:active{
|
|
transform: translateY(0);
|
|
box-shadow: 0 2px 5px rgba(0, 123, 255, 0.3);
|
|
}
|
|
|
|
.modal-overlay{
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
height: 100%;
|
|
width: 100%;
|
|
background: rgba(0,0,0,0.6);
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
z-index: 1000;
|
|
opacity: 0;
|
|
visibility: hidden;
|
|
transition: opacity 0.3s ease, visibility 0.3s ease;
|
|
}
|
|
|
|
.modal-overlay.visible{
|
|
opacity: 1;
|
|
visibility: visible;
|
|
}
|
|
|
|
.modal-content{
|
|
background: white;
|
|
padding: 30px;
|
|
border-radius: 10px;
|
|
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
|
|
text-align: center;
|
|
max-width: 400px;
|
|
width: 90%;
|
|
transform: translateY(-20px);
|
|
transition: transform 0.3s ease;
|
|
}
|
|
|
|
.modal-overlay.visible .modal-content{
|
|
transform: translateY(0);
|
|
}
|
|
|
|
.modal-content h2{
|
|
margin-top: 0;
|
|
color: #666;
|
|
}
|
|
|
|
.modal-content button{
|
|
background-color: #007bff;
|
|
color: white;
|
|
padding: 10px 20px;
|
|
border: none;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
font-size: 1em;
|
|
}
|
|
|
|
.modal-content button:hover{
|
|
background-color: #0056b3;
|
|
}
|
|
|
|
/* CRUD Operations Section */
|
|
.crud-section {
|
|
background-color: #ffffff;
|
|
padding: 30px;
|
|
border-radius: 15px;
|
|
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
|
|
margin-top: 30px;
|
|
width: 100%;
|
|
max-width: 800px;
|
|
}
|
|
|
|
.user-list {
|
|
margin-top: 20px;
|
|
}
|
|
|
|
.user-card {
|
|
background: #f8f9fa;
|
|
padding: 15px;
|
|
border-radius: 8px;
|
|
margin-bottom: 10px;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
}
|
|
|
|
.user-actions button {
|
|
padding: 8px 12px;
|
|
margin-left: 8px;
|
|
font-size: 0.9em;
|
|
}
|
|
|
|
.user-actions button.delete {
|
|
background-color: #dc3545;
|
|
}
|
|
|
|
.user-actions button.delete:hover {
|
|
background-color: #c82333;
|
|
}
|
|
|
|
.user-actions button.edit {
|
|
background-color: #ffc107;
|
|
color: #212529;
|
|
}
|
|
|
|
.user-actions button.edit:hover {
|
|
background-color: #e0a800;
|
|
}
|
|
</style>
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap">
|
|
</head>
|
|
<body>
|
|
|
|
<h1>User Management</h1>
|
|
<form id="userForm" action="">
|
|
<label for="name">Name: <input type="text" name="name" id="name" required> </label>
|
|
<label for="email">Email: <input type="email" name="email" id="email" required> </label>
|
|
<input type="hidden" id="userId">
|
|
<button type="submit" id="submitButton">Add User</button>
|
|
<button type="button" id="cancelEdit" style="display: none; background-color: #6c757d;">Cancel Edit</button>
|
|
</form>
|
|
|
|
<div class="crud-section">
|
|
<h2>User List</h2>
|
|
<button id="refreshUsers">Refresh List</button>
|
|
<div class="user-list" id="userList"></div>
|
|
</div>
|
|
|
|
<div id="customAlertModal" class="modal-overlay">
|
|
<div class="modal-content">
|
|
<h2 id="modalTitle"></h2>
|
|
<p id="modalMessage"></p>
|
|
<button id="modalCloseButton">OK</button>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
// Modal functions
|
|
function showCustomAlert(title, message){
|
|
const modal = document.getElementById('customAlertModal');
|
|
document.getElementById('modalTitle').innerText = title;
|
|
document.getElementById('modalMessage').innerText = message;
|
|
modal.classList.add('visible');
|
|
}
|
|
|
|
document.getElementById('modalCloseButton').addEventListener('click', () => {
|
|
document.getElementById('customAlertModal').classList.remove('visible');
|
|
});
|
|
|
|
// DOM Elements
|
|
const form = document.getElementById('userForm');
|
|
const submitButton = document.getElementById('submitButton');
|
|
const cancelEdit = document.getElementById('cancelEdit');
|
|
const userList = document.getElementById('userList');
|
|
const refreshButton = document.getElementById('refreshUsers');
|
|
let isEditing = false;
|
|
let currentEditId = null;
|
|
|
|
// Form submission
|
|
form.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const data = Object.fromEntries(new FormData(form).entries());
|
|
|
|
try {
|
|
let response;
|
|
if (isEditing) {
|
|
// Update existing user
|
|
response = await fetch(`/users/${currentEditId}`, {
|
|
method: 'PUT',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify(data)
|
|
});
|
|
} else {
|
|
// Create new user
|
|
response = await fetch('/users', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify(data)
|
|
});
|
|
}
|
|
|
|
if (response.ok) {
|
|
const result = await response.json();
|
|
showCustomAlert('✅ Success!', isEditing
|
|
? 'User updated successfully!'
|
|
: `User created with ID: ${result.id}`);
|
|
form.reset();
|
|
resetFormState();
|
|
fetchUsers();
|
|
} else {
|
|
const error = await response.json();
|
|
showCustomAlert('❌ Error!', error.error || 'Operation failed');
|
|
}
|
|
} catch (error) {
|
|
showCustomAlert('⚠ Network Error', 'Could not connect to the server.');
|
|
console.error('Fetch error:', error);
|
|
}
|
|
});
|
|
|
|
// Cancel edit
|
|
cancelEdit.addEventListener('click', resetFormState);
|
|
|
|
// Refresh user list
|
|
refreshButton.addEventListener('click', fetchUsers);
|
|
|
|
// Reset form to "add" state
|
|
function resetFormState() {
|
|
isEditing = false;
|
|
currentEditId = null;
|
|
submitButton.textContent = 'Add User';
|
|
cancelEdit.style.display = 'none';
|
|
document.getElementById('userId').value = '';
|
|
}
|
|
|
|
// Fetch all users
|
|
async function fetchUsers() {
|
|
try {
|
|
const response = await fetch('/users');
|
|
if (response.ok) {
|
|
const users = await response.json();
|
|
displayUsers(users);
|
|
} else {
|
|
throw new Error('Failed to fetch users');
|
|
}
|
|
} catch (error) {
|
|
showCustomAlert('⚠ Error', 'Could not load users');
|
|
console.error('Fetch users error:', error);
|
|
}
|
|
}
|
|
|
|
// Display users in the list
|
|
function displayUsers(users) {
|
|
userList.innerHTML = '';
|
|
if (users.length === 0) {
|
|
userList.innerHTML = '<p>No users found</p>';
|
|
return;
|
|
}
|
|
|
|
users.forEach(user => {
|
|
const userCard = document.createElement('div');
|
|
userCard.className = 'user-card';
|
|
userCard.innerHTML = `
|
|
<div>
|
|
<strong>${user.name}</strong>
|
|
<p>${user.email}</p>
|
|
<small>ID: ${user.id}</small>
|
|
</div>
|
|
<div class="user-actions">
|
|
<button class="edit" data-id="${user.id}">Edit</button>
|
|
<button class="delete" data-id="${user.id}">Delete</button>
|
|
</div>
|
|
`;
|
|
userList.appendChild(userCard);
|
|
});
|
|
|
|
// Add event listeners to edit and delete buttons
|
|
document.querySelectorAll('.user-actions .edit').forEach(button => {
|
|
button.addEventListener('click', (e) => editUser(e.target.dataset.id));
|
|
});
|
|
|
|
document.querySelectorAll('.user-actions .delete').forEach(button => {
|
|
button.addEventListener('click', (e) => deleteUser(e.target.dataset.id));
|
|
});
|
|
}
|
|
|
|
// Edit user
|
|
async function editUser(id) {
|
|
try {
|
|
const response = await fetch(`/users/${id}`);
|
|
if (response.ok) {
|
|
const user = await response.json();
|
|
document.getElementById('name').value = user.name;
|
|
document.getElementById('email').value = user.email;
|
|
document.getElementById('userId').value = user.id;
|
|
|
|
isEditing = true;
|
|
currentEditId = user.id;
|
|
submitButton.textContent = 'Update User';
|
|
cancelEdit.style.display = 'inline-block';
|
|
} else {
|
|
throw new Error('Failed to fetch user');
|
|
}
|
|
} catch (error) {
|
|
showCustomAlert('⚠ Error', 'Could not load user data');
|
|
console.error('Edit user error:', error);
|
|
}
|
|
}
|
|
|
|
// Delete user
|
|
async function deleteUser(id) {
|
|
if (!confirm('Are you sure you want to delete this user?')) return;
|
|
|
|
try {
|
|
const response = await fetch(`/users/${id}`, { method: 'DELETE' });
|
|
if (response.ok) {
|
|
showCustomAlert('✅ Success!', 'User deleted successfully');
|
|
fetchUsers();
|
|
} else {
|
|
throw new Error('Failed to delete user');
|
|
}
|
|
} catch (error) {
|
|
showCustomAlert('⚠ Error', 'Could not delete user');
|
|
console.error('Delete user error:', error);
|
|
}
|
|
}
|
|
|
|
// Initial load
|
|
fetchUsers();
|
|
</script>
|
|
</body>
|
|
</html> |