2022-10-24 15:27:17 +00:00
|
|
|
import { playlistStore } from '@/stores'
|
2024-04-23 11:24:29 +00:00
|
|
|
import { eventBus, pluralize } from '@/utils'
|
|
|
|
import { useErrorHandler, useMessageToaster } from '@/composables'
|
2022-10-24 15:27:17 +00:00
|
|
|
|
|
|
|
export const usePlaylistManagement = () => {
|
2024-04-23 11:24:29 +00:00
|
|
|
const { handleHttpError } = useErrorHandler('dialog')
|
2022-11-18 17:45:38 +00:00
|
|
|
const { toastSuccess } = useMessageToaster()
|
2022-10-24 15:27:17 +00:00
|
|
|
|
|
|
|
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)
|
2022-11-18 17:45:38 +00:00
|
|
|
toastSuccess(`Added ${pluralize(songs, 'song')} into "${playlist.name}."`)
|
2024-04-23 11:24:29 +00:00
|
|
|
} catch (error: unknown) {
|
|
|
|
handleHttpError(error)
|
2022-10-24 15:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-11-18 17:45:38 +00:00
|
|
|
toastSuccess(`Removed ${pluralize(songs, 'song')} from "${playlist.name}."`)
|
2024-04-23 11:24:29 +00:00
|
|
|
} catch (error: unknown) {
|
|
|
|
handleHttpError(error)
|
2022-10-24 15:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
addSongsToPlaylist,
|
|
|
|
removeSongsFromPlaylist
|
|
|
|
}
|
|
|
|
}
|