Update index.html
This commit is contained in:
1 parent
d6cf42a95a
commit
62e3ef9dfc
1 file changed
+64
-243
+64
-243
@@ -3,8 +3,9 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Firebase CRUD App</title>
|
||||
<title>Firebase Firestore CRUD App</title>
|
||||
<style>
|
||||
/* (Keep all your existing CSS styles the same) */
|
||||
body {
|
||||
font-family: 'Inter', sans-serif;
|
||||
display: flex;
|
||||
@@ -16,182 +17,12 @@
|
||||
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;
|
||||
}
|
||||
/* ... (rest of your CSS remains identical) ... */
|
||||
</style>
|
||||
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap">
|
||||
</head>
|
||||
<body>
|
||||
<!-- (Keep all your HTML the same) -->
|
||||
<h1>User Management</h1>
|
||||
<form id="userForm" action="">
|
||||
<label for="name">Name: <input type="text" name="name" id="name" required> </label>
|
||||
@@ -218,21 +49,21 @@
|
||||
<script type="module">
|
||||
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-app.js";
|
||||
import {
|
||||
getDatabase,
|
||||
ref,
|
||||
push,
|
||||
set,
|
||||
onValue,
|
||||
update,
|
||||
remove,
|
||||
serverTimestamp
|
||||
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-database.js";
|
||||
getFirestore,
|
||||
collection,
|
||||
addDoc,
|
||||
getDocs,
|
||||
doc,
|
||||
updateDoc,
|
||||
deleteDoc,
|
||||
onSnapshot,
|
||||
serverTimestamp
|
||||
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore.js";
|
||||
|
||||
// Your web app's Firebase configuration
|
||||
// Your Firebase configuration
|
||||
const firebaseConfig = {
|
||||
apiKey: "AIzaSyDoRv9_xpy19uoyVRVEjGYB-Ckr3ZyruTE",
|
||||
authDomain: "nosql-demo-bd597.firebaseapp.com",
|
||||
databaseURL: "https://nosql-demo-bd597-default-rtdb.firebaseio.com",
|
||||
projectId: "nosql-demo-bd597",
|
||||
storageBucket: "nosql-demo-bd597.appspot.com",
|
||||
messagingSenderId: "813176623239",
|
||||
@@ -241,10 +72,10 @@
|
||||
|
||||
// Initialize Firebase
|
||||
const app = initializeApp(firebaseConfig);
|
||||
const database = getDatabase(app);
|
||||
const usersRef = ref(database, 'users');
|
||||
const db = getFirestore(app);
|
||||
const usersCollection = collection(db, "users");
|
||||
|
||||
// Modal functions
|
||||
// Modal functions (keep the same)
|
||||
function showCustomAlert(title, message) {
|
||||
const modal = document.getElementById('customAlertModal');
|
||||
document.getElementById('modalTitle').textContent = title;
|
||||
@@ -256,7 +87,7 @@
|
||||
document.getElementById('customAlertModal').classList.remove('visible');
|
||||
});
|
||||
|
||||
// DOM Elements
|
||||
// DOM Elements (keep the same)
|
||||
const form = document.getElementById('userForm');
|
||||
const submitButton = document.getElementById('submitButton');
|
||||
const cancelEdit = document.getElementById('cancelEdit');
|
||||
@@ -265,7 +96,7 @@
|
||||
let isEditing = false;
|
||||
let currentEditId = null;
|
||||
|
||||
// Form submission
|
||||
// Form submission - Firestore version
|
||||
form.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const name = document.getElementById('name').value;
|
||||
@@ -280,32 +111,27 @@
|
||||
try {
|
||||
if (isEditing) {
|
||||
// Update existing user
|
||||
await update(ref(database, `users/${currentEditId}`), userData);
|
||||
const userRef = doc(db, "users", currentEditId);
|
||||
await updateDoc(userRef, userData);
|
||||
showCustomAlert('✅ Success!', 'User updated successfully!');
|
||||
} else {
|
||||
// Create new user
|
||||
userData.createdAt = serverTimestamp();
|
||||
const newUserRef = push(usersRef);
|
||||
await set(newUserRef, userData);
|
||||
showCustomAlert('✅ Success!', `User created with ID: ${newUserRef.key}`);
|
||||
const docRef = await addDoc(usersCollection, userData);
|
||||
showCustomAlert('✅ Success!', `User created with ID: ${docRef.id}`);
|
||||
}
|
||||
form.reset();
|
||||
resetFormState();
|
||||
} catch (error) {
|
||||
showCustomAlert('⚠ Error', 'Operation failed');
|
||||
console.error('Firebase error:', error);
|
||||
console.error('Firestore error:', error);
|
||||
}
|
||||
});
|
||||
|
||||
// Cancel edit
|
||||
// Cancel edit (keep the same)
|
||||
cancelEdit.addEventListener('click', resetFormState);
|
||||
|
||||
// Refresh user list
|
||||
refreshButton.addEventListener('click', () => {
|
||||
loadUsers();
|
||||
});
|
||||
|
||||
// Reset form to "add" state
|
||||
// Reset form (keep the same)
|
||||
function resetFormState() {
|
||||
isEditing = false;
|
||||
currentEditId = null;
|
||||
@@ -314,34 +140,34 @@
|
||||
document.getElementById('userId').value = '';
|
||||
}
|
||||
|
||||
// Display users in the list
|
||||
// Display users - Firestore version
|
||||
function displayUsers(users) {
|
||||
userList.innerHTML = '';
|
||||
|
||||
if (!users) {
|
||||
if (users.empty) {
|
||||
userList.innerHTML = '<p>No users found</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
Object.keys(users).forEach(key => {
|
||||
const user = users[key];
|
||||
users.forEach((doc) => {
|
||||
const user = doc.data();
|
||||
const userCard = document.createElement('div');
|
||||
userCard.className = 'user-card';
|
||||
userCard.innerHTML = `
|
||||
<div>
|
||||
<strong>${user.name}</strong>
|
||||
<p>${user.email}</p>
|
||||
<small>ID: ${key}</small>
|
||||
<small>ID: ${doc.id}</small>
|
||||
</div>
|
||||
<div class="user-actions">
|
||||
<button class="edit" data-id="${key}">Edit</button>
|
||||
<button class="delete" data-id="${key}">Delete</button>
|
||||
<button class="edit" data-id="${doc.id}">Edit</button>
|
||||
<button class="delete" data-id="${doc.id}">Delete</button>
|
||||
</div>
|
||||
`;
|
||||
userList.appendChild(userCard);
|
||||
});
|
||||
|
||||
// Add event listeners to edit and delete buttons
|
||||
// Add event listeners
|
||||
document.querySelectorAll('.user-actions .edit').forEach(button => {
|
||||
button.addEventListener('click', (e) => editUser(e.target.dataset.id));
|
||||
});
|
||||
@@ -351,60 +177,55 @@
|
||||
});
|
||||
}
|
||||
|
||||
// Load users from Firebase
|
||||
function loadUsers() {
|
||||
onValue(usersRef, (snapshot) => {
|
||||
const users = snapshot.val();
|
||||
console.log("Users loaded:", users);
|
||||
displayUsers(users);
|
||||
}, {
|
||||
onlyOnce: true
|
||||
});
|
||||
// Load users - Firestore version
|
||||
async function loadUsers() {
|
||||
const querySnapshot = await getDocs(usersCollection);
|
||||
displayUsers(querySnapshot);
|
||||
}
|
||||
|
||||
// Edit user
|
||||
// Edit user - Firestore version
|
||||
async function editUser(id) {
|
||||
try {
|
||||
const userRef = ref(database, `users/${id}`);
|
||||
onValue(userRef, (snapshot) => {
|
||||
const user = snapshot.val();
|
||||
if (user) {
|
||||
document.getElementById('name').value = user.name;
|
||||
document.getElementById('email').value = user.email;
|
||||
document.getElementById('userId').value = id;
|
||||
|
||||
isEditing = true;
|
||||
currentEditId = id;
|
||||
submitButton.textContent = 'Update User';
|
||||
cancelEdit.style.display = 'inline-block';
|
||||
}
|
||||
}, { onlyOnce: true });
|
||||
} catch (error) {
|
||||
showCustomAlert('⚠ Error', 'Could not load user data');
|
||||
console.error('Edit user error:', error);
|
||||
const userRef = doc(db, "users", id);
|
||||
const userSnap = await getDoc(userRef);
|
||||
|
||||
if (userSnap.exists()) {
|
||||
const user = userSnap.data();
|
||||
document.getElementById('name').value = user.name;
|
||||
document.getElementById('email').value = user.email;
|
||||
document.getElementById('userId').value = id;
|
||||
|
||||
isEditing = true;
|
||||
currentEditId = id;
|
||||
submitButton.textContent = 'Update User';
|
||||
cancelEdit.style.display = 'inline-block';
|
||||
} else {
|
||||
showCustomAlert('⚠ Error', 'User not found');
|
||||
}
|
||||
}
|
||||
|
||||
// Delete user
|
||||
// Delete user - Firestore version
|
||||
async function deleteUser(id) {
|
||||
if (!confirm('Are you sure you want to delete this user?')) return;
|
||||
|
||||
try {
|
||||
await remove(ref(database, `users/${id}`));
|
||||
await deleteDoc(doc(db, "users", id));
|
||||
showCustomAlert('✅ Success!', 'User deleted successfully');
|
||||
} catch (error) {
|
||||
showCustomAlert('⚠ Error', 'Could not delete user');
|
||||
console.error('Delete user error:', error);
|
||||
console.error('Delete error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Real-time listener for users
|
||||
onValue(usersRef, (snapshot) => {
|
||||
displayUsers(snapshot.val());
|
||||
// Real-time listener - Firestore version
|
||||
onSnapshot(usersCollection, (querySnapshot) => {
|
||||
displayUsers(querySnapshot);
|
||||
});
|
||||
|
||||
// Initial load
|
||||
loadUsers();
|
||||
|
||||
// Refresh button
|
||||
document.getElementById('refreshUsers').addEventListener('click', loadUsers);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user