koel/resources/assets/js/composables/useSongMenuMethods.ts

49 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-07-04 10:39:02 +00:00
import { Ref } from 'vue'
2022-04-26 13:36:26 +00:00
import { favoriteStore, playlistStore, queueStore } from '@/stores'
import { pluralize, requireInjection } from '@/utils'
import { DialogBoxKey, MessageToasterKey } from '@/symbols'
2022-04-15 14:24:30 +00:00
export const useSongMenuMethods = (songs: Ref<Song[]>, close: Closure) => {
const toaster = requireInjection(MessageToasterKey)
const dialog = requireInjection(DialogBoxKey)
2022-04-15 14:24:30 +00:00
const queueSongsAfterCurrent = () => {
close()
2022-06-10 10:47:46 +00:00
queueStore.queueAfterCurrent(songs.value)
2022-04-15 14:24:30 +00:00
}
const queueSongsToBottom = () => {
close()
2022-06-10 10:47:46 +00:00
queueStore.queue(songs.value)
2022-04-15 14:24:30 +00:00
}
const queueSongsToTop = () => {
close()
2022-06-10 10:47:46 +00:00
queueStore.queueToTop(songs.value)
2022-04-15 14:24:30 +00:00
}
const addSongsToFavorite = async () => {
close()
2022-06-10 10:47:46 +00:00
await favoriteStore.like(songs.value)
2022-04-15 14:24:30 +00:00
}
const addSongsToExistingPlaylist = async (playlist: Playlist) => {
close()
2022-07-19 08:19:57 +00:00
try {
await playlistStore.addSongs(playlist, songs.value)
toaster.value.success(`Added ${pluralize(songs.value, 'song')} into "${playlist.name}."`)
2022-07-19 08:19:57 +00:00
} catch (error) {
dialog.value.error('Something went wrong. Please try again.', 'Error')
2022-07-19 08:19:57 +00:00
}
2022-04-15 14:24:30 +00:00
}
return {
queueSongsAfterCurrent,
queueSongsToBottom,
queueSongsToTop,
addSongsToFavorite,
addSongsToExistingPlaylist
}
}