Files
2025-06-04 14:36:33 +08:00

757 lines
24 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 Firestore CRUD App</title>
<style>
:root {
--primary-color: #4361ee;
--secondary-color: #3f37c9;
--accent-color: #4895ef;
--danger-color: #f72585;
--success-color: #4cc9f0;
--light-color: #f8f9fa;
--dark-color: #212529;
--text-color: #2b2d42;
--border-radius: 12px;
--box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
--transition: all 0.3s ease;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Poppins', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
color: var(--text-color);
background-color: #f8f9fa;
padding: 20px;
min-height: 100vh;
}
h1, h2, h3 {
font-family: 'Montserrat', sans-serif;
font-weight: 700;
line-height: 1.2;
margin-bottom: 1rem;
color: var(--dark-color);
}
h1 {
font-size: 2.5rem;
background: linear-gradient(90deg, #4361ee, #3f37c9);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
text-align: center;
margin: 1.5rem 0;
letter-spacing: -0.5px;
}
h2 {
font-size: 1.8rem;
color: var(--primary-color);
margin-top: 2rem;
}
.container {
max-width: 1200px;
margin: 0 auto;
width: 100%;
}
form {
background-color: white;
padding: 2rem;
border-radius: var(--border-radius);
box-shadow: var(--box-shadow);
margin-bottom: 2rem;
width: 100%;
max-width: 500px;
margin-left: auto;
margin-right: auto;
}
label {
display: block;
margin-bottom: 1rem;
font-weight: 500;
color: var(--text-color);
}
input[type="text"],
input[type="email"] {
width: 100%;
padding: 0.8rem 1rem;
border: 2px solid #e9ecef;
border-radius: var(--border-radius);
font-size: 1rem;
transition: var(--transition);
margin-top: 0.5rem;
}
input[type="text"]:focus,
input[type="email"]:focus {
border-color: var(--accent-color);
outline: none;
box-shadow: 0 0 0 3px rgba(72, 149, 239, 0.25);
}
.button-group {
display: flex;
gap: 1rem;
margin-top: 1.5rem;
flex-wrap: wrap;
}
button {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0.8rem 1.5rem;
border: none;
border-radius: var(--border-radius);
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: var(--transition);
font-family: 'Poppins', sans-serif;
}
.btn-primary {
background-color: var(--primary-color);
color: white;
flex: 1;
min-width: 120px;
}
.btn-primary:hover {
background-color: var(--secondary-color);
transform: translateY(-2px);
}
.btn-secondary {
background-color: #6c757d;
color: white;
}
.btn-secondary:hover {
background-color: #5a6268;
transform: translateY(-2px);
}
.btn-danger {
background-color: var(--danger-color);
color: white;
}
.btn-danger:hover {
background-color: #e3176a;
transform: translateY(-2px);
}
.btn-success {
background-color: var(--success-color);
color: white;
}
.btn-success:hover {
background-color: #3aa8d8;
transform: translateY(-2px);
}
.btn-sm {
padding: 0.5rem 1rem;
font-size: 0.9rem;
}
.crud-section {
background-color: white;
padding: 2rem;
border-radius: var(--border-radius);
box-shadow: var(--box-shadow);
width: 100%;
margin: 0 auto;
}
.user-list {
margin-top: 1.5rem;
}
.user-card {
background-color: white;
border: 1px solid #e9ecef;
border-radius: var(--border-radius);
padding: 1.5rem;
margin-bottom: 1rem;
transition: var(--transition);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.user-card:hover {
transform: translateY(-3px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
}
.user-card h3 {
color: var(--primary-color);
font-size: 1.3rem;
margin-bottom: 0.5rem;
}
.user-card p {
margin-bottom: 0.5rem;
color: #495057;
}
.user-card small {
color: #6c757d;
font-size: 0.8rem;
}
.user-actions {
display: flex;
gap: 0.5rem;
margin-top: 1rem;
flex-wrap: wrap;
}
.modal-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: var(--transition);
}
.modal-overlay.visible {
opacity: 1;
visibility: visible;
}
.modal-content {
background-color: white;
padding: 2rem;
border-radius: var(--border-radius);
max-width: 400px;
width: 90%;
text-align: center;
transform: translateY(-20px);
transition: var(--transition);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}
.modal-overlay.visible .modal-content {
transform: translateY(0);
}
.modal-content h2 {
margin-bottom: 1rem;
color: var(--primary-color);
}
.modal-content p {
margin-bottom: 1.5rem;
}
/* Responsive adjustments */
@media (max-width: 768px) {
h1 {
font-size: 2rem;
}
h2 {
font-size: 1.5rem;
}
form {
padding: 1.5rem;
}
.user-card {
padding: 1rem;
}
.button-group {
flex-direction: column;
}
button {
width: 100%;
}
}
@media (max-width: 480px) {
body {
padding: 15px;
}
h1 {
font-size: 1.8rem;
}
.crud-section {
padding: 1rem;
}
}
/* Animation */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.user-card {
animation: fadeIn 0.3s ease-out forwards;
}
/* Loading spinner */
.spinner {
display: inline-block;
width: 20px;
height: 20px;
border: 3px solid rgba(255, 255, 255, 0.3);
border-radius: 50%;
border-top-color: white;
animation: spin 1s ease-in-out infinite;
margin-right: 8px;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
/* Empty state */
.empty-state {
text-align: center;
padding: 2rem;
color: #6c757d;
}
.empty-state svg {
width: 80px;
height: 80px;
margin-bottom: 1rem;
opacity: 0.6;
}
.dark-mode-toggle {
position: absolute;
top: 20px;
right: 20px;
}
.dark-mode-toggle {
position: fixed;
top: 20px;
right: 20px;
z-index: 1000;
}
#darkModeToggle {
padding: 8px 15px;
border-radius: 20px;
border: none;
background: #f0f0f0;
cursor: pointer;
display: flex;
align-items: center;
gap: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
/* Dark Mode Styles */
body.dark-mode {
background-color: #121212;
color: #fff;
}
.dark-mode form,
.dark-mode .crud-section {
background-color: #1e1e1e;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
}
.dark-mode input[type="text"],
.dark-mode input[type="email"] {
background-color: #333;
color: #fff;
border-color: #444;
}
.dark-mode .user-card {
background-color: #2a2a2a;
border-color: #444;
}
.dark-mode .modal-content {
background-color: #2a2a2a;
color: #e0e0e0;
}
</style>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@700&family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
<div class="container">
<h1>User Management</h1>
<div class="dark-mode-toggle">
<button id="darkModeToggle">
<span id="darkModeIcon">🌙</span> Dark Mode
</button>
</div>
<form id="userForm">
<label for="name">
Name
<input type="text" name="name" id="name" required placeholder="Enter user name">
</label>
<label for="email">
Email
<input type="email" name="email" id="email" required placeholder="Enter user email">
</label>
<input type="hidden" id="userId">
<div class="button-group">
<button type="submit" id="submitButton" class="btn-primary">
<span id="submitText">Add User</span>
</button>
<button type="button" id="cancelEdit" class="btn-secondary" style="display: none;">
Cancel
</button>
</div>
</form>
<div class="crud-section">
<div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1rem;">
<h2>User List</h2>
<button id="refreshUsers" class="btn-success">
<i class="fas fa-sync-alt" style="margin-right: 8px;"></i>
Refresh
</button>
</div>
<div class="user-list" id="userList">
<!-- Users will be loaded here -->
</div>
</div>
</div>
<div id="customAlertModal" class="modal-overlay">
<div class="modal-content">
<h2 id="modalTitle">Title</h2>
<p id="modalMessage">Message</p>
<button id="modalCloseButton" class="btn-primary">OK</button>
</div>
</div>
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.0/firebase-app.js";
import {
getFirestore,
collection,
addDoc,
getDocs,
doc,
updateDoc,
deleteDoc,
onSnapshot,
getDoc,
serverTimestamp
} from "https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore.js";
// Your Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyDoRv9_xpy19uoyVRVEjGYB-Ckr3ZyruTE",
authDomain: "nosql-demo-bd597.firebaseapp.com",
projectId: "nosql-demo-bd597",
storageBucket: "nosql-demo-bd597.appspot.com",
messagingSenderId: "813176623239",
appId: "1:813176623239:web:aa855f2e3fe29dfc3ca822"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const usersCollection = collection(db, "users");
// Modal functions
function showCustomAlert(title, message) {
const modal = document.getElementById('customAlertModal');
document.getElementById('modalTitle').textContent = title;
document.getElementById('modalMessage').textContent = 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 submitText = document.getElementById('submitText');
const cancelEdit = document.getElementById('cancelEdit');
const userList = document.getElementById('userList');
const refreshButton = document.getElementById('refreshUsers');
let isEditing = false;
let currentEditId = null;
// Dark Mode Functionality
const darkModeToggle = document.getElementById('darkModeToggle');
const darkModeIcon = document.getElementById('darkModeIcon');
// Check for saved preference or system preference
function initDarkMode() {
const savedMode = localStorage.getItem('darkMode');
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (savedMode === 'enabled' || (!savedMode && systemPrefersDark)) {
enableDarkMode();
}
}
function enableDarkMode() {
document.body.classList.add('dark-mode');
localStorage.setItem('darkMode', 'enabled');
darkModeIcon.textContent = '☀️';
darkModeToggle.textContent = ' Light Mode';
}
function disableDarkMode() {
document.body.classList.remove('dark-mode');
localStorage.removeItem('darkMode');
darkModeIcon.textContent = '🌙';
darkModeToggle.textContent = ' Dark Mode';
}
darkModeToggle.addEventListener('click', () => {
if (document.body.classList.contains('dark-mode')) {
disableDarkMode();
} else {
enableDarkMode();
}
});
// Initialize dark mode on page load
initDarkMode();
// Form submission
form.addEventListener('submit', async (e) => {
e.preventDefault();
const name = document.getElementById('name').value.trim();
const email = document.getElementById('email').value.trim();
if (!name || !email) {
showCustomAlert('⚠️ Validation Error', 'Please fill in all fields');
return;
}
// Show loading state
submitButton.disabled = true;
submitText.innerHTML = '<span class="spinner"></span> Processing...';
const userData = {
name,
email,
updatedAt: serverTimestamp()
};
try {
if (isEditing) {
// Update existing user
const userRef = doc(db, "users", currentEditId);
await updateDoc(userRef, userData);
showCustomAlert('✅ Success!', 'User updated successfully!');
} else {
// Create new user
userData.createdAt = serverTimestamp();
const docRef = await addDoc(usersCollection, userData);
showCustomAlert('🎉 User Created!', `New user added successfully!`);
}
form.reset();
resetFormState();
} catch (error) {
showCustomAlert('⚠️ Error', 'Operation failed. Please try again.');
console.error('Firestore error:', error);
} finally {
// Reset button state
submitButton.disabled = false;
submitText.textContent = isEditing ? 'Update User' : 'Add User';
}
});
// Cancel edit
cancelEdit.addEventListener('click', resetFormState);
// Refresh button
refreshButton.addEventListener('click', loadUsers);
// Reset form to "add" state
function resetFormState() {
isEditing = false;
currentEditId = null;
document.getElementById('name').value = '';
document.getElementById('email').value = '';
document.getElementById('userId').value = '';
submitText.textContent = 'Add User';
cancelEdit.style.display = 'none';
}
// Display users
function displayUsers(users) {
userList.innerHTML = '';
if (users.empty) {
userList.innerHTML = `
<div class="empty-state">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z" />
</svg>
<h3>No Users Found</h3>
<p>Add your first user using the form above</p>
</div>
`;
return;
}
users.forEach((doc) => {
const user = doc.data();
const createdAt = user.createdAt?.toDate() || new Date();
const updatedAt = user.updatedAt?.toDate() || new Date();
const userCard = document.createElement('div');
userCard.className = 'user-card';
userCard.innerHTML = `
<h3>${user.name}</h3>
<p><i class="fas fa-envelope" style="margin-right: 8px; color: #6c757d;"></i>${user.email}</p>
<small>
<i class="fas fa-id-card" style="margin-right: 5px;"></i>ID: ${doc.id}
</small>
<small style="display: block; margin-top: 8px;">
<i class="fas fa-calendar-plus" style="margin-right: 5px;"></i>
Created: ${createdAt.toLocaleDateString()}
</small>
<small style="display: block;">
<i class="fas fa-calendar-check" style="margin-right: 5px;"></i>
Updated: ${updatedAt.toLocaleDateString()}
</small>
<div class="user-actions">
<button class="btn-primary btn-sm edit" data-id="${doc.id}">
<i class="fas fa-edit" style="margin-right: 5px;"></i>Edit
</button>
<button class="btn-danger btn-sm delete" data-id="${doc.id}">
<i class="fas fa-trash-alt" style="margin-right: 5px;"></i>Delete
</button>
</div>
`;
userList.appendChild(userCard);
});
// Add event listeners
document.querySelectorAll('.edit').forEach(button => {
button.addEventListener('click', (e) => editUser(e.currentTarget.dataset.id));
});
document.querySelectorAll('.delete').forEach(button => {
button.addEventListener('click', (e) => deleteUser(e.currentTarget.dataset.id));
});
}
// Load users
async function loadUsers() {
try {
refreshButton.innerHTML = '<span class="spinner"></span> Loading...';
const querySnapshot = await getDocs(usersCollection);
displayUsers(querySnapshot);
} catch (error) {
showCustomAlert('⚠️ Error', 'Failed to load users');
console.error('Load users error:', error);
} finally {
refreshButton.innerHTML = '<i class="fas fa-sync-alt" style="margin-right: 8px;"></i> Refresh';
}
}
// Edit user
async function editUser(id) {
try {
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;
submitText.textContent = 'Update User';
cancelEdit.style.display = 'inline-block';
// Scroll to form
form.scrollIntoView({ behavior: 'smooth' });
} else {
showCustomAlert('⚠️ Error', 'User not found');
}
} catch (error) {
showCustomAlert('⚠️ Error', 'Failed to load user data');
console.error('Edit user error:', error);
}
}
// Delete user
async function deleteUser(id) {
try {
const userRef = doc(db, "users", id);
const userSnap = await getDoc(userRef);
if (!userSnap.exists()) {
showCustomAlert('⚠️ Error', 'User not found');
return;
}
const user = userSnap.data();
if (!confirm(`Are you sure you want to delete ${user.name}? This action cannot be undone.`)) {
return;
}
await deleteDoc(userRef);
showCustomAlert('✅ Deleted!', 'User has been removed successfully.');
} catch (error) {
showCustomAlert('⚠️ Error', 'Could not delete user');
console.error('Delete error:', error);
}
}
// Real-time listener
onSnapshot(usersCollection, (querySnapshot) => {
displayUsers(querySnapshot);
});
// Initial load
loadUsers();
</script>
</body>
</html>