From 7894750d2b59e786ee5d8119e6d24de0d8081b11 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 2 Jun 2025 09:48:53 -0700 Subject: [PATCH] Fix attaching new photos in new album --- .../components/admin/GalleryUploader.svelte | 23 ++++++---- .../admin/albums/[id]/edit/+page.svelte | 10 +++++ src/routes/admin/albums/new/+page.svelte | 42 ++++++++++++++++++- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/lib/components/admin/GalleryUploader.svelte b/src/lib/components/admin/GalleryUploader.svelte index 136706f..10477b2 100644 --- a/src/lib/components/admin/GalleryUploader.svelte +++ b/src/lib/components/admin/GalleryUploader.svelte @@ -314,15 +314,22 @@ // For gallery mode, selectedMedia will be an array const mediaArray = Array.isArray(selectedMedia) ? selectedMedia : [selectedMedia] - // Add selected media to existing gallery (avoid duplicates) - // Check both id and mediaId to handle different object types - const currentIds = value?.map((m) => m.mediaId || m.id) || [] - const newMedia = mediaArray.filter((media) => !currentIds.includes(media.id)) + // Filter out duplicates before passing to parent + // Create a comprehensive set of existing IDs (both id and mediaId) + const existingIds = new Set() + value?.forEach((m) => { + if (m.id) existingIds.add(m.id) + if (m.mediaId) existingIds.add(m.mediaId) + }) + + // Filter out any media that already exists (check both id and potential mediaId) + const newMedia = mediaArray.filter((media) => { + return !existingIds.has(media.id) && !existingIds.has(media.mediaId) + }) if (newMedia.length > 0) { - const updatedGallery = [...(value || []), ...newMedia] - value = updatedGallery - // Only pass the newly selected media, not the entire gallery + // Don't modify the value array here - let the parent component handle it + // through the API calls and then update the bound value onUpload(newMedia) } } @@ -467,7 +474,7 @@ {#if hasImages}