Fix attaching new photos in new album
This commit is contained in:
parent
78443e2bdd
commit
7894750d2b
3 changed files with 66 additions and 9 deletions
|
|
@ -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 @@
|
|||
<!-- Image Gallery -->
|
||||
{#if hasImages}
|
||||
<div class="image-gallery">
|
||||
{#each value as media, index (media.id)}
|
||||
{#each value as media, index (`${media.mediaId || media.id || index}`)}
|
||||
<div
|
||||
class="gallery-item"
|
||||
class:dragging={draggedIndex === index}
|
||||
|
|
|
|||
|
|
@ -213,7 +213,11 @@
|
|||
|
||||
// Add photos to album via API
|
||||
const addedPhotos = []
|
||||
console.log('Adding photos to album:', newMedia.map(m => ({ id: m.id, filename: m.filename })))
|
||||
|
||||
for (const media of newMedia) {
|
||||
console.log(`Adding photo ${media.id} (${media.filename}) to album ${album.id}`)
|
||||
|
||||
const response = await fetch(`/api/albums/${album.id}/photos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
|
|
@ -226,17 +230,23 @@
|
|||
})
|
||||
})
|
||||
|
||||
console.log(`API response for media ${media.id}:`, response.status, response.statusText)
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.text()
|
||||
console.error(`Failed to add photo ${media.filename}:`, response.status, errorData)
|
||||
throw new Error(`Failed to add photo ${media.filename}: ${response.status} ${errorData}`)
|
||||
}
|
||||
|
||||
const photo = await response.json()
|
||||
console.log('Photo added successfully:', photo)
|
||||
addedPhotos.push(photo)
|
||||
}
|
||||
|
||||
console.log('All photos added, updating local state. Added photos:', addedPhotos)
|
||||
// Update local state with all added photos
|
||||
albumPhotos = [...albumPhotos, ...addedPhotos]
|
||||
console.log('Updated albumPhotos array:', albumPhotos.length, 'photos')
|
||||
|
||||
// Update album photo count
|
||||
if (album._count) {
|
||||
|
|
|
|||
|
|
@ -99,7 +99,47 @@
|
|||
|
||||
const album = await response.json()
|
||||
|
||||
// Redirect to album edit page or albums list
|
||||
// Add selected photos to the newly created album
|
||||
if (albumPhotos.length > 0) {
|
||||
console.log(`Adding ${albumPhotos.length} photos to newly created album ${album.id}`)
|
||||
|
||||
try {
|
||||
const addedPhotos = []
|
||||
for (let i = 0; i < albumPhotos.length; i++) {
|
||||
const media = albumPhotos[i]
|
||||
console.log(`Adding photo ${media.id} to album ${album.id}`)
|
||||
|
||||
const photoResponse = await fetch(`/api/albums/${album.id}/photos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Basic ${auth}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
mediaId: media.id,
|
||||
displayOrder: i
|
||||
})
|
||||
})
|
||||
|
||||
if (!photoResponse.ok) {
|
||||
const errorData = await photoResponse.text()
|
||||
console.error(`Failed to add photo ${media.filename}:`, photoResponse.status, errorData)
|
||||
// Continue with other photos even if one fails
|
||||
} else {
|
||||
const photo = await photoResponse.json()
|
||||
addedPhotos.push(photo)
|
||||
console.log(`Successfully added photo ${photo.id} to album`)
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Successfully added ${addedPhotos.length} out of ${albumPhotos.length} photos to album`)
|
||||
} catch (photoError) {
|
||||
console.error('Error adding photos to album:', photoError)
|
||||
// Don't fail the whole creation - just log the error
|
||||
}
|
||||
}
|
||||
|
||||
// Redirect to album edit page
|
||||
goto(`/admin/albums/${album.id}/edit`)
|
||||
} catch (err) {
|
||||
error = err instanceof Error ? err.message : 'Failed to create album'
|
||||
|
|
|
|||
Loading…
Reference in a new issue