Updating featured and mood views; Controls to forceRefetch, fixing #526

This commit is contained in:
James Barnsley
2020-09-07 21:04:28 +12:00
parent 60326d9de6
commit 9b3c0bcefe
19 changed files with 752 additions and 553 deletions

View File

@ -107,74 +107,74 @@ export function updateColdStore(items) {
* relevant service to load the record - all from one neat package.
* */
export function loadItems(uris, forceReload = false) {
export function loadItems(uris, forceRefetch = false) {
return {
type: 'LOAD_ITEMS',
uris,
forceReload,
forceRefetch,
};
}
export function loadItem(uri, forceReload = false) {
return loadItems([uri], forceReload);
export function loadItem(uri, forceRefetch = false) {
return loadItems([uri], forceRefetch);
}
export function loadTrack(uri, forceReload = false) {
export function loadTrack(uri, forceRefetch = false) {
return {
type: 'LOAD_TRACK',
uri,
forceReload,
forceRefetch,
};
}
export function loadAlbum(uri, forceReload = false, callbackAction) {
export function loadAlbum(uri, forceRefetch = false, callbackAction) {
return {
type: 'LOAD_ALBUM',
uri,
forceReload,
forceRefetch,
callbackAction,
};
}
export function loadArtist(uri, forceReload = false, callbackAction) {
export function loadArtist(uri, forceRefetch = false, callbackAction) {
return {
type: 'LOAD_ARTIST',
uri,
forceReload,
forceRefetch,
callbackAction,
};
}
export function loadPlaylist(uri, forceReload = false, callbackAction) {
export function loadPlaylist(uri, forceRefetch = false, callbackAction) {
return {
type: 'LOAD_PLAYLIST',
uri,
forceReload,
forceRefetch,
callbackAction,
};
}
export function loadUser(uri, forceReload = false) {
export function loadUser(uri, forceRefetch = false) {
return {
type: 'LOAD_USER',
uri,
forceReload,
forceRefetch,
};
}
export function loadUserPlaylists(uri, forceReload = false) {
export function loadUserPlaylists(uri, forceRefetch = false) {
return {
type: 'LOAD_USER_PLAYLISTS',
uri,
forceReload,
forceRefetch,
};
}
export function loadLibrary(uri, forceReload = false) {
export function loadLibrary(uri, forceRefetch = false) {
return {
type: 'LOAD_LIBRARY',
uri,
forceReload,
forceRefetch,
};
}

View File

@ -318,7 +318,7 @@ const CoreMiddleware = (function () {
store.dispatch({
type: `LOAD_${uriType(uri).toUpperCase()}`,
uri,
forceReload: action.forceReload,
forceRefetch: action.forceRefetch,
});
});
break;
@ -327,7 +327,7 @@ const CoreMiddleware = (function () {
const fetchTrack = () => {
switch (uriSource(action.uri)) {
case 'spotify':
store.dispatch(spotifyActions.getTrack(action.uri));
store.dispatch(spotifyActions.getTrack(action.uri, action.forceRefetch));
if (spotify.me) {
store.dispatch(spotifyActions.following(action.uri));
@ -340,8 +340,8 @@ const CoreMiddleware = (function () {
}
};
if (action.forceReload) {
console.info(`Force-reloading "${action.uri}"`);
if (action.forceRefetch) {
console.info(`Force-refetching "${action.uri}"`);
fetchTrack();
break;
}
@ -367,7 +367,7 @@ const CoreMiddleware = (function () {
const fetchAlbum = () => {
switch (uriSource(action.uri)) {
case 'spotify':
store.dispatch(spotifyActions.getAlbum(action.uri));
store.dispatch(spotifyActions.getAlbum(action.uri, action.forceRefetch));
if (spotify.me) {
store.dispatch(spotifyActions.following(action.uri));
@ -380,8 +380,8 @@ const CoreMiddleware = (function () {
};
};
if (action.forceReload) {
console.info(`Force-reloading "${action.uri}"`);
if (action.forceRefetch) {
console.info(`Force-refetching "${action.uri}"`);
fetchAlbum();
break;
}
@ -414,7 +414,7 @@ const CoreMiddleware = (function () {
const fetchArtist = () => {
switch (uriSource(action.uri)) {
case 'spotify':
store.dispatch(spotifyActions.getArtist(action.uri, true));
store.dispatch(spotifyActions.getArtist(action.uri, true, action.forceRefetch));
if (spotify.me) {
store.dispatch(spotifyActions.following(action.uri));
@ -427,8 +427,8 @@ const CoreMiddleware = (function () {
}
};
if (action.forceReload) {
console.info(`Force-reloading "${action.uri}"`);
if (action.forceRefetch) {
console.info(`Force-refetching "${action.uri}"`);
fetchArtist();
break;
}
@ -459,10 +459,11 @@ const CoreMiddleware = (function () {
break;
case 'LOAD_PLAYLIST':
console.log(action);
const fetchPlaylist = () => {
switch (uriSource(action.uri)) {
case 'spotify':
store.dispatch(spotifyActions.getPlaylist(action.uri));
store.dispatch(spotifyActions.getPlaylist(action.uri, action.forceRefetch));
if (spotify.me) {
store.dispatch(spotifyActions.following(action.uri));
@ -475,8 +476,8 @@ const CoreMiddleware = (function () {
}
};
if (action.forceReload) {
console.info(`Force-reloading "${action.uri}"`);
if (action.forceRefetch) {
console.info(`Force-refetching "${action.uri}"`);
fetchPlaylist();
break;
}
@ -505,7 +506,7 @@ const CoreMiddleware = (function () {
case 'LOAD_USER':
if (
!action.forceReload
!action.forceRefetch
&& store.getState().core.users[action.uri]
&& store.getState().core.users[action.uri].playlists_uris) {
console.info(`Loading "${action.uri}" from index`);
@ -531,7 +532,7 @@ const CoreMiddleware = (function () {
case 'LOAD_USER_PLAYLISTS':
if (
!action.forceReload
!action.forceRefetch
&& store.getState().core.users[action.uri]
&& store.getState().core.users[action.uri].playlists_uris) {
console.info(`Loading "${action.uri}" playlists from index`);
@ -556,7 +557,7 @@ const CoreMiddleware = (function () {
switch (uriSource(action.uri)) {
case 'spotify':
store.dispatch(
spotifyActions[`getLibrary${titleCase(uriType(action.uri))}`](),
spotifyActions[`getLibrary${titleCase(uriType(action.uri))}`](action.forceRefetch),
);
break;
case 'google':
@ -572,8 +573,8 @@ const CoreMiddleware = (function () {
}
};
if (action.forceReload) {
console.info(`Force-reloading "${action.uri}"`);
if (action.forceRefetch) {
console.info(`Force-refetching "${action.uri}"`);
fetchLibrary();
break;
}
@ -721,6 +722,7 @@ const CoreMiddleware = (function () {
case 'LIBRARY_LOADED':
store.dispatch(coreActions.updateColdStore([action.library]));
next(action);
break;
case 'ARTISTS_LOADED':

View File

@ -86,7 +86,6 @@ export default function reducer(core = {}, action) {
};
case 'LIBRARY_LOADED':
console.log(action);
return {
...core,
libraries: {

View File

@ -20,6 +20,7 @@ import {
formatAlbums,
formatPlaylists,
formatImages,
formatTrack,
} from '../../util/format';
import URILink from '../../components/URILink';
import { i18n } from '../../locale';
@ -334,13 +335,7 @@ export function getTrack(uri) {
request(dispatch, getState, `tracks/${getFromUri('trackid', uri)}`)
.then(
(response) => {
dispatch(coreActions.trackLoaded(response));
},
(error) => {
dispatch(coreActions.handleException(
'Could not load track',
error,
));
dispatch(coreActions.itemLoaded(formatTrack(response)));
},
);
};
@ -389,26 +384,21 @@ export function getFeaturedPlaylists() {
request(dispatch, getState, `browse/featured-playlists?limit=50&country=${getState().spotify.country}&locale=${getState().spotify.locale}timestamp=${timestamp}`)
.then(
(response) => {
const playlists = [];
for (let i = 0; i < response.playlists.items.length; i++) {
playlists.push({
...response.playlists.items[i],
is_completely_loaded: false,
can_edit: (getState().spotify.me && response.playlists.items[i].owner.id == getState().spotify.me.id),
tracks_total: response.playlists.items[i].tracks.total,
});
}
const playlists = response.playlists.items.map(
(raw_playlist) => {
const playlist = formatPlaylist(raw_playlist);
delete playlist.tracks; // Don't overwrite (we may already have loaded these)
return playlist;
},
);
dispatch({
type: 'PLAYLISTS_LOADED',
playlists,
});
dispatch(coreActions.itemsLoaded(playlists));
dispatch({
type: 'SPOTIFY_FEATURED_PLAYLISTS_LOADED',
data: {
message: response.message,
playlists: upgradeSpotifyPlaylistUris(arrayOf('uri', response.playlists.items)),
uris: upgradeSpotifyPlaylistUris(arrayOf('uri', playlists)),
},
});
},
@ -797,11 +787,10 @@ export function following(uri, method = 'GET') {
is_following = is_following;
}
dispatch({
type: `SPOTIFY_LIBRARY_${asset_name.toUpperCase()}_CHECK`,
key: uri,
dispatch(coreActions.itemLoaded({
uri,
in_library: is_following,
});
}));
if (method === 'DELETE') {
dispatch(uiActions.createNotification({
@ -1046,9 +1035,10 @@ export function getGenres() {
* @param uri string
* @param full boolean (whether we want a full artist object)
* */
export function getArtist(uri, full = false) {
export function getArtist(uri, full = false, forceRefetch = false) {
return (dispatch, getState) => {
request(dispatch, getState, `artists/${getFromUri('artistid', uri)}`, 'GET', false, true)
const endpoint = `artists/${getFromUri('artistid', uri)}${forceRefetch ? `?refetch=${Date.now()}` : ''}`;
request(dispatch, getState, endpoint, 'GET', false, true)
.then(
(response) => {
dispatch(coreActions.itemLoaded(formatArtist(response)));
@ -1210,10 +1200,10 @@ export function getUserPlaylists(uri) {
*
* @oaram uri string
* */
export function getAlbum(uri) {
export function getAlbum(uri, forceRefetch) {
return (dispatch, getState) => {
// get the album
request(dispatch, getState, `albums/${getFromUri('albumid', uri)}`)
const endpoint = `albums/${getFromUri('albumid', uri)}${forceRefetch ? `?refetch=${Date.now()}` : ''}`;
request(dispatch, getState, endpoint)
.then(
(response) => {
dispatch(coreActions.itemLoaded({
@ -1363,9 +1353,10 @@ export function savePlaylist(uri, name, description, is_public, is_collaborative
};
}
export function getPlaylist(uri, callbackAction = null) {
export function getPlaylist(uri, forceRefetch = false, callbackAction = null) {
return (dispatch, getState) => {
request(dispatch, getState, `playlists/${getFromUri('playlistid', uri)}?market=${getState().spotify.country}`)
const endpoint = `playlists/${getFromUri('playlistid', uri)}?market=${getState().spotify.country}${forceRefetch ? `&refetch=${Date.now()}` : ''}`;
request(dispatch, getState, endpoint)
.then(
(response) => {
let tracks = formatTracks(response.tracks.items);
@ -1729,65 +1720,74 @@ export function flushLibrary() {
};
}
export function getLibraryPlaylists() {
export function getLibraryPlaylists(forceRefetch) {
return (dispatch, getState) => {
let libraryPlaylists = [];
let libraryItems = [];
const fetchLibraryPlaylists = (endpoint) => request(dispatch, getState, endpoint)
.then((response) => {
libraryPlaylists = [...libraryPlaylists, ...formatPlaylists(response.items)];
const items = response.items.map(
(item) => ({ ...item, in_library: true }),
);
libraryItems = [...libraryItems, ...formatPlaylists(items)];
if (response.next) {
fetchLibraryPlaylists(response.next);
} else {
dispatch(coreActions.itemsLoaded(libraryPlaylists));
dispatch(coreActions.itemsLoaded(libraryItems));
dispatch(coreActions.libraryLoaded({
uri: 'spotify:library:playlists',
items_uris: arrayOf('uri', libraryPlaylists),
items_uris: arrayOf('uri', libraryItems),
}));
}
});
fetchLibraryPlaylists('me/playlists?limit=50');
fetchLibraryPlaylists(`me/playlists?limit=50${forceRefetch ? `&refetch=${Date.now()}` : ''}`);
};
}
export function getLibraryAlbums() {
export function getLibraryAlbums(forceRefetch) {
return (dispatch, getState) => {
let libraryAlbums = [];
let libraryItems = [];
const fetchLibraryAlbums = (endpoint) => request(dispatch, getState, endpoint)
.then((response) => {
libraryAlbums = [...libraryAlbums, ...formatAlbums(response.items)];
const items = response.items.map(
(item) => ({ ...item, in_library: true }),
);
libraryItems = [...libraryItems, ...formatAlbums(items)];
if (response.next) {
fetchLibraryAlbums(response.next);
} else {
dispatch(coreActions.itemsLoaded(libraryAlbums));
dispatch(coreActions.itemsLoaded(libraryItems));
dispatch(coreActions.libraryLoaded({
uri: 'spotify:library:albums',
items_uris: arrayOf('uri', libraryAlbums),
items_uris: arrayOf('uri', libraryItems),
}));
}
});
fetchLibraryAlbums('me/albums?limit=50');
fetchLibraryAlbums(`me/albums?limit=50${forceRefetch ? `&refetch=${Date.now()}` : ''}`);
};
}
export function getLibraryArtists() {
export function getLibraryArtists(forceRefetch) {
return (dispatch, getState) => {
let libraryArtists = [];
let libraryItems = [];
const fetchLibraryArtists = (endpoint) => request(dispatch, getState, endpoint)
.then((response) => {
libraryArtists = [...libraryArtists, ...formatArtists(response.artists.items)];
const items = response.artists.items.map(
(item) => ({ ...item, in_library: true }),
);
libraryItems = [...libraryItems, ...formatArtists(items)];
if (response.next) {
fetchLibraryArtists(response.next);
} else {
dispatch(coreActions.itemsLoaded(libraryArtists));
dispatch(coreActions.itemsLoaded(libraryItems));
dispatch(coreActions.libraryLoaded({
uri: 'spotify:library:artists',
items_uris: arrayOf('uri', libraryArtists),
items_uris: arrayOf('uri', libraryItems),
}));
}
});
fetchLibraryArtists('me/following?type=artist&limit=50');
fetchLibraryArtists(`me/following?type=artist&limit=50${forceRefetch ? `&refetch=${Date.now()}` : ''}`);
};
}