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
|
// For gallery mode, selectedMedia will be an array
|
||||||
const mediaArray = Array.isArray(selectedMedia) ? selectedMedia : [selectedMedia]
|
const mediaArray = Array.isArray(selectedMedia) ? selectedMedia : [selectedMedia]
|
||||||
|
|
||||||
// Add selected media to existing gallery (avoid duplicates)
|
// Filter out duplicates before passing to parent
|
||||||
// Check both id and mediaId to handle different object types
|
// Create a comprehensive set of existing IDs (both id and mediaId)
|
||||||
const currentIds = value?.map((m) => m.mediaId || m.id) || []
|
const existingIds = new Set()
|
||||||
const newMedia = mediaArray.filter((media) => !currentIds.includes(media.id))
|
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) {
|
if (newMedia.length > 0) {
|
||||||
const updatedGallery = [...(value || []), ...newMedia]
|
// Don't modify the value array here - let the parent component handle it
|
||||||
value = updatedGallery
|
// through the API calls and then update the bound value
|
||||||
// Only pass the newly selected media, not the entire gallery
|
|
||||||
onUpload(newMedia)
|
onUpload(newMedia)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -467,7 +474,7 @@
|
||||||
<!-- Image Gallery -->
|
<!-- Image Gallery -->
|
||||||
{#if hasImages}
|
{#if hasImages}
|
||||||
<div class="image-gallery">
|
<div class="image-gallery">
|
||||||
{#each value as media, index (media.id)}
|
{#each value as media, index (`${media.mediaId || media.id || index}`)}
|
||||||
<div
|
<div
|
||||||
class="gallery-item"
|
class="gallery-item"
|
||||||
class:dragging={draggedIndex === index}
|
class:dragging={draggedIndex === index}
|
||||||
|
|
|
||||||
|
|
@ -213,7 +213,11 @@
|
||||||
|
|
||||||
// Add photos to album via API
|
// Add photos to album via API
|
||||||
const addedPhotos = []
|
const addedPhotos = []
|
||||||
|
console.log('Adding photos to album:', newMedia.map(m => ({ id: m.id, filename: m.filename })))
|
||||||
|
|
||||||
for (const media of newMedia) {
|
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`, {
|
const response = await fetch(`/api/albums/${album.id}/photos`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -226,17 +230,23 @@
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
console.log(`API response for media ${media.id}:`, response.status, response.statusText)
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.text()
|
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}`)
|
throw new Error(`Failed to add photo ${media.filename}: ${response.status} ${errorData}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const photo = await response.json()
|
const photo = await response.json()
|
||||||
|
console.log('Photo added successfully:', photo)
|
||||||
addedPhotos.push(photo)
|
addedPhotos.push(photo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('All photos added, updating local state. Added photos:', addedPhotos)
|
||||||
// Update local state with all added photos
|
// Update local state with all added photos
|
||||||
albumPhotos = [...albumPhotos, ...addedPhotos]
|
albumPhotos = [...albumPhotos, ...addedPhotos]
|
||||||
|
console.log('Updated albumPhotos array:', albumPhotos.length, 'photos')
|
||||||
|
|
||||||
// Update album photo count
|
// Update album photo count
|
||||||
if (album._count) {
|
if (album._count) {
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,47 @@
|
||||||
|
|
||||||
const album = await response.json()
|
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`)
|
goto(`/admin/albums/${album.id}/edit`)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
error = err instanceof Error ? err.message : 'Failed to create album'
|
error = err instanceof Error ? err.message : 'Failed to create album'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue