Splitting out helpers into separate files
This commit is contained in:
@ -1,10 +1,25 @@
|
||||
import { exception } from 'react-ga';
|
||||
|
||||
import {
|
||||
arrayOf,
|
||||
shuffle,
|
||||
} from '../../util/arrays';
|
||||
import {
|
||||
generateGuid,
|
||||
getFromUri,
|
||||
uriType,
|
||||
upgradeSpotifyPlaylistUris,
|
||||
} from '../../util/helpers';
|
||||
import {
|
||||
formatTracks,
|
||||
formatPlaylist,
|
||||
formatUser,
|
||||
formatAlbum,
|
||||
} from '../../util/format';
|
||||
|
||||
const coreActions = require('../../services/core/actions');
|
||||
const uiActions = require('../../services/ui/actions');
|
||||
const mopidyActions = require('../../services/mopidy/actions');
|
||||
const lastfmActions = require('../../services/lastfm/actions');
|
||||
const helpers = require('../../helpers');
|
||||
|
||||
/**
|
||||
* Send an ajax request to the Spotify API
|
||||
@ -21,7 +36,7 @@ const request = (dispatch, getState, endpoint, method = 'GET', data = false, cac
|
||||
// Add reference to loader queue
|
||||
// We do this straight away so that even if we're refreshing the token, it still registers as
|
||||
// loading said endpoint
|
||||
const loader_key = helpers.generateGuid();
|
||||
const loader_key = generateGuid();
|
||||
dispatch(uiActions.startLoading(loader_key, `spotify_${endpoint}`));
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@ -137,7 +152,7 @@ function getToken(dispatch, getState) {
|
||||
function refreshToken(dispatch, getState) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// add reference to loader queue
|
||||
const loader_key = helpers.generateGuid();
|
||||
const loader_key = generateGuid();
|
||||
dispatch(uiActions.startLoading(loader_key, 'spotify_refresh_token'));
|
||||
|
||||
// Fully-authorized, so we can use the local Spotify credentials
|
||||
@ -304,7 +319,7 @@ export function getMe() {
|
||||
* */
|
||||
export function getTrack(uri) {
|
||||
return (dispatch, getState) => {
|
||||
request(dispatch, getState, `tracks/${helpers.getFromUri('trackid', uri)}`)
|
||||
request(dispatch, getState, `tracks/${getFromUri('trackid', uri)}`)
|
||||
.then(
|
||||
(response) => {
|
||||
dispatch(coreActions.trackLoaded(response));
|
||||
@ -382,7 +397,7 @@ export function getFeaturedPlaylists() {
|
||||
type: 'SPOTIFY_FEATURED_PLAYLISTS_LOADED',
|
||||
data: {
|
||||
message: response.message,
|
||||
playlists: helpers.upgradeSpotifyPlaylistUris(helpers.arrayOf('uri', response.playlists.items)),
|
||||
playlists: upgradeSpotifyPlaylistUris(arrayOf('uri', response.playlists.items)),
|
||||
},
|
||||
});
|
||||
},
|
||||
@ -563,7 +578,7 @@ export function getSearchResults(type, query, limit = 50, offset = 0) {
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'tracks',
|
||||
query,
|
||||
results: helpers.formatTracks(response.tracks.items),
|
||||
results: formatTracks(response.tracks.items),
|
||||
more: response.tracks.next,
|
||||
});
|
||||
}
|
||||
@ -577,7 +592,7 @@ export function getSearchResults(type, query, limit = 50, offset = 0) {
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'artists',
|
||||
query,
|
||||
results: helpers.arrayOf('uri', response.artists.items),
|
||||
results: arrayOf('uri', response.artists.items),
|
||||
more: response.artists.next,
|
||||
});
|
||||
}
|
||||
@ -591,7 +606,7 @@ export function getSearchResults(type, query, limit = 50, offset = 0) {
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'albums',
|
||||
query,
|
||||
results: helpers.arrayOf('uri', response.albums.items),
|
||||
results: arrayOf('uri', response.albums.items),
|
||||
more: response.albums.next,
|
||||
});
|
||||
}
|
||||
@ -601,7 +616,7 @@ export function getSearchResults(type, query, limit = 50, offset = 0) {
|
||||
for (const playlist of response.playlists.items) {
|
||||
playlists.push({
|
||||
|
||||
...helpers.formatPlaylist(playlist),
|
||||
...formatPlaylist(playlist),
|
||||
can_edit: (getState().spotify.me && playlist.owner.id == getState().spotify.me.id),
|
||||
tracks_total: playlist.tracks.total,
|
||||
});
|
||||
@ -615,7 +630,7 @@ export function getSearchResults(type, query, limit = 50, offset = 0) {
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'playlists',
|
||||
query,
|
||||
results: helpers.arrayOf('uri', playlists),
|
||||
results: arrayOf('uri', playlists),
|
||||
more: response.playlists.next,
|
||||
});
|
||||
}
|
||||
@ -712,7 +727,7 @@ export function clearAutocompleteResults(field_id = null) {
|
||||
|
||||
export function following(uri, method = 'GET') {
|
||||
return (dispatch, getState) => {
|
||||
const asset_name = helpers.uriType(uri);
|
||||
const asset_name = uriType(uri);
|
||||
let endpoint;
|
||||
let data;
|
||||
let is_following = null;
|
||||
@ -726,39 +741,39 @@ export function following(uri, method = 'GET') {
|
||||
switch (asset_name) {
|
||||
case 'track':
|
||||
if (method == 'GET') {
|
||||
endpoint = `me/tracks/contains?ids=${helpers.getFromUri('trackid', uri)}`;
|
||||
endpoint = `me/tracks/contains?ids=${getFromUri('trackid', uri)}`;
|
||||
} else {
|
||||
endpoint = `me/tracks?ids=${helpers.getFromUri('trackid', uri)}`;
|
||||
endpoint = `me/tracks?ids=${getFromUri('trackid', uri)}`;
|
||||
}
|
||||
break;
|
||||
case 'album':
|
||||
if (method == 'GET') {
|
||||
endpoint = `me/albums/contains?ids=${helpers.getFromUri('albumid', uri)}`;
|
||||
endpoint = `me/albums/contains?ids=${getFromUri('albumid', uri)}`;
|
||||
} else {
|
||||
endpoint = `me/albums?ids=${helpers.getFromUri('albumid', uri)}`;
|
||||
endpoint = `me/albums?ids=${getFromUri('albumid', uri)}`;
|
||||
}
|
||||
break;
|
||||
case 'artist':
|
||||
if (method == 'GET') {
|
||||
endpoint = `me/following/contains?type=artist&ids=${helpers.getFromUri('artistid', uri)}`;
|
||||
endpoint = `me/following/contains?type=artist&ids=${getFromUri('artistid', uri)}`;
|
||||
} else {
|
||||
endpoint = `me/following?type=artist&ids=${helpers.getFromUri('artistid', uri)}`;
|
||||
endpoint = `me/following?type=artist&ids=${getFromUri('artistid', uri)}`;
|
||||
data = {};
|
||||
}
|
||||
break;
|
||||
case 'user':
|
||||
if (method == 'GET') {
|
||||
endpoint = `me/following/contains?type=user&ids=${helpers.getFromUri('userid', uri)}`;
|
||||
endpoint = `me/following/contains?type=user&ids=${getFromUri('userid', uri)}`;
|
||||
} else {
|
||||
endpoint = `me/following?type=user&ids=${helpers.getFromUri('userid', uri)}`;
|
||||
endpoint = `me/following?type=user&ids=${getFromUri('userid', uri)}`;
|
||||
data = {};
|
||||
}
|
||||
break;
|
||||
case 'playlist':
|
||||
if (method == 'GET') {
|
||||
endpoint = `playlists/${helpers.getFromUri('playlistid', uri)}/followers/contains?ids=${getState().spotify.me.id}`;
|
||||
endpoint = `playlists/${getFromUri('playlistid', uri)}/followers/contains?ids=${getState().spotify.me.id}`;
|
||||
} else {
|
||||
endpoint = `playlists/${helpers.getFromUri('playlistid', uri)}/followers?`;
|
||||
endpoint = `playlists/${getFromUri('playlistid', uri)}/followers?`;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -801,7 +816,7 @@ export function resolveRadioSeeds(radio) {
|
||||
let artist_ids = '';
|
||||
for (var i = 0; i < radio.seed_artists.length; i++) {
|
||||
if (i > 0) artist_ids += ',';
|
||||
artist_ids += helpers.getFromUri('artistid', radio.seed_artists[i]);
|
||||
artist_ids += getFromUri('artistid', radio.seed_artists[i]);
|
||||
}
|
||||
|
||||
request(dispatch, getState, `artists?ids=${artist_ids}`)
|
||||
@ -829,7 +844,7 @@ export function resolveRadioSeeds(radio) {
|
||||
let track_ids = '';
|
||||
for (var i = 0; i < radio.seed_tracks.length; i++) {
|
||||
if (i > 0) track_ids += ',';
|
||||
track_ids += helpers.getFromUri('trackid', radio.seed_tracks[i]);
|
||||
track_ids += getFromUri('trackid', radio.seed_tracks[i]);
|
||||
}
|
||||
|
||||
request(dispatch, getState, `tracks?ids=${track_ids}`)
|
||||
@ -908,17 +923,17 @@ export function getRecommendations(uris = [], limit = 20, tunabilities = null) {
|
||||
for (let i = 0; i < uris.length; i++) {
|
||||
const uri = uris[i];
|
||||
|
||||
switch (helpers.uriType(uri)) {
|
||||
switch (uriType(uri)) {
|
||||
case 'artist':
|
||||
artists_ids.push(helpers.getFromUri('artistid', uri));
|
||||
artists_ids.push(getFromUri('artistid', uri));
|
||||
break;
|
||||
|
||||
case 'track':
|
||||
tracks_ids.push(helpers.getFromUri('trackid', uri));
|
||||
tracks_ids.push(getFromUri('trackid', uri));
|
||||
break;
|
||||
|
||||
case 'genre':
|
||||
genres.push(helpers.getFromUri('genreid', uri));
|
||||
genres.push(getFromUri('genreid', uri));
|
||||
break;
|
||||
|
||||
case 'default':
|
||||
@ -992,9 +1007,9 @@ export function getRecommendations(uris = [], limit = 20, tunabilities = null) {
|
||||
dispatch({
|
||||
type: 'SPOTIFY_RECOMMENDATIONS_LOADED',
|
||||
seeds_uris: uris,
|
||||
tracks_uris: helpers.arrayOf('uri', tracks),
|
||||
tracks_uris: arrayOf('uri', tracks),
|
||||
artists_uris,
|
||||
albums_uris: helpers.arrayOf('uri', albums),
|
||||
albums_uris: arrayOf('uri', albums),
|
||||
});
|
||||
},
|
||||
(error) => {
|
||||
@ -1053,7 +1068,7 @@ export function getArtist(uri, full = false) {
|
||||
|
||||
// We need our artist, obviously
|
||||
const requests = [
|
||||
request(dispatch, getState, `artists/${helpers.getFromUri('artistid', uri)}`, 'GET', false, true)
|
||||
request(dispatch, getState, `artists/${getFromUri('artistid', uri)}`, 'GET', false, true)
|
||||
.then(
|
||||
(response) => {
|
||||
Object.assign(artist, response);
|
||||
@ -1070,7 +1085,7 @@ export function getArtist(uri, full = false) {
|
||||
// Do we want a full artist, with all supporting material?
|
||||
if (full) {
|
||||
requests.push(
|
||||
request(dispatch, getState, `artists/${helpers.getFromUri('artistid', uri)}/top-tracks?country=${getState().spotify.country}`)
|
||||
request(dispatch, getState, `artists/${getFromUri('artistid', uri)}/top-tracks?country=${getState().spotify.country}`)
|
||||
.then(
|
||||
(response) => {
|
||||
Object.assign(artist, response);
|
||||
@ -1085,11 +1100,11 @@ export function getArtist(uri, full = false) {
|
||||
);
|
||||
|
||||
requests.push(
|
||||
request(dispatch, getState, `artists/${helpers.getFromUri('artistid', uri)}/related-artists`)
|
||||
request(dispatch, getState, `artists/${getFromUri('artistid', uri)}/related-artists`)
|
||||
.then(
|
||||
(response) => {
|
||||
dispatch(coreActions.artistsLoaded(response.artists));
|
||||
Object.assign(artist, { related_artists_uris: helpers.arrayOf('uri', response.artists) });
|
||||
Object.assign(artist, { related_artists_uris: arrayOf('uri', response.artists) });
|
||||
},
|
||||
(error) => {
|
||||
dispatch(coreActions.handleException(
|
||||
@ -1113,7 +1128,7 @@ export function getArtist(uri, full = false) {
|
||||
|
||||
// Now go get our artist albums
|
||||
if (full) {
|
||||
request(dispatch, getState, `artists/${helpers.getFromUri('artistid', uri)}/albums?market=${getState().spotify.country}`)
|
||||
request(dispatch, getState, `artists/${getFromUri('artistid', uri)}/albums?market=${getState().spotify.country}`)
|
||||
.then(
|
||||
(response) => {
|
||||
dispatch({
|
||||
@ -1140,7 +1155,7 @@ export function getArtists(uris) {
|
||||
let ids = '';
|
||||
for (let i = 0; i < uris.length; i++) {
|
||||
if (ids != '') ids += ',';
|
||||
ids += helpers.getFromUri('artistid', uris[i]);
|
||||
ids += getFromUri('artistid', uris[i]);
|
||||
}
|
||||
|
||||
request(dispatch, getState, `artists/?ids=${ids}`)
|
||||
@ -1154,7 +1169,7 @@ export function getArtists(uris) {
|
||||
album: artist.albums[i],
|
||||
});
|
||||
}
|
||||
artist.albums_uris = helpers.arrayOf('uri', artist.albums);
|
||||
artist.albums_uris = arrayOf('uri', artist.albums);
|
||||
artist.albums_more = artist.albums.next;
|
||||
dispatch(coreActions.artistLoaded(artist));
|
||||
}
|
||||
@ -1199,15 +1214,15 @@ export function playArtistTopTracks(uri) {
|
||||
|
||||
// Do we have this artist (and their tracks) in our index already?
|
||||
if (typeof (artists[uri]) !== 'undefined' && typeof (artists[uri].tracks) !== 'undefined') {
|
||||
const uris = helpers.arrayOf('uri', artists[uri].tracks);
|
||||
const uris = arrayOf('uri', artists[uri].tracks);
|
||||
dispatch(mopidyActions.playURIs(uris, uri));
|
||||
|
||||
// We need to load the artist's top tracks first
|
||||
} else {
|
||||
request(dispatch, getState, `artists/${helpers.getFromUri('artistid', uri)}/top-tracks?country=${getState().spotify.country}`)
|
||||
request(dispatch, getState, `artists/${getFromUri('artistid', uri)}/top-tracks?country=${getState().spotify.country}`)
|
||||
.then(
|
||||
(response) => {
|
||||
const uris = helpers.arrayOf('uri', response.tracks);
|
||||
const uris = arrayOf('uri', response.tracks);
|
||||
dispatch(mopidyActions.playURIs(uris, uri));
|
||||
},
|
||||
(error) => {
|
||||
@ -1229,10 +1244,10 @@ export function playArtistTopTracks(uri) {
|
||||
|
||||
export function getUser(uri) {
|
||||
return (dispatch, getState) => {
|
||||
request(dispatch, getState, `users/${helpers.getFromUri('userid', uri)}`)
|
||||
request(dispatch, getState, `users/${getFromUri('userid', uri)}`)
|
||||
.then(
|
||||
(response) => {
|
||||
dispatch(coreActions.userLoaded(helpers.formatUser(response)));
|
||||
dispatch(coreActions.userLoaded(formatUser(response)));
|
||||
},
|
||||
(error) => {
|
||||
dispatch(coreActions.handleException(
|
||||
@ -1247,7 +1262,7 @@ export function getUser(uri) {
|
||||
export function getUserPlaylists(uri) {
|
||||
return (dispatch, getState) => {
|
||||
// get the first page of playlists
|
||||
request(dispatch, getState, `users/${helpers.getFromUri('userid', uri)}/playlists?limit=40`)
|
||||
request(dispatch, getState, `users/${getFromUri('userid', uri)}/playlists?limit=40`)
|
||||
.then(
|
||||
(response) => {
|
||||
const playlists = [];
|
||||
@ -1259,7 +1274,7 @@ export function getUserPlaylists(uri) {
|
||||
|
||||
const playlist = {
|
||||
|
||||
...helpers.formatPlaylist(raw_playlist),
|
||||
...formatPlaylist(raw_playlist),
|
||||
can_edit,
|
||||
tracks_total: raw_playlist.tracks.total,
|
||||
};
|
||||
@ -1293,7 +1308,7 @@ export function getUserPlaylists(uri) {
|
||||
export function getAlbum(uri) {
|
||||
return (dispatch, getState) => {
|
||||
// get the album
|
||||
request(dispatch, getState, `albums/${helpers.getFromUri('albumid', uri)}`)
|
||||
request(dispatch, getState, `albums/${getFromUri('albumid', uri)}`)
|
||||
.then(
|
||||
(response) => {
|
||||
// dispatch our loaded artists (simple objects)
|
||||
@ -1302,9 +1317,9 @@ export function getAlbum(uri) {
|
||||
const tracks = Object.assign([], response.tracks.items);
|
||||
|
||||
const album = {
|
||||
...helpers.formatAlbum(response),
|
||||
artists_uris: helpers.arrayOf('uri', response.artists),
|
||||
tracks_uris: helpers.arrayOf('uri', tracks),
|
||||
...formatAlbum(response),
|
||||
artists_uris: arrayOf('uri', response.artists),
|
||||
tracks_uris: arrayOf('uri', tracks),
|
||||
tracks_more: response.tracks.next,
|
||||
tracks_total: response.tracks.total,
|
||||
};
|
||||
@ -1324,7 +1339,7 @@ export function getAlbum(uri) {
|
||||
// we do this to get the artist artwork
|
||||
const artist_ids = [];
|
||||
for (var i = 0; i < response.artists.length; i++) {
|
||||
artist_ids.push(helpers.getFromUri('artistid', response.artists[i].uri));
|
||||
artist_ids.push(getFromUri('artistid', response.artists[i].uri));
|
||||
}
|
||||
|
||||
// get all album artists as full objects
|
||||
@ -1410,7 +1425,7 @@ export function savePlaylist(uri, name, description, is_public, is_collaborative
|
||||
|
||||
// Update the playlist fields
|
||||
request(
|
||||
dispatch, getState, `users/${getState().spotify.me.id}/playlists/${helpers.getFromUri('playlistid', uri)}`, 'PUT', data,
|
||||
dispatch, getState, `users/${getState().spotify.me.id}/playlists/${getFromUri('playlistid', uri)}`, 'PUT', data,
|
||||
)
|
||||
.then(
|
||||
(response) => {
|
||||
@ -1418,7 +1433,7 @@ export function savePlaylist(uri, name, description, is_public, is_collaborative
|
||||
|
||||
// Save the image
|
||||
if (image) {
|
||||
request(dispatch, getState, `users/${getState().spotify.me.id}/playlists/${helpers.getFromUri('playlistid', uri)}/images`, 'PUT', image)
|
||||
request(dispatch, getState, `users/${getState().spotify.me.id}/playlists/${getFromUri('playlistid', uri)}/images`, 'PUT', image)
|
||||
.then(
|
||||
|
||||
(response) => {
|
||||
@ -1468,7 +1483,7 @@ export function savePlaylist(uri, name, description, is_public, is_collaborative
|
||||
export function getPlaylist(uri) {
|
||||
return (dispatch, getState) => {
|
||||
// get the main playlist object
|
||||
request(dispatch, getState, `playlists/${helpers.getFromUri('playlistid', uri)}?market=${getState().spotify.country}`)
|
||||
request(dispatch, getState, `playlists/${getFromUri('playlistid', uri)}?market=${getState().spotify.country}`)
|
||||
.then(
|
||||
(response) => {
|
||||
// convert links in description
|
||||
@ -1480,20 +1495,20 @@ export function getPlaylist(uri) {
|
||||
description = description.split('<a href="spotify:user:').join('<a href="#' + '/user/spotify:user:');
|
||||
}
|
||||
|
||||
const tracks = helpers.formatTracks(response.tracks.items);
|
||||
const tracks = formatTracks(response.tracks.items);
|
||||
|
||||
const playlist = {
|
||||
|
||||
...helpers.formatPlaylist(response),
|
||||
...formatPlaylist(response),
|
||||
is_completely_loaded: true,
|
||||
user_uri: response.owner.uri,
|
||||
tracks_uris: tracks ? helpers.arrayOf('uri', tracks) : null,
|
||||
tracks_uris: tracks ? arrayOf('uri', tracks) : null,
|
||||
tracks_more: response.tracks.next,
|
||||
tracks_total: response.tracks.total,
|
||||
description,
|
||||
};
|
||||
|
||||
dispatch(coreActions.userLoaded(helpers.formatUser(response.owner)));
|
||||
dispatch(coreActions.userLoaded(formatUser(response.owner)));
|
||||
dispatch(coreActions.tracksLoaded(tracks));
|
||||
dispatch(coreActions.playlistLoaded(playlist));
|
||||
},
|
||||
@ -1599,7 +1614,7 @@ export function getAllPlaylistTracks(uri, shuffle = false, callback_action = nul
|
||||
'Loading playlist tracks',
|
||||
{
|
||||
uri,
|
||||
next: `playlists/${helpers.getFromUri('playlistid', uri)}/tracks?market=${getState().spotify.country}`,
|
||||
next: `playlists/${getFromUri('playlistid', uri)}/tracks?market=${getState().spotify.country}`,
|
||||
shuffle,
|
||||
play_next,
|
||||
at_position,
|
||||
@ -1663,7 +1678,7 @@ export function getAllPlaylistTracksProcessor(data) {
|
||||
));
|
||||
} else {
|
||||
if (data.shuffle) {
|
||||
uris = helpers.shuffle(uris);
|
||||
uris = shuffle(uris);
|
||||
}
|
||||
|
||||
// We don't bother "finishing", we just want it "finished" immediately
|
||||
@ -1690,7 +1705,7 @@ export function getAllPlaylistTracksProcessor(data) {
|
||||
|
||||
export function addTracksToPlaylist(uri, tracks_uris) {
|
||||
return (dispatch, getState) => {
|
||||
request(dispatch, getState, `playlists/${helpers.getFromUri('playlistid', uri)}/tracks`, 'POST', { uris: tracks_uris })
|
||||
request(dispatch, getState, `playlists/${getFromUri('playlistid', uri)}/tracks`, 'POST', { uris: tracks_uris })
|
||||
.then(
|
||||
(response) => {
|
||||
dispatch({
|
||||
@ -1712,7 +1727,7 @@ export function addTracksToPlaylist(uri, tracks_uris) {
|
||||
|
||||
export function deleteTracksFromPlaylist(uri, snapshot_id, tracks_indexes) {
|
||||
return (dispatch, getState) => {
|
||||
request(dispatch, getState, `playlists/${helpers.getFromUri('playlistid', uri)}/tracks`, 'DELETE', { snapshot_id, positions: tracks_indexes })
|
||||
request(dispatch, getState, `playlists/${getFromUri('playlistid', uri)}/tracks`, 'DELETE', { snapshot_id, positions: tracks_indexes })
|
||||
.then(
|
||||
(response) => {
|
||||
dispatch({
|
||||
@ -1734,7 +1749,7 @@ export function deleteTracksFromPlaylist(uri, snapshot_id, tracks_indexes) {
|
||||
|
||||
export function reorderPlaylistTracks(uri, range_start, range_length, insert_before, snapshot_id) {
|
||||
return (dispatch, getState) => {
|
||||
request(dispatch, getState, `playlists/${helpers.getFromUri('playlistid', uri)}/tracks`, 'PUT', {
|
||||
request(dispatch, getState, `playlists/${getFromUri('playlistid', uri)}/tracks`, 'PUT', {
|
||||
uri, range_start, range_length, insert_before, snapshot_id,
|
||||
})
|
||||
.then(
|
||||
|
||||
@ -2,17 +2,22 @@
|
||||
import ReactGA from 'react-ga';
|
||||
import { sha256 } from 'js-sha256';
|
||||
|
||||
const helpers = require('./../../helpers');
|
||||
import { arrayOf } from '../../util/arrays';
|
||||
import { upgradeSpotifyPlaylistUris } from '../../util/helpers';
|
||||
import {
|
||||
formatTracks,
|
||||
formatImages,
|
||||
formatUser,
|
||||
} from '../../util/format';
|
||||
const coreActions = require('../core/actions');
|
||||
const spotifyActions = require('./actions');
|
||||
const uiActions = require('../ui/actions');
|
||||
const pusherActions = require('../pusher/actions');
|
||||
|
||||
const SpotifyMiddleware = (function () {
|
||||
return (store) => (next) => (action) => {
|
||||
const { spotify } = store.getState();
|
||||
|
||||
switch (action.type) {
|
||||
switch (action.type) {
|
||||
case 'SPOTIFY_AUTHORIZATION_GRANTED':
|
||||
if (store.getState().ui.allow_reporting) {
|
||||
ReactGA.event({ category: 'Spotify', action: 'Authorization granted' });
|
||||
@ -94,7 +99,7 @@ const SpotifyMiddleware = (function () {
|
||||
});
|
||||
|
||||
// Collate result into the three key values we want
|
||||
action.uris = helpers.arrayOf('uri', action.data.albums.items);
|
||||
action.uris = arrayOf('uri', action.data.albums.items);
|
||||
action.more = action.data.albums.next;
|
||||
action.total = action.data.albums.total;
|
||||
|
||||
@ -107,7 +112,7 @@ const SpotifyMiddleware = (function () {
|
||||
store.dispatch({
|
||||
type: 'ARTIST_ALBUMS_LOADED',
|
||||
artist_uri: action.artist_uri,
|
||||
albums_uris: helpers.arrayOf('uri', action.data.items),
|
||||
albums_uris: arrayOf('uri', action.data.items),
|
||||
more: action.data.next,
|
||||
total: action.data.total,
|
||||
});
|
||||
@ -136,7 +141,7 @@ const SpotifyMiddleware = (function () {
|
||||
store.dispatch({
|
||||
type: 'USER_PLAYLISTS_LOADED',
|
||||
key: action.key,
|
||||
uris: helpers.arrayOf('uri', playlists),
|
||||
uris: arrayOf('uri', playlists),
|
||||
more: action.data.next,
|
||||
total: action.data.total,
|
||||
});
|
||||
@ -169,7 +174,7 @@ const SpotifyMiddleware = (function () {
|
||||
store.dispatch({
|
||||
type: 'CATEGORY_PLAYLISTS_LOADED',
|
||||
key: action.key,
|
||||
uris: helpers.arrayOf('uri',playlists),
|
||||
uris: arrayOf('uri',playlists),
|
||||
more: action.data.playlists.next,
|
||||
total: action.data.playlists.total
|
||||
});
|
||||
@ -179,13 +184,13 @@ const SpotifyMiddleware = (function () {
|
||||
case 'SPOTIFY_CATEGORY_PLAYLISTS_LOADED':
|
||||
store.dispatch(coreActions.playlistsLoaded(action.playlists.items));
|
||||
|
||||
action.uris = helpers.arrayOf('uri', action.playlists.items);
|
||||
action.uris = arrayOf('uri', action.playlists.items);
|
||||
action.more = action.playlists.next;
|
||||
action.total = action.playlists.total;
|
||||
delete action.playlists;
|
||||
|
||||
// Upgrade our URIs
|
||||
action.uris = helpers.upgradeSpotifyPlaylistUris(action.uris);
|
||||
action.uris = upgradeSpotifyPlaylistUris(action.uris);
|
||||
|
||||
next(action);
|
||||
break;
|
||||
@ -221,7 +226,7 @@ const SpotifyMiddleware = (function () {
|
||||
}
|
||||
|
||||
if (category.icons) {
|
||||
category.icons = helpers.formatImages(category.icons);
|
||||
category.icons = formatImages(category.icons);
|
||||
}
|
||||
|
||||
categories_loaded.push(category);
|
||||
@ -261,7 +266,7 @@ const SpotifyMiddleware = (function () {
|
||||
});
|
||||
|
||||
// Append our action with the uris. This gets handed down to subsequent middleware and our reducer.
|
||||
action.uris = helpers.arrayOf('uri', playlists);
|
||||
action.uris = arrayOf('uri', playlists);
|
||||
next(action);
|
||||
break;
|
||||
|
||||
@ -288,7 +293,7 @@ const SpotifyMiddleware = (function () {
|
||||
});
|
||||
|
||||
// Append our action with the uris. This gets handed down to subsequent middleware and our reducer.
|
||||
action.uris = helpers.arrayOf('uri', artists);
|
||||
action.uris = arrayOf('uri', artists);
|
||||
next(action);
|
||||
break;
|
||||
|
||||
@ -327,7 +332,7 @@ const SpotifyMiddleware = (function () {
|
||||
});
|
||||
|
||||
// Append our action with the uris. This gets handed down to subsequent middleware and our reducer.
|
||||
action.uris = helpers.arrayOf('uri', albums);
|
||||
action.uris = arrayOf('uri', albums);
|
||||
next(action);
|
||||
break;
|
||||
|
||||
@ -337,14 +342,14 @@ const SpotifyMiddleware = (function () {
|
||||
type: 'ARTISTS_LOADED',
|
||||
artists: action.artists,
|
||||
});
|
||||
action.artists_uris = helpers.arrayOf('uri', action.artists);
|
||||
action.artists_uris = arrayOf('uri', action.artists);
|
||||
}
|
||||
if (action.tracks.length > 0) {
|
||||
store.dispatch({
|
||||
type: 'TRACKS_LOADED',
|
||||
tracks: action.tracks,
|
||||
});
|
||||
action.tracks_uris = helpers.arrayOf('uri', action.tracks);
|
||||
action.tracks_uris = arrayOf('uri', action.tracks);
|
||||
}
|
||||
next(action);
|
||||
break;
|
||||
@ -385,7 +390,7 @@ const SpotifyMiddleware = (function () {
|
||||
store.dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'tracks',
|
||||
results: helpers.formatTracks(action.data.tracks.items),
|
||||
results: formatTracks(action.data.tracks.items),
|
||||
more: action.data.tracks.next,
|
||||
});
|
||||
break;
|
||||
@ -400,7 +405,7 @@ const SpotifyMiddleware = (function () {
|
||||
store.dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'artists',
|
||||
results: helpers.arrayOf('uri', action.data.playlists.items),
|
||||
results: arrayOf('uri', action.data.playlists.items),
|
||||
more: action.data.playlists.next,
|
||||
});
|
||||
break;
|
||||
@ -415,7 +420,7 @@ const SpotifyMiddleware = (function () {
|
||||
store.dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'playlists',
|
||||
results: helpers.arrayOf('uri', action.data.albums.items),
|
||||
results: arrayOf('uri', action.data.albums.items),
|
||||
more: action.data.albums.next,
|
||||
});
|
||||
break;
|
||||
@ -439,14 +444,14 @@ const SpotifyMiddleware = (function () {
|
||||
store.dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'playlists',
|
||||
results: helpers.arrayOf('uri', action.data.playlists.items),
|
||||
results: arrayOf('uri', action.data.playlists.items),
|
||||
more: action.data.playlists.next,
|
||||
});
|
||||
break;
|
||||
|
||||
|
||||
case 'SPOTIFY_ME_LOADED':
|
||||
var me = { ...helpers.formatUser(action.me) };
|
||||
var me = { ...formatUser(action.me) };
|
||||
|
||||
// We are Anonymous currently so use 'me' name as my Pusher username
|
||||
if (store.getState().pusher.username == 'Anonymous') {
|
||||
@ -468,7 +473,7 @@ const SpotifyMiddleware = (function () {
|
||||
default:
|
||||
return next(action);
|
||||
}
|
||||
};
|
||||
};
|
||||
}());
|
||||
|
||||
export default SpotifyMiddleware;
|
||||
export default SpotifyMiddleware;
|
||||
|
||||
@ -1,70 +1,71 @@
|
||||
|
||||
import * as helpers from '../../helpers';
|
||||
import { removeDuplicates, arrayOf } from '../../util/arrays';
|
||||
import { formatTracks } from '../../util/format';
|
||||
|
||||
export default function reducer(spotify = {}, action) {
|
||||
switch (action.type) {
|
||||
switch (action.type) {
|
||||
case 'SPOTIFY_SET':
|
||||
return { ...spotify, ...action.data };
|
||||
|
||||
case 'PUSHER_SPOTIFY_TOKEN':
|
||||
if (spotify.authorization) return spotify;
|
||||
return {
|
||||
...spotify,
|
||||
return {
|
||||
...spotify,
|
||||
authorizing: false,
|
||||
authorization: false,
|
||||
access_token: action.data.access_token,
|
||||
token_expiry: action.data.token_expiry,
|
||||
token_expiry: action.data.token_expiry,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_AUTHORIZATION_GRANTED':
|
||||
return {
|
||||
...spotify,
|
||||
return {
|
||||
...spotify,
|
||||
enabled: true,
|
||||
authorizing: false,
|
||||
authorization: action.data,
|
||||
access_token: action.data.access_token,
|
||||
refresh_token: action.data.refresh_token,
|
||||
token_expiry: action.data.token_expiry,
|
||||
token_expiry: action.data.token_expiry,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_AUTHORIZATION_REVOKED':
|
||||
return {
|
||||
...spotify,
|
||||
return {
|
||||
...spotify,
|
||||
authorizing: false,
|
||||
authorization: false,
|
||||
access_token: false,
|
||||
refresh_token: false,
|
||||
token_expiry: 0,
|
||||
me: false,
|
||||
me: false,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_IMPORT_AUTHORIZATION':
|
||||
return {
|
||||
...spotify,
|
||||
return {
|
||||
...spotify,
|
||||
authorizing: false,
|
||||
authorization: action.authorization,
|
||||
access_token: action.authorization.access_token,
|
||||
refresh_token: action.authorization.refresh_token,
|
||||
token_expiry: action.authorization.token_expiry,
|
||||
me: action.me,
|
||||
me: action.me,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_TOKEN_REFRESHING':
|
||||
return { ...spotify, refreshing_token: true };
|
||||
|
||||
case 'SPOTIFY_TOKEN_REFRESHED':
|
||||
return {
|
||||
...spotify,
|
||||
return {
|
||||
...spotify,
|
||||
refreshing_token: false,
|
||||
access_token: action.data.access_token,
|
||||
token_expiry: action.data.token_expiry,
|
||||
token_expiry: action.data.token_expiry,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_TOKEN_CHANGED':
|
||||
return {
|
||||
...spotify,
|
||||
return {
|
||||
...spotify,
|
||||
access_token: action.spotify_token.access_token,
|
||||
token_expiry: action.spotify_token.token_expiry,
|
||||
token_expiry: action.spotify_token.token_expiry,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_ME_LOADED':
|
||||
@ -79,11 +80,11 @@ export default function reducer(spotify = {}, action) {
|
||||
if (spotify.new_releases) {
|
||||
new_releases = Object.assign([], spotify.new_releases);
|
||||
}
|
||||
return {
|
||||
...spotify,
|
||||
new_releases: helpers.removeDuplicates([...new_releases, ...action.uris]),
|
||||
return {
|
||||
...spotify,
|
||||
new_releases: removeDuplicates([...new_releases, ...action.uris]),
|
||||
new_releases_more: action.more,
|
||||
new_releases_total: action.total,
|
||||
new_releases_total: action.total,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_DISCOVER_LOADED':
|
||||
@ -111,7 +112,7 @@ export default function reducer(spotify = {}, action) {
|
||||
artists_uris: action.artists_uris,
|
||||
albums_uris: action.albums_uris,
|
||||
tracks_uris: action.tracks_uris,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
case 'SPOTIFY_FAVORITES_LOADED':
|
||||
@ -119,7 +120,7 @@ export default function reducer(spotify = {}, action) {
|
||||
|
||||
...spotify,
|
||||
favorite_artists: action.artists_uris,
|
||||
favorite_tracks: action.tracks_uris,
|
||||
favorite_tracks: action.tracks_uris,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_AUTOCOMPLETE_LOADING':
|
||||
@ -128,7 +129,7 @@ export default function reducer(spotify = {}, action) {
|
||||
return {
|
||||
|
||||
...spotify,
|
||||
autocomplete_results,
|
||||
autocomplete_results,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_AUTOCOMPLETE_LOADED':
|
||||
@ -138,7 +139,7 @@ export default function reducer(spotify = {}, action) {
|
||||
return {
|
||||
|
||||
...spotify,
|
||||
autocomplete_results,
|
||||
autocomplete_results,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_AUTOCOMPLETE_CLEAR':
|
||||
@ -149,14 +150,14 @@ export default function reducer(spotify = {}, action) {
|
||||
return {
|
||||
|
||||
...spotify,
|
||||
autocomplete_results,
|
||||
autocomplete_results,
|
||||
};
|
||||
|
||||
case 'SPOTIFY_GENRES_LOADED':
|
||||
return {
|
||||
|
||||
...spotify,
|
||||
genres: action.genres,
|
||||
genres: action.genres,
|
||||
};
|
||||
|
||||
|
||||
@ -218,7 +219,7 @@ export default function reducer(spotify = {}, action) {
|
||||
} else {
|
||||
var { uris } = action;
|
||||
}
|
||||
return { ...spotify, library_playlists: helpers.removeDuplicates(uris) };
|
||||
return { ...spotify, library_playlists: removeDuplicates(uris) };
|
||||
|
||||
case 'SPOTIFY_LIBRARY_ARTISTS_LOADED':
|
||||
if (spotify.library_artists) {
|
||||
@ -226,7 +227,7 @@ export default function reducer(spotify = {}, action) {
|
||||
} else {
|
||||
var { uris } = action;
|
||||
}
|
||||
return { ...spotify, library_artists: helpers.removeDuplicates(uris) };
|
||||
return { ...spotify, library_artists: removeDuplicates(uris) };
|
||||
|
||||
case 'SPOTIFY_LIBRARY_ALBUMS_LOADED':
|
||||
if (spotify.library_albums) {
|
||||
@ -234,7 +235,7 @@ export default function reducer(spotify = {}, action) {
|
||||
} else {
|
||||
var { uris } = action;
|
||||
}
|
||||
return { ...spotify, library_albums: helpers.removeDuplicates(uris) };
|
||||
return { ...spotify, library_albums: removeDuplicates(uris) };
|
||||
|
||||
case 'SPOTIFY_LIBRARY_TRACKS_LOADED':
|
||||
case 'SPOTIFY_LIBRARY_TRACKS_LOADED_MORE':
|
||||
@ -242,17 +243,17 @@ export default function reducer(spotify = {}, action) {
|
||||
var uris = [];
|
||||
|
||||
if (tracks) {
|
||||
tracks = helpers.formatTracks(tracks);
|
||||
uris = helpers.arrayOf('uri', tracks);
|
||||
tracks = formatTracks(tracks);
|
||||
uris = arrayOf('uri', tracks);
|
||||
if (spotify.library_tracks) {
|
||||
uris = [...spotify.library_tracks, ...uris];
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...spotify,
|
||||
library_tracks: helpers.removeDuplicates(uris),
|
||||
library_tracks_more: action.data.next,
|
||||
return {
|
||||
...spotify,
|
||||
library_tracks: removeDuplicates(uris),
|
||||
library_tracks_more: action.data.next,
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user