Fixes avatar management

main
Yasen Pramatarov 2024-09-09 16:35:06 +03:00
parent 573df0fe4f
commit da5e5ec4ae
1 changed files with 18 additions and 5 deletions

View File

@ -122,14 +122,27 @@ document.getElementById('avatar-upload').addEventListener('change', function(eve
reader.readAsDataURL(event.target.files[0]); reader.readAsDataURL(event.target.files[0]);
}); });
const maxFileSize = 500 * 1024; // 500 KB in bytes
const currentAvatar = '<?= $app_root . htmlspecialchars($avatar) ?>'; // current avatar
document.getElementById('avatar-upload').addEventListener('change', function() { document.getElementById('avatar-upload').addEventListener('change', function() {
// Enable the "Upload" button when a file is selected
const uploadButton = document.getElementById('avatar-upload-button'); const uploadButton = document.getElementById('avatar-upload-button');
if (this.files.length > 0) { const file = this.files[0];
uploadButton.disabled = false;
uploadButton.className = 'avatar-btn btn btn-success'; if (file) {
// Check file size
if (file.size > maxFileSize) {
alert('File size exceeds 500 KB. Please select a smaller file.');
this.value = ''; // Clear the file input
uploadButton.disabled = true; // Keep the upload button disabled
uploadButton.className = 'avatar-btn btn btn-secondary';
document.querySelector('.avatar-img').src = currentAvatar;
} else {
// Enable the upload button if the file size is valid
uploadButton.disabled = false;
uploadButton.className = 'avatar-btn btn btn-success';
}
} else { } else {
uploadButton.disabled = true; uploadButton.disabled = true; // Disable the button if no file is selected
uploadButton.className = 'avatar-btn btn btn-secondary'; uploadButton.className = 'avatar-btn btn btn-secondary';
} }
}); });