Connect profile settings to user data
This commit is contained in:
1 parent
4f7e9bac68
commit
d3b1d01058
13 files changed
+258
-32
No files matched your search
@@ -13,6 +13,7 @@
|
||||
<link rel="stylesheet" href="../css/login.css">
|
||||
<link rel="stylesheet" href="../css/topbar.css">
|
||||
<link rel="stylesheet" href="../css/settings.css">
|
||||
<script src="../js/profile-shared.js"></script>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
||||
</head>
|
||||
<body>
|
||||
@@ -40,7 +41,7 @@
|
||||
|
||||
<!-- Tab Content: Profile -->
|
||||
<div id="profileTab" class="tab-content active">
|
||||
<form class="settings-form">
|
||||
<form class="settings-form" id="profileSettingsForm">
|
||||
<div class="form-group">
|
||||
<label for="avatar">Profile Picture</label>
|
||||
<div class="avatar-upload">
|
||||
@@ -51,11 +52,11 @@
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="displayName">Display Name</label>
|
||||
<input type="text" id="displayName" placeholder="Sunny the Cat">
|
||||
<input type="text" id="displayName" placeholder="Display name">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="username">Username</label>
|
||||
<input type="text" id="username" placeholder="@username">
|
||||
<input type="text" id="username" placeholder="username" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="bio">Bio</label>
|
||||
@@ -63,10 +64,11 @@
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="email">Email Address</label>
|
||||
<input type="email" id="email" placeholder="your@email.com">
|
||||
<input type="email" id="email" placeholder="your@email.com" readonly>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="save-btn">Save Changes</button>
|
||||
<p id="profileSaveStatus" style="margin-top:10px;color:#64748b;text-align:center;"></p>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@@ -154,7 +156,29 @@
|
||||
});
|
||||
});
|
||||
|
||||
// Avatar preview (simple)
|
||||
let currentAvatarUrl = '';
|
||||
let currentSlug = '';
|
||||
|
||||
function setProfileForm(profile) {
|
||||
document.getElementById('displayName').value = profile.displayName || '';
|
||||
document.getElementById('username').value = profile.user?.userName || '';
|
||||
document.getElementById('email').value = profile.user?.email || '';
|
||||
document.getElementById('bio').value = profile.bio || '';
|
||||
currentAvatarUrl = profile.avatarUrl || '../images/content/cat.png';
|
||||
currentSlug = profile.slug || profile.user?.userName || '';
|
||||
avatarPreview.src = currentAvatarUrl;
|
||||
}
|
||||
|
||||
async function loadProfileSettings() {
|
||||
try {
|
||||
const profile = await window.loadCurrentProfile();
|
||||
setProfileForm(profile);
|
||||
} catch (error) {
|
||||
document.getElementById('profileSaveStatus').textContent = 'Profile could not be loaded.';
|
||||
}
|
||||
}
|
||||
|
||||
// Avatar preview and save as data URL
|
||||
const avatarUpload = document.getElementById('avatarUpload');
|
||||
const avatarPreview = document.getElementById('avatarPreview');
|
||||
if (avatarUpload) {
|
||||
@@ -163,15 +187,53 @@
|
||||
if (file && (file.type === 'image/png' || file.type === 'image/jpeg')) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(ev) {
|
||||
avatarPreview.src = ev.target.result;
|
||||
currentAvatarUrl = ev.target.result;
|
||||
avatarPreview.src = currentAvatarUrl;
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Demo form submits for all forms
|
||||
document.querySelectorAll('.settings-form').forEach(form => {
|
||||
document.getElementById('profileSettingsForm').addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const status = document.getElementById('profileSaveStatus');
|
||||
status.textContent = 'Saving...';
|
||||
|
||||
try {
|
||||
const userName = document.getElementById('username').value.trim();
|
||||
const response = await fetch('/api/v1/profiles', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify({
|
||||
displayName: document.getElementById('displayName').value.trim(),
|
||||
userName,
|
||||
slug: userName || currentSlug,
|
||||
bio: document.getElementById('bio').value.trim(),
|
||||
avatarUrl: currentAvatarUrl,
|
||||
specialities: null,
|
||||
isPublic: false
|
||||
})
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
location.href = '/login';
|
||||
return;
|
||||
}
|
||||
if (!response.ok) throw new Error('Profile could not be saved.');
|
||||
|
||||
const savedProfile = await window.loadCurrentProfile(true);
|
||||
window.setCurrentProfile(savedProfile);
|
||||
setProfileForm(savedProfile);
|
||||
status.textContent = 'Saved.';
|
||||
} catch (error) {
|
||||
status.textContent = error.message;
|
||||
}
|
||||
});
|
||||
|
||||
// Demo form submits for forms that are not connected yet
|
||||
document.querySelectorAll('.settings-form:not(#profileSettingsForm)').forEach(form => {
|
||||
form.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
alert('Settings saved (demo)');
|
||||
@@ -192,6 +254,8 @@
|
||||
fetch('/topbar.html')
|
||||
.then(r => r.text())
|
||||
.then(data => document.getElementById('topbar-container').innerHTML = data);
|
||||
|
||||
loadProfileSettings();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
Reference in new issue
Block a user