From b2b85fb84ede8f2ea2d7868941bba54facca52f6 Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Fri, 12 Mar 2021 20:12:46 +0100 Subject: [PATCH] fix: favorites fetching --- public/js/bundle.js | 46 +++++++++++--------------------------------- src/use/favorites.js | 42 ++++++++-------------------------------- src/utils/api.js | 4 +++- 3 files changed, 22 insertions(+), 70 deletions(-) diff --git a/public/js/bundle.js b/public/js/bundle.js index 1a75799..10cb28a 100644 --- a/public/js/bundle.js +++ b/public/js/bundle.js @@ -12240,7 +12240,9 @@ function fetchData(key, data = {}) { url.searchParams.append(key, data[key]); }); - return fetch(url.href).then(response => response.json()) + return fetch(url.href) + .then(response => response.json()) + .catch(() => {}) } function sendToServer(key, data) { @@ -36285,14 +36287,17 @@ async function refreshFavorites({ isInitial = false }) { isRefreshingFavorites.value = true; } - const favorites = await fetchData('getFavorites'); + const favorites = await fetchData('getUserFavorites'); setAllFavorites(favorites); if (store.getters.isLoggedWithSpotify) { // TODO - const res = await fetchData('getUserSpotifyPlaylists', { spotifyUser: store.getters.getSpotifyUser.id }); - // socket.emit('update_userSpotifyPlaylists', store.getters.getSpotifyUser.id) + const spotifyPlaylists = await fetchData('getUserSpotifyPlaylists', { + spotifyUser: store.getters.getSpotifyUser.id + }); + console.log({ spotifyPlaylists }); + favoriteSpotifyPlaylists.value = spotifyPlaylists; } } @@ -36311,43 +36316,14 @@ function useFavorites() { function setAllFavorites(data) { const { tracks, albums, artists, playlists } = data; + isRefreshingFavorites.value = false; + 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 -}); - -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; -}); - // var script$g = defineComponent({ diff --git a/src/use/favorites.js b/src/use/favorites.js index e06ef50..5cecd0b 100644 --- a/src/use/favorites.js +++ b/src/use/favorites.js @@ -21,14 +21,17 @@ async function refreshFavorites({ isInitial = false }) { isRefreshingFavorites.value = true } - const favorites = await fetchData('getFavorites') + const favorites = await fetchData('getUserFavorites') setAllFavorites(favorites) if (store.getters.isLoggedWithSpotify) { // TODO - const res = await fetchData('getUserSpotifyPlaylists', { spotifyUser: store.getters.getSpotifyUser.id }) - // socket.emit('update_userSpotifyPlaylists', store.getters.getSpotifyUser.id) + const spotifyPlaylists = await fetchData('getUserSpotifyPlaylists', { + spotifyUser: store.getters.getSpotifyUser.id + }) + console.log({ spotifyPlaylists }) + favoriteSpotifyPlaylists.value = spotifyPlaylists } } @@ -47,39 +50,10 @@ export function useFavorites() { function setAllFavorites(data) { const { tracks, albums, artists, playlists } = data + isRefreshingFavorites.value = false + 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 -}) - -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 -}) diff --git a/src/utils/api.js b/src/utils/api.js index 7b8c4b5..1738cf3 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -5,7 +5,9 @@ export function fetchData(key, data = {}) { url.searchParams.append(key, data[key]) }) - return fetch(url.href).then(response => response.json()) + return fetch(url.href) + .then(response => response.json()) + .catch(() => {}) } export function sendToServer(key, data) {