deemix-webui/src/use/favorites.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

import { ref } from '@vue/composition-api'
import store from '@/store'
import { fetchData } from '@/utils/api'
const favoriteArtists = ref([])
const favoriteAlbums = ref([])
const favoriteSpotifyPlaylists = ref([])
const favoritePlaylists = ref([])
const favoriteTracks = ref([])
const isRefreshingFavorites = ref(false)
2021-03-12 21:02:06 +01:00
function refreshFavorites({ isInitial = false }) {
if (!isInitial) {
isRefreshingFavorites.value = true
}
2021-03-12 21:02:06 +01:00
fetchData('getUserFavorites').then(setAllFavorites).catch(console.error)
if (store.getters.isLoggedWithSpotify) {
2021-03-12 21:02:06 +01:00
fetchData('getUserSpotifyPlaylists', {
2021-03-12 20:12:46 +01:00
spotifyUser: store.getters.getSpotifyUser.id
})
2021-03-12 21:02:06 +01:00
.then(({ data: spotifyPlaylists }) => {
favoriteSpotifyPlaylists.value = spotifyPlaylists
})
.catch(console.error)
}
}
2021-03-12 21:02:06 +01:00
function setAllFavorites(data) {
const { tracks, albums, artists, playlists } = data
isRefreshingFavorites.value = false
favoriteArtists.value = artists
favoriteAlbums.value = albums
favoritePlaylists.value = playlists
favoriteTracks.value = tracks
}
export function useFavorites() {
return {
favoriteArtists,
favoriteAlbums,
favoriteSpotifyPlaylists,
favoritePlaylists,
favoriteTracks,
isRefreshingFavorites,
refreshFavorites
}
}