fix: Multiple images not saving in new post creation
Problem: - Only cover image was being saved when creating posts - Section images were shown in preview but not sent to backend - File inputs were not preserved after section saving Solution: - Store section files in global JavaScript storage - Send all section images in FormData during form submission - Update backend to process section_image_* files - Add is_cover flag distinction between cover and section images - Preserve file references throughout the editing process Features: - Multiple images per post section now work correctly - Cover image marked with is_cover=True - Section images marked with is_cover=False - Proper file cleanup when images are removed - Enhanced logging for debugging image uploads
This commit is contained in:
@@ -244,6 +244,8 @@
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// Global storage for section files
|
||||
window.sectionFiles = {};
|
||||
let sectionCounter = 0;
|
||||
|
||||
// Populate form from URL parameters
|
||||
@@ -418,18 +420,40 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
|
||||
imageInput.addEventListener('change', function() {
|
||||
const files = Array.from(this.files);
|
||||
|
||||
// Store files in global storage for this section
|
||||
if (!window.sectionFiles[sectionId]) {
|
||||
window.sectionFiles[sectionId] = [];
|
||||
}
|
||||
|
||||
files.forEach(file => {
|
||||
if (file.type.startsWith('image/')) {
|
||||
// Add to global storage
|
||||
window.sectionFiles[sectionId].push(file);
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
const imagePreview = document.createElement('div');
|
||||
imagePreview.className = 'image-preview';
|
||||
imagePreview.dataset.fileIndex = window.sectionFiles[sectionId].length - 1;
|
||||
imagePreview.innerHTML = `
|
||||
<img src="${e.target.result}" alt="Preview">
|
||||
<div class="remove-btn">×</div>
|
||||
`;
|
||||
|
||||
imagePreview.querySelector('.remove-btn').addEventListener('click', function() {
|
||||
// Remove from global storage
|
||||
const fileIndex = parseInt(imagePreview.dataset.fileIndex);
|
||||
window.sectionFiles[sectionId].splice(fileIndex, 1);
|
||||
|
||||
// Update indices for remaining previews
|
||||
const remainingPreviews = imagesPreview.querySelectorAll('.image-preview');
|
||||
remainingPreviews.forEach((preview, index) => {
|
||||
if (parseInt(preview.dataset.fileIndex) > fileIndex) {
|
||||
preview.dataset.fileIndex = parseInt(preview.dataset.fileIndex) - 1;
|
||||
}
|
||||
});
|
||||
|
||||
imagePreview.remove();
|
||||
});
|
||||
|
||||
@@ -609,6 +633,19 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
if (gpxFile) {
|
||||
formData.append('gpx_file', gpxFile);
|
||||
}
|
||||
|
||||
// Add section images from all saved sections
|
||||
let imageCounter = 0;
|
||||
savedSections.forEach((section, sectionIndex) => {
|
||||
const sectionId = section.dataset.sectionId;
|
||||
if (window.sectionFiles[sectionId] && window.sectionFiles[sectionId].length > 0) {
|
||||
window.sectionFiles[sectionId].forEach((file, fileIndex) => {
|
||||
formData.append(`section_image_${imageCounter}`, file);
|
||||
formData.append(`section_image_${imageCounter}_section`, sectionIndex);
|
||||
imageCounter++;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Show loading state
|
||||
const submitBtn = document.querySelector('button[type="submit"]');
|
||||
|
||||
Reference in New Issue
Block a user