Reworked flow

This commit is contained in:
Djeex
2025-08-16 11:17:15 +02:00
parent 1b0b228273
commit 041db66b3d
5 changed files with 125 additions and 145 deletions

View File

@ -1,61 +1,39 @@
// Upload handler for gallery images
// --- Upload gallery images ---
document.getElementById('upload-gallery').addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return; // Exit if no file is selected
const files = e.target.files;
if (!files.length) return;
// Create a FormData object to send the file
const formData = new FormData();
formData.append('file', file); // Key must match what upload.py expects
for (const file of files) formData.append('files', file);
try {
// Send POST request to the gallery upload endpoint
const res = await fetch('/api/gallery/upload', {
method: 'POST',
body: formData
});
const res = await fetch('/api/gallery/upload', { method: 'POST', body: formData });
const data = await res.json();
if (res.ok) {
alert('✅ Gallery image uploaded!');
refreshGallery(); // Refresh the gallery list from the server
} else {
alert('Error: ' + data.error); // Show server error if upload failed
}
} catch (err) {
console.error(err);
alert('Server error!'); // Network or server failure
} finally {
e.target.value = ''; // Reset file input so the same file can be uploaded again if needed
}
alert(`${data.uploaded.length} gallery image(s) uploaded!`);
refreshGallery();
} else alert('Error: ' + data.error);
} catch(err) {
console.error(err); alert('Server error!');
} finally { e.target.value = ''; }
});
// Upload handler for hero images
// --- Upload hero images ---
document.getElementById('upload-hero').addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return; // Exit if no file is selected
const files = e.target.files;
if (!files.length) return;
// Create a FormData object to send the file
const formData = new FormData();
formData.append('file', file); // Key must match what upload.py expects
for (const file of files) formData.append('files', file);
try {
// Send POST request to the hero upload endpoint
const res = await fetch('/api/hero/upload', {
method: 'POST',
body: formData
});
const res = await fetch('/api/hero/upload', { method: 'POST', body: formData });
const data = await res.json();
if (res.ok) {
alert('✅ Hero image uploaded!');
refreshHero(); // Refresh the hero list from the server
} else {
alert('Error: ' + data.error); // Show server error if upload failed
}
} catch (err) {
console.error(err);
alert('Server error!'); // Network or server failure
} finally {
e.target.value = ''; // Reset file input so the same file can be uploaded again if needed
}
alert(`${data.uploaded.length} hero image(s) uploaded!`);
refreshHero();
} else alert('Error: ' + data.error);
} catch(err) {
console.error(err); alert('Server error!');
} finally { e.target.value = ''; }
});