73 lines
1.8 KiB
JavaScript
73 lines
1.8 KiB
JavaScript
function updateAllAvatars() {
|
|
const avatarContent = currentUserAvatar
|
|
? `<img src="${currentUserAvatar}" alt="${currentUser}">`
|
|
: currentUser.charAt(0).toUpperCase();
|
|
|
|
// Update all avatar locations (убран profileHeaderAvatarMain)
|
|
const avatarLocations = [
|
|
"profileHeaderAvatar",
|
|
"profileHeaderAvatarDesktop",
|
|
"currentUserAvatar",
|
|
];
|
|
|
|
avatarLocations.forEach((id) => {
|
|
const element = document.getElementById(id);
|
|
if (element) {
|
|
element.innerHTML = avatarContent;
|
|
}
|
|
});
|
|
|
|
// Update profile modal avatar
|
|
updateProfileModal();
|
|
}
|
|
|
|
// Function to upload avatar
|
|
async function uploadAvatar(file) {
|
|
const formData = new FormData();
|
|
formData.append("avatar", file);
|
|
|
|
try {
|
|
const response = await fetch("/api/upload-avatar", {
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: `Bearer ${currentToken}`,
|
|
},
|
|
body: formData,
|
|
});
|
|
|
|
const data = await response.json();
|
|
if (data.success) {
|
|
currentUserAvatar = data.avatar_url;
|
|
updateAllAvatars();
|
|
showAlert("Avatar uploaded successfully!", "success");
|
|
loadChats(); // Refresh chats to show updated avatar
|
|
} else {
|
|
showAlert(data.message, "error");
|
|
}
|
|
} catch (error) {
|
|
console.error("Error uploading avatar:", error);
|
|
showAlert("Error uploading avatar", "error");
|
|
}
|
|
}
|
|
|
|
// Avatar upload handler
|
|
document.getElementById("avatarInput").addEventListener("change", function (e) {
|
|
const file = e.target.files[0];
|
|
if (file) {
|
|
// Validate file type
|
|
if (!file.type.startsWith("image/")) {
|
|
showAlert("Please select an image file", "error");
|
|
return;
|
|
}
|
|
|
|
// Validate file size (5MB)
|
|
if (file.size > 5 * 1024 * 1024) {
|
|
showAlert("Image must be less than 5MB", "error");
|
|
return;
|
|
}
|
|
|
|
uploadAvatar(file);
|
|
e.target.value = ""; // Reset input
|
|
}
|
|
});
|