-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.html
More file actions
238 lines (202 loc) · 7.44 KB
/
Copy pathprofile.html
File metadata and controls
238 lines (202 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile Page</title>
<link rel="stylesheet" href="profile.css">
<script type="module" src="firebase.js"></script>
<script type="module" src="avatar.js"></script>
</head>
<body>
<h1>🪐 Your Profile</h1>
<button id="logoutBtn">Logout</button>
<div class="profile-box">
<h2>Profile Info</h2>
<input type="text" id="username" placeholder="Username">
<input type="text" id="bio" placeholder="Bio">
<button id="updateProfileBtn">Update Profile</button>
</div>
<!-- Avatar Selection -->
<div class="avatar-selection">
<h2>Choose Your Avatar</h2>
<div id="avatarContainer"></div>
</div>
<div class="notes-box">
<h2>To-Do / Notes</h2>
<input type="text" id="newNote" placeholder="Add a new note">
<button id="addNoteBtn">Add</button>
<ul id="notesList"></ul>
</div>
<!-- Navigation Bar -->
<nav class="navbar">
<h1>🪐 My Profile</h1>
<img id="navAvatar" src="avatars/avatar1.png" alt="Avatar" class="nav-avatar">
</nav>
<!-- Your existing profile-box, notes-box stay unchanged -->
<!-- Footer Avatar Selection -->
<footer class="avatar-footer">
<h3>Choose Your Avatar</h3>
<div id="avatarFooterContainer"></div>
</footer>
<div id="surpriseMsg" class="surprise-message"></div>
<script type="module">
import { auth, db } from './firebase.js';
import { onAuthStateChanged, signOut } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-auth.js";
import { doc, setDoc, getDoc, collection, addDoc, onSnapshot, deleteDoc, updateDoc } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-firestore.js";
// Profile elements
const usernameInput = document.getElementById('username');
const bioInput = document.getElementById('bio');
const updateBtn = document.getElementById('updateProfileBtn');
const newNoteInput = document.getElementById('newNote');
const addNoteBtn = document.getElementById('addNoteBtn');
const notesList = document.getElementById('notesList');
const logoutBtn = document.getElementById('logoutBtn');
const surpriseMsg = document.getElementById('surpriseMsg');
// Avatar elements
const avatarContainer = document.getElementById('avatarContainer');
const avatarCount = 8; // Number of avatars you have
let selectedAvatar = null;
// Create avatar images
for (let i = 1; i <= avatarCount; i++) {
const img = document.createElement('img');
img.src = `avatars/avatar${i}.png`;
img.classList.add('avatar-option');
img.dataset.avatar = `avatar${i}.png`;
avatarContainer.appendChild(img);
img.onclick = () => {
document.querySelectorAll('.avatar-option').forEach(a => a.classList.remove('selected-avatar'));
img.classList.add('selected-avatar');
selectedAvatar = img.dataset.avatar;
};
}
// Authentication and profile handling
onAuthStateChanged(auth, async (user) => {
if (!user) { window.location.href = "index.html"; return; }
const uid = user.uid;
// Logout
logoutBtn.onclick = async () => {
await signOut(auth);
window.location.href = "index.html";
};
// Load profile
const profileDoc = doc(db, "users", uid);
const profileSnap = await getDoc(profileDoc);
if (profileSnap.exists()) {
usernameInput.value = profileSnap.data().username || "";
bioInput.value = profileSnap.data().bio || "";
selectedAvatar = profileSnap.data().avatar || null;
// Highlight avatar if previously selected
if (selectedAvatar) {
const avatarImg = document.querySelector(`[data-avatar="${selectedAvatar}"]`);
if (avatarImg) avatarImg.classList.add('selected-avatar');
}
if (usernameInput.value.trim() !== "") {
surpriseMsg.innerHTML = `<span class="animate">✨ Hello ${usernameInput.value}! Have a great day! ✨</span>`;
}
}
// Update profile and avatar
updateBtn.onclick = async () => {
await setDoc(profileDoc, {
username: usernameInput.value,
bio: bioInput.value,
avatar: selectedAvatar
}, { merge: true });
alert("Profile Updated 🚀");
if (usernameInput.value.trim() !== "") {
surpriseMsg.innerHTML = `<span class="animate">✨ Hello ${usernameInput.value}! Have a great day! ✨</span>`;
}
};
// To-do notes
const notesCol = collection(db, "users", uid, "notes");
onSnapshot(notesCol, (snapshot) => {
notesList.innerHTML = "";
snapshot.forEach(docSnap => {
const li = document.createElement('li');
li.textContent = docSnap.data().text;
li.dataset.id = docSnap.id;
const delBtn = document.createElement('button');
delBtn.textContent = "Delete";
delBtn.onclick = async () => { await deleteDoc(doc(db, "users", uid, "notes", docSnap.id)); };
const editBtn = document.createElement('button');
editBtn.textContent = "Edit";
editBtn.onclick = async () => {
const newText = prompt("Edit note:", docSnap.data().text);
if (newText !== null) await updateDoc(doc(db, "users", uid, "notes", docSnap.id), { text: newText });
};
li.appendChild(editBtn);
li.appendChild(delBtn);
notesList.appendChild(li);
});
});
addNoteBtn.onclick = async () => {
const noteText = newNoteInput.value.trim();
if (!noteText) return;
await addDoc(notesCol, { text: noteText });
newNoteInput.value = "";
};
});
// === Avatar Logic ===
const avatarFooterContainer = document.getElementById('avatarFooterContainer');
const navAvatar = document.getElementById('navAvatar');
const avatarCount = 8;
let selectedAvatar = null;
onAuthStateChanged(auth, async (user) => {
if (!user) return;
const uid = user.uid;
const profileDoc = doc(db, "users", uid);
const profileSnap = await getDoc(profileDoc);
if (profileSnap.exists()) {
selectedAvatar = profileSnap.data().avatar || 'avatar1.png';
navAvatar.src = `avatars/${selectedAvatar}`;
} else {
selectedAvatar = 'avatar1.png'; // default avatar
navAvatar.src = `avatars/${selectedAvatar}`;
}
// Clear old avatars (avoid duplication if user reloads)
avatarFooterContainer.innerHTML = "";
for (let i = 1; i <= avatarCount; i++) {
const img = document.createElement('img');
img.src = `avatars/avatar${i}.png`;
img.classList.add('avatar-option');
img.dataset.avatar = `avatar${i}.png`;
// Highlight if matches saved avatar
if (img.dataset.avatar === selectedAvatar) {
img.classList.add('selected-avatar');
}
img.onclick = async () => {
// Remove highlight from all
document.querySelectorAll('.avatar-option').forEach(a => a.classList.remove('selected-avatar'));
// Add highlight to clicked one
img.classList.add('selected-avatar');
// Save + update navbar
selectedAvatar = img.dataset.avatar;
navAvatar.src = `avatars/${selectedAvatar}`;
await setDoc(profileDoc, { avatar: selectedAvatar }, { merge: true });
};
avatarFooterContainer.appendChild(img);
}
});
</script>
<style>
/* Avatar styling */
.avatar-option {
width: 80px;
height: 80px;
margin: 10px;
border-radius: 50%;
cursor: pointer;
border: 2px solid transparent;
transition: border 0.3s, transform 0.3s;
}
.avatar-option:hover {
transform: scale(1.1);
border-color: #00ffea;
}
.selected-avatar {
border-color: #ff00ff;
transform: scale(1.2);
}
</style>
</body>
</html>