-
Notifications
You must be signed in to change notification settings - Fork 0
Fix image uploads #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Image preview functionality for profile pictures | ||
|
|
||
| import { uploadCircuitImage } from './api.js' | ||
| import { showNotification } from './notifications.js' | ||
| import { loadRaceSettings, populateSettingsModal } from './settings.js' | ||
|
|
||
| /** | ||
| * Setup image preview for file input | ||
| * @param {string} fileInputId - ID of the file input element | ||
| * @param {string} previewContainerId - ID of the preview container | ||
| * @param {string} previewImageId - ID of the preview image element | ||
| */ | ||
| export function setupImagePreview(fileInputId, previewContainerId, previewImageId) { | ||
| const fileInput = document.getElementById(fileInputId) | ||
| const previewContainer = document.getElementById(previewContainerId) | ||
| const previewImage = document.getElementById(previewImageId) | ||
|
|
||
| if (!fileInput || !previewContainer || !previewImage) { | ||
| return | ||
| } | ||
|
|
||
| fileInput.addEventListener('change', async (event) => { | ||
| const file = event.target.files[0] | ||
|
|
||
| if (file) { | ||
| // Validate file type | ||
| if (!file.type.startsWith('image/')) { | ||
| showNotification('Please select a valid image file', 'error') | ||
| clearPreview(previewContainerId, previewImageId) | ||
| return | ||
| } | ||
|
|
||
| // Validate file size (5MB limit) | ||
| const maxSize = 5 * 1024 * 1024 // 5MB in bytes | ||
| if (file.size > maxSize) { | ||
| showNotification('File size must be less than 5MB', 'error') | ||
| clearPreview(previewContainerId, previewImageId) | ||
| fileInput.value = '' // Clear the input | ||
| return | ||
| } | ||
|
|
||
| // Create and display preview | ||
| const reader = new FileReader() | ||
| reader.onload = (e) => { | ||
| previewImage.src = e.target.result | ||
| previewContainer.style.display = 'block' | ||
| } | ||
| reader.readAsDataURL(file) | ||
|
|
||
| // For circuit images, upload immediately | ||
| if (fileInputId === 'circuitImageUpload') { | ||
| try { | ||
| await uploadCircuitImage(file) | ||
| showNotification('Circuit image uploaded successfully!', 'success') | ||
|
|
||
| // Refresh the race settings and modal | ||
| await loadRaceSettings() | ||
| populateSettingsModal() | ||
|
|
||
| // Clear the preview since we now show the actual uploaded image | ||
| clearPreview(previewContainerId, previewImageId) | ||
| fileInput.value = '' // Clear the file input | ||
| } catch (_error) { | ||
| showNotification('Failed to upload circuit image. Please try again.', 'error') | ||
| clearPreview(previewContainerId, previewImageId) | ||
| fileInput.value = '' // Clear the file input | ||
| } | ||
| } | ||
| } else { | ||
| clearPreview(previewContainerId, previewImageId) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Clear image preview | ||
| * @param {string} previewContainerId - ID of the preview container | ||
| * @param {string} previewImageId - ID of the preview image element | ||
| */ | ||
| export function clearPreview(previewContainerId, previewImageId) { | ||
| const previewContainer = document.getElementById(previewContainerId) | ||
| const previewImage = document.getElementById(previewImageId) | ||
|
|
||
| if (previewContainer) { | ||
| previewContainer.style.display = 'none' | ||
| } | ||
|
|
||
| if (previewImage) { | ||
| previewImage.src = '' | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clear all previews (useful when modals are closed) | ||
| */ | ||
| export function clearAllPreviews() { | ||
| clearPreview('addPicturePreviewContainer', 'addPicturePreview') | ||
| clearPreview('editPicturePreviewContainer', 'editPicturePreview') | ||
| clearPreview('circuitImagePreviewContainer', 'circuitImagePreview') | ||
| } | ||
|
|
||
| /** | ||
| * Setup all image previews for the application | ||
| */ | ||
| export function initializeImagePreviews() { | ||
| // Setup preview for add driver modal | ||
| setupImagePreview('profilePictureAdd', 'addPicturePreviewContainer', 'addPicturePreview') | ||
|
|
||
| // Setup preview for edit driver modal | ||
| setupImagePreview('profilePictureEdit', 'editPicturePreviewContainer', 'editPicturePreview') | ||
|
|
||
| // Setup preview for circuit image upload | ||
| setupImagePreview('circuitImageUpload', 'circuitImagePreviewContainer', 'circuitImagePreview') | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,3 +1,4 @@ | ||||||
| import { clearPreview } from './imagePreview.js' | ||||||
| import { showNotification } from './notifications.js' | ||||||
| import { findDriverById, getIsAuthorized } from './state.js' | ||||||
|
|
||||||
|
|
@@ -16,6 +17,7 @@ export function showAddDriverModal() { | |||||
| export function closeAddDriverModal() { | ||||||
| document.getElementById('addDriverModal').style.display = 'none' | ||||||
| document.getElementById('addDriverForm').reset() | ||||||
| clearPreview('addPicturePreviewContainer', 'addPicturePreview') | ||||||
| } | ||||||
|
|
||||||
| // Show edit driver modal | ||||||
|
|
@@ -56,6 +58,7 @@ export function showEditDriverModal(driverId) { | |||||
| export function closeEditDriverModal() { | ||||||
| document.getElementById('editDriverModal').style.display = 'none' | ||||||
| document.getElementById('editDriverForm').reset() | ||||||
| clearPreview('editPicturePreviewContainer', 'editPicturePreview') | ||||||
| } | ||||||
|
|
||||||
| // Show settings modal | ||||||
|
|
@@ -83,6 +86,7 @@ export function showSettingsModal() { | |||||
| export function closeSettingsModal() { | ||||||
| document.getElementById('settingsModal').style.display = 'none' | ||||||
| document.getElementById('settingsForm').reset() | ||||||
| clearPreview('circuitImagePreviewContainer', 'circuitImagePreview') | ||||||
| } | ||||||
|
|
||||||
| // Setup modal event listeners | ||||||
|
|
@@ -110,6 +114,10 @@ export function triggerFileUpload() { | |||||
| document.getElementById('profilePictureInput').click() | ||||||
| } | ||||||
|
|
||||||
| export function triggerEditFileUpload() { | ||||||
| document.getElementById('profilePictureEdit').click() | ||||||
| } | ||||||
|
|
||||||
| export function triggerCircuitImageUpload() { | ||||||
| document.getElementById('circuitImageInput').click() | ||||||
| document.getElementById('circuitImageUpload').click() | ||||||
|
||||||
| document.getElementById('circuitImageUpload').click() | |
| document.getElementById('profilePictureInput').click() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file input ID 'profilePictureAdd' doesn't match the actual ID 'profilePictureAdd' in the HTML, but the HTML shows 'profilePictureAdd' while other references in the codebase use 'profilePictureInput'. This mismatch could prevent the preview functionality from working for the add driver modal.