2020-11-28 22:11:11 +01:00
|
|
|
import { ref } from '@vue/composition-api'
|
|
|
|
|
|
|
|
import store from '@/store'
|
|
|
|
import { socket } from '@/utils/socket'
|
2021-03-01 23:09:14 +01:00
|
|
|
import { fetchData } from '@/utils/api'
|
2020-11-28 22:11:11 +01:00
|
|
|
|
|
|
|
const favoriteArtists = ref([])
|
|
|
|
const favoriteAlbums = ref([])
|
|
|
|
const favoriteSpotifyPlaylists = ref([])
|
|
|
|
const favoritePlaylists = ref([])
|
|
|
|
const favoriteTracks = ref([])
|
|
|
|
|
|
|
|
const isRefreshingFavorites = ref(false)
|
|
|
|
|
|
|
|
if (store.getters.isLoggedIn) {
|
|
|
|
refreshFavorites({ isInitial: true })
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:09:14 +01:00
|
|
|
async function refreshFavorites({ isInitial = false }) {
|
2020-11-28 22:11:11 +01:00
|
|
|
if (!isInitial) {
|
|
|
|
isRefreshingFavorites.value = true
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:09:14 +01:00
|
|
|
const favorites = await fetchData('getFavorites')
|
|
|
|
|
|
|
|
setAllFavorites(favorites)
|
2020-11-28 22:11:11 +01:00
|
|
|
|
|
|
|
if (store.getters.isLoggedWithSpotify) {
|
2021-03-01 23:09:14 +01:00
|
|
|
// TODO
|
|
|
|
const res = await fetchData('getUserSpotifyPlaylists', { spotifyUser: store.getters.getSpotifyUser.id })
|
|
|
|
// socket.emit('update_userSpotifyPlaylists', store.getters.getSpotifyUser.id)
|
2020-11-28 22:11:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useFavorites() {
|
|
|
|
return {
|
|
|
|
favoriteArtists,
|
|
|
|
favoriteAlbums,
|
|
|
|
favoriteSpotifyPlaylists,
|
|
|
|
favoritePlaylists,
|
|
|
|
favoriteTracks,
|
|
|
|
isRefreshingFavorites,
|
|
|
|
refreshFavorites
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setAllFavorites(data) {
|
|
|
|
const { tracks, albums, artists, playlists } = data
|
|
|
|
|
|
|
|
favoriteArtists.value = artists
|
|
|
|
favoriteAlbums.value = albums
|
|
|
|
favoritePlaylists.value = playlists
|
|
|
|
favoriteTracks.value = tracks
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('updated_userFavorites', data => {
|
|
|
|
setAllFavorites(data)
|
|
|
|
// Commented out because the corresponding emit function is never called at the moment
|
|
|
|
// therefore isRefreshingFavorites is never set to true
|
|
|
|
// isRefreshingFavorites.value = false
|
|
|
|
})
|
2021-03-01 23:09:14 +01:00
|
|
|
|
2020-11-28 22:11:11 +01:00
|
|
|
socket.on('init_favorites', data => {
|
|
|
|
setAllFavorites(data)
|
|
|
|
isRefreshingFavorites.value = false
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('updated_userSpotifyPlaylists', data => {
|
|
|
|
favoriteSpotifyPlaylists.value = data
|
|
|
|
})
|
|
|
|
socket.on('updated_userSpotifyPlaylists', data => {
|
|
|
|
favoriteSpotifyPlaylists.value = data
|
|
|
|
})
|
|
|
|
socket.on('updated_userPlaylists', data => {
|
|
|
|
favoritePlaylists.value = data
|
|
|
|
})
|
|
|
|
socket.on('updated_userAlbums', data => {
|
|
|
|
favoriteAlbums.value = data
|
|
|
|
})
|
|
|
|
socket.on('updated_userArtist', data => {
|
|
|
|
favoriteArtists.value = data
|
|
|
|
})
|
|
|
|
socket.on('updated_userTracks', data => {
|
|
|
|
favoriteTracks.value = data
|
|
|
|
})
|