mirror of
https://github.com/koel/koel
synced 2024-12-29 22:13:06 +00:00
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { playlistStore } from '@/stores'
|
|
import { eventBus, logger, pluralize } from '@/utils'
|
|
import { useDialogBox, useMessageToaster } from '@/composables'
|
|
|
|
export const usePlaylistManagement = () => {
|
|
const { toastSuccess } = useMessageToaster()
|
|
const { showErrorDialog } = useDialogBox()
|
|
|
|
const addSongsToPlaylist = async (playlist: Playlist, songs: Song[]) => {
|
|
if (playlist.is_smart || songs.length === 0) return
|
|
|
|
try {
|
|
await playlistStore.addSongs(playlist, songs)
|
|
eventBus.emit('PLAYLIST_UPDATED', playlist)
|
|
toastSuccess(`Added ${pluralize(songs, 'song')} into "${playlist.name}."`)
|
|
} catch (error) {
|
|
logger.error(error)
|
|
showErrorDialog('Something went wrong. Please try again.', 'Error')
|
|
}
|
|
}
|
|
|
|
const removeSongsFromPlaylist = async (playlist: Playlist, songs: Song[]) => {
|
|
if (playlist.is_smart) return
|
|
|
|
try {
|
|
await playlistStore.removeSongs(playlist, songs)
|
|
eventBus.emit('PLAYLIST_SONGS_REMOVED', playlist, songs)
|
|
toastSuccess(`Removed ${pluralize(songs, 'song')} from "${playlist.name}."`)
|
|
} catch (error) {
|
|
logger.error(error)
|
|
showErrorDialog('Something went wrong. Please try again.', 'Error')
|
|
}
|
|
}
|
|
|
|
return {
|
|
addSongsToPlaylist,
|
|
removeSongsFromPlaylist
|
|
}
|
|
}
|