From 205dcae2bc00079090358e41eaa914a3e2bc7a16 Mon Sep 17 00:00:00 2001 From: Djeex Date: Thu, 4 Sep 2025 12:16:08 +0200 Subject: [PATCH] Refactored upload.js --- src/webui/js/upload.js | 104 +++++++---------------------------------- 1 file changed, 17 insertions(+), 87 deletions(-) diff --git a/src/webui/js/upload.js b/src/webui/js/upload.js index 8316b6e..27e745f 100644 --- a/src/webui/js/upload.js +++ b/src/webui/js/upload.js @@ -11,107 +11,37 @@ function hideLoader() { if (loader) loader.classList.remove("active"); } - -// --- Upload gallery images --- -const galleryInput = document.getElementById('upload-gallery'); -if (galleryInput) { - galleryInput.addEventListener('change', async (e) => { +// --- Generic upload handler --- +function setupUpload(inputId, apiUrl, loaderText, successMsg, refreshFn) { + const input = document.getElementById(inputId); + if (!input) return; + input.addEventListener('change', async (e) => { const files = e.target.files; if (!files.length) return; - showLoader("Uploading photos..."); + showLoader(loaderText); const formData = new FormData(); for (const file of files) formData.append('files', file); try { - const res = await fetch('/api/gallery/upload', { method: 'POST', body: formData }); + const res = await fetch(apiUrl, { method: 'POST', body: formData }); const data = await res.json(); hideLoader(); if (res.ok) { - showToast(`✅ ${data.uploaded.length} gallery image(s) uploaded!`, "success"); - if (typeof refreshGallery === "function") refreshGallery(); + showToast(`✅ ${data.uploaded.length} ${successMsg}`, "success"); + if (typeof refreshFn === "function") refreshFn(); } else showToast('Error: ' + data.error, "error"); } catch(err) { hideLoader(); console.error(err); showToast('Server error!', "error"); - } finally { e.target.value = ''; } + } finally { + e.target.value = ''; + } }); } -// --- Upload hero images --- -const heroInput = document.getElementById('upload-hero'); -if (heroInput) { - heroInput.addEventListener('change', async (e) => { - const files = e.target.files; - if (!files.length) return; - showLoader("Uploading hero photos..."); - const formData = new FormData(); - for (const file of files) formData.append('files', file); - - try { - const res = await fetch('/api/hero/upload', { method: 'POST', body: formData }); - const data = await res.json(); - hideLoader(); - if (res.ok) { - showToast(`✅ ${data.uploaded.length} hero image(s) uploaded!`, "success"); - if (typeof refreshHero === "function") refreshHero(); - } else showToast('Error: ' + data.error, "error"); - } catch(err) { - hideLoader(); - console.error(err); - showToast('Server error!', "error"); - } finally { e.target.value = ''; } - }); -} - -// --- Upload gallery images (bottom button) --- -const galleryInputBottom = document.getElementById('upload-gallery-bottom'); -if (galleryInputBottom) { - galleryInputBottom.addEventListener('change', async (e) => { - const files = e.target.files; - if (!files.length) return; - showLoader("Uploading photos..."); - const formData = new FormData(); - for (const file of files) formData.append('files', file); - - try { - const res = await fetch('/api/gallery/upload', { method: 'POST', body: formData }); - const data = await res.json(); - hideLoader(); - if (res.ok) { - showToast(`✅ ${data.uploaded.length} gallery image(s) uploaded!`, "success"); - if (typeof refreshGallery === "function") refreshGallery(); - } else showToast('Error: ' + data.error, "error"); - } catch(err) { - hideLoader(); - console.error(err); - showToast('Server error!', "error"); - } finally { e.target.value = ''; } - }); -} - -// --- Upload hero images (bottom button) --- -const heroInputBottom = document.getElementById('upload-hero-bottom'); -if (heroInputBottom) { - heroInputBottom.addEventListener('change', async (e) => { - const files = e.target.files; - if (!files.length) return; - showLoader("Uploading hero photos..."); - const formData = new FormData(); - for (const file of files) formData.append('files', file); - - try { - const res = await fetch('/api/hero/upload', { method: 'POST', body: formData }); - const data = await res.json(); - hideLoader(); - if (res.ok) { - showToast(`✅ ${data.uploaded.length} hero image(s) uploaded!`, "success"); - if (typeof refreshHero === "function") refreshHero(); - } else showToast('Error: ' + data.error, "error"); - } catch(err) { - hideLoader(); - console.error(err); - showToast('Server error!', "error"); - } finally { e.target.value = ''; } - }); -} \ No newline at end of file +// --- Setup all upload inputs --- +setupUpload('upload-gallery', '/api/gallery/upload', "Uploading photos...", "gallery image(s) uploaded!", refreshGallery); +setupUpload('upload-hero', '/api/hero/upload', "Uploading hero photos...", "hero image(s) uploaded!", refreshHero); +setupUpload('upload-gallery-bottom', '/api/gallery/upload', "Uploading photos...", "gallery image(s) uploaded!", refreshGallery); +setupUpload('upload-hero-bottom', '/api/hero/upload', "Uploading hero photos...", "hero image(s) uploaded!", refreshHero); \ No newline at end of file