feat: implement favorites toggle functionality in PlaylistEditHeader and PlaylistEditPage

This commit is contained in:
JSC
2025-08-17 01:08:39 +02:00
parent af1d543669
commit 0024f1d647
2 changed files with 55 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ import {
playlistsService,
} from '@/lib/api/services/playlists'
import { type Sound, soundsService } from '@/lib/api/services/sounds'
import { favoritesService } from '@/lib/api/services/favorites'
import {
DndContext,
type DragEndEvent,
@@ -235,6 +236,37 @@ export function PlaylistEditPage() {
}
}
const handleFavoriteToggle = async (playlistId: number, shouldFavorite: boolean) => {
try {
if (shouldFavorite) {
await favoritesService.addPlaylistFavorite(playlistId)
toast.success('Added to favorites')
} else {
await favoritesService.removePlaylistFavorite(playlistId)
toast.success('Removed from favorites')
}
// Update the playlist in the local state
setPlaylist(prevPlaylist =>
prevPlaylist
? {
...prevPlaylist,
is_favorited: shouldFavorite,
favorite_count: shouldFavorite
? prevPlaylist.favorite_count + 1
: Math.max(0, prevPlaylist.favorite_count - 1),
}
: null,
)
} catch (error) {
toast.error(
`Failed to ${shouldFavorite ? 'add to' : 'remove from'} favorites: ${
error instanceof Error ? error.message : 'Unknown error'
}`,
)
}
}
const handleMoveSoundUp = async (index: number) => {
if (index === 0 || sounds.length < 2) return
@@ -574,6 +606,7 @@ export function PlaylistEditPage() {
playlist={playlist}
isEditMode={isEditMode}
onSetCurrent={handleSetCurrent}
onFavoriteToggle={handleFavoriteToggle}
/>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">