-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserprofile.html
More file actions
324 lines (272 loc) · 11.4 KB
/
userprofile.html
File metadata and controls
324 lines (272 loc) · 11.4 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Setup Profile — SwapSkill</title>
<link rel="manifest" href="/manifest.json">
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js');
}
</script>
<link rel="icon" type="image/png" href="/logo.png">
<style>
:root {
--primary: #3b82f6;
--bg: #0f172a;
--text: #f8fafc;
--glass: rgba(30, 41, 59, 0.6);
--border: rgba(255, 255, 255, 0.1);
--danger: #ef4444;
--success: #22c55e;
}
body {
background: radial-gradient(circle at top right, #1e293b, #0f172a);
color: var(--text); min-height: 100vh; margin: 0;
font-family: system-ui, -apple-system, sans-serif;
display: flex; align-items: center; justify-content: center; padding: 20px;
}
/* Card */
.container {
background: var(--glass); backdrop-filter: blur(12px);
border: 1px solid var(--border); border-radius: 24px;
padding: 30px; width: 100%; max-width: 500px;
box-shadow: 0 20px 50px rgba(0,0,0,0.3);
animation: fadeIn 0.5s ease;
display: none;
}
h2 { margin: 0 0 10px 0; text-align: center; color: #fff; }
p.sub { text-align: center; color: #94a3b8; font-size: 14px; margin-bottom: 24px; }
.form-group { margin-bottom: 16px; position: relative; }
label { display: block; font-size: 13px; color: #94a3b8; margin-bottom: 6px; font-weight: 600; }
input, textarea {
width: 100%; padding: 12px; border-radius: 12px;
background: rgba(0,0,0,0.2); border: 1px solid var(--border);
color: #fff; outline: none; box-sizing: border-box; font-size: 14px;
transition: 0.2s;
}
input:focus, textarea:focus { border-color: var(--primary); background: rgba(0,0,0,0.4); }
/* Username Status Message */
.status-msg { font-size: 12px; margin-top: 4px; height: 16px; }
.status-msg.error { color: var(--danger); }
.status-msg.success { color: var(--success); }
/* Avatar Grid */
.avatar-label { text-align: center; margin-bottom: 12px; color: #fff; font-size: 14px; }
.avatar-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 24px; }
.avatar-opt {
width: 100%; aspect-ratio: 1; border-radius: 50%; object-fit: cover;
cursor: pointer; border: 2px solid transparent; transition: 0.2s;
background: #334155;
}
.avatar-opt:hover { transform: scale(1.1); }
.avatar-opt.selected { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(59,130,246,0.3); transform: scale(1.1); }
button {
width: 100%; padding: 14px; background: var(--primary);
border: none; border-radius: 12px; color: white; font-weight: 600;
cursor: pointer; margin-top: 10px; font-size: 16px; transition: 0.2s;
}
button:hover { background: #2563eb; transform: translateY(-2px); }
button:disabled { opacity: 0.7; cursor: not-allowed; }
@keyframes fadeIn { from{opacity:0;transform:translateY(20px)} to{opacity:1;transform:translateY(0)} }
/* Loader Overlay */
#loaderOverlay {
position: fixed; inset: 0; background: #0f172a; z-index: 999;
display: flex; align-items: center; justify-content: center;
transition: opacity 0.5s;
}
.loader { width: 50px; height: 50px; border: 4px solid rgba(255,255,255,0.1); border-top-color: var(--primary); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
</style>
</head>
<body>
<div id="loaderOverlay"><div class="loader"></div></div>
<div class="container" id="mainContent">
<h2 id="pageTitle">Setup Profile</h2>
<p class="sub">Let's get to know you better</p>
<div class="avatar-label">Choose an Avatar</div>
<div class="avatar-grid" id="avatarGrid"></div>
<div class="form-group">
<label>Full Name</label>
<input type="text" id="name" placeholder="e.g. Alex Smith">
</div>
<div class="form-group">
<label>Username</label>
<input type="text" id="username" placeholder="e.g. alex_s">
<div id="usernameMsg" class="status-msg"></div>
</div>
<div class="form-group">
<label>University / College</label>
<input type="text" id="university" placeholder="e.g. MIT">
</div>
<div class="form-group">
<label>Bio</label>
<textarea id="bio" rows="3" placeholder="I love coding and design..."></textarea>
</div>
<div class="form-group">
<label>Skills I Offer (comma separated)</label>
<input type="text" id="skillsOffered" placeholder="React, Python, Graphic Design">
</div>
<button id="saveBtn">Complete Setup</button>
</div>
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/12.3.0/firebase-app.js";
import { getAuth, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/12.3.0/firebase-auth.js";
import {
getFirestore, doc, getDoc, setDoc, serverTimestamp, collection, query, where, getDocs
} from "https://www.gstatic.com/firebasejs/12.3.0/firebase-firestore.js";
const app = initializeApp({
apiKey: "AIzaSyDwrZVBhR0VSbkbiktvkOsW3iSYJ6PFJ9A",
authDomain: "swapskill.web.app",
projectId: "swapskill-6e456"
});
const auth = getAuth(app);
const db = getFirestore(app);
let currentUID = null;
let selectedAvatar = 'avatars/a1.png'; // Fallback Default
let isEditMode = false; // Tracks if user exists or is new
const loader = document.getElementById('loaderOverlay');
const container = document.getElementById('mainContent');
const usernameInput = document.getElementById('username');
const usernameMsg = document.getElementById('usernameMsg');
// Avatars (Local)
const avatars = [
'avatars/a1.png', 'avatars/a2.png', 'avatars/a3.png', 'avatars/a4.png',
'avatars/a5.png', 'avatars/a6.png', 'avatars/a7.png', 'avatars/a8.png'
];
const grid = document.getElementById('avatarGrid');
// Initialize Avatar Grid
avatars.forEach(src => {
const img = document.createElement('img');
img.src = src;
img.className = 'avatar-opt';
img.onclick = () => {
document.querySelectorAll('.avatar-opt').forEach(el => el.classList.remove('selected'));
img.classList.add('selected');
selectedAvatar = src;
};
grid.appendChild(img);
});
// AUTH STATE CHANGE
onAuthStateChanged(auth, async u => {
if(!u) return location.href = 'login.html';
currentUID = u.uid;
// 1. Prepare Google Defaults
let googleName = u.displayName || "";
let googleUser = u.email ? u.email.split('@')[0].replace(/[^a-z0-9_]/gi, '').toLowerCase() : "";
// Default to Google Photo initially
if(u.photoURL) selectedAvatar = u.photoURL;
// 2. Check Database
const ref = doc(db, 'users', currentUID);
const snap = await getDoc(ref);
if(snap.exists()) {
isEditMode = true; // User exists
const d = snap.data();
// If they have data, pre-fill it
document.getElementById('name').value = d.name || googleName;
document.getElementById('username').value = d.username || googleUser;
document.getElementById('university').value = d.university || "";
document.getElementById('bio').value = d.bio || "";
document.getElementById('skillsOffered').value = d.skillsOffered ? d.skillsOffered.join(', ') : "";
if(d.avatarUrl) selectedAvatar = d.avatarUrl;
document.getElementById('saveBtn').innerText = "Save Changes";
document.getElementById('pageTitle').innerText = "Edit Profile";
} else {
// New User
isEditMode = false;
document.getElementById('name').value = googleName;
document.getElementById('username').value = googleUser;
// Highlight first avatar if no Google photo
if(!u.photoURL) document.querySelector('.avatar-opt').classList.add('selected');
}
// Highlight correct avatar in grid if it matches selected
document.querySelectorAll('.avatar-opt').forEach(img => {
if(img.getAttribute('src') === selectedAvatar) img.classList.add('selected');
});
// Show Form
loader.style.opacity = '0';
setTimeout(() => {
loader.style.display = 'none';
container.style.display = 'block';
}, 400);
});
// Real-time Username Check (with Debounce)
let timeout = null;
usernameInput.addEventListener('input', () => {
clearTimeout(timeout);
const val = usernameInput.value.trim();
usernameMsg.textContent = "Checking...";
usernameMsg.className = "status-msg";
if(val.length < 3) {
usernameMsg.textContent = "Too short";
return;
}
timeout = setTimeout(async () => {
// Query to check if username exists
const q = query(collection(db, "users"), where("username", "==", val));
const snap = await getDocs(q);
let taken = false;
snap.forEach(d => {
// If doc exists and ID is NOT mine, it's taken
if(d.id !== currentUID) taken = true;
});
if(taken) {
usernameMsg.textContent = "❌ Username taken";
usernameMsg.className = "status-msg error";
} else {
usernameMsg.textContent = "✅ Available";
usernameMsg.className = "status-msg success";
}
}, 500);
});
document.getElementById('saveBtn').onclick = async () => {
const name = document.getElementById('name').value.trim();
const username = document.getElementById('username').value.trim();
const university = document.getElementById('university').value.trim();
const bio = document.getElementById('bio').value.trim();
const skillsStr = document.getElementById('skillsOffered').value.trim();
const btn = document.getElementById('saveBtn');
if(!name || !username || !university) return alert("Please fill in Name, Username and University.");
// Username Final Validation
btn.innerText = "Validating...";
btn.disabled = true;
const q = query(collection(db, "users"), where("username", "==", username));
const snap = await getDocs(q);
let taken = false;
snap.forEach(d => { if(d.id !== currentUID) taken = true; });
if(taken) {
alert("Username is already taken! Please choose another.");
btn.innerText = isEditMode ? "Save Changes" : "Complete Setup";
btn.disabled = false;
return;
}
btn.innerText = "Saving...";
try {
const skillsArr = skillsStr.split(',').map(s => s.trim()).filter(s => s.length > 0);
// Prepare Data Object
const userData = {
name, username, university, bio,
avatarUrl: selectedAvatar,
skillsOffered: skillsArr,
email: auth.currentUser.email
};
// ONLY ADD THESE IF NEW USER (Fixes the Score/Date Reset Bug)
if (!isEditMode) {
userData.score = 0;
userData.createdAt = serverTimestamp();
userData.isBanned = false;
userData.verified = false;
}
// Save (Merge ensures we don't wipe existing fields like 'score' if we are in edit mode)
await setDoc(doc(db, 'users', currentUID), userData, { merge: true });
location.href = "dashboard.html";
} catch(e) {
console.error(e);
alert("Error saving: " + e.message);
btn.innerText = isEditMode ? "Save Changes" : "Complete Setup";
btn.disabled = false;
}
};
</script>
</body>
</html>