Files
Iris/src/js/services/lastfm/actions.js

137 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-11-07 21:21:15 +13:00
var coreActions = require('../core/actions')
2017-03-27 14:23:38 +13:00
var uiActions = require('../ui/actions')
var helpers = require('../../helpers')
2016-11-07 21:21:15 +13:00
/**
* Send an ajax request to the Spotify API
*
* @param dispatch obj
* @param getState obj
* @param endpoint params = the url params to send
**/
const sendRequest = ( dispatch, getState, params ) => {
return new Promise( (resolve, reject) => {
2016-11-07 21:21:15 +13:00
2017-03-27 14:23:38 +13:00
var loader_key = helpers.generateGuid()
dispatch(uiActions.startLoading(loader_key, 'lastfm_'+params))
var config = {
method: 'GET',
cache: true,
timeout: 15000,
url: '//ws.audioscrobbler.com/2.0/?format=json&api_key=4320a3ef51c9b3d69de552ac083c55e3&'+params
}
$.ajax(config).then(
2017-03-27 14:23:38 +13:00
response => {
dispatch(uiActions.stopLoading(loader_key))
resolve(response)
},
2016-11-07 21:21:15 +13:00
(xhr, status, error) => {
2017-03-27 14:23:38 +13:00
dispatch(uiActions.stopLoading(loader_key))
dispatch(coreActions.handleException(
'LastFM: '+xhr.responseText,
{
config: config,
error: error,
status: status,
xhr: xhr
}
));
2016-11-07 21:21:15 +13:00
reject(error)
}
)
})
}
export function connect(){
return (dispatch, getState) => {
dispatch({ type: 'LASTFM_CONNECTING' })
sendRequest(dispatch, getState, 'method=artist.getInfo&artist=')
.then(
response => {
dispatch({ type: 'LASTFM_CONNECTED' })
}
)
}
}
2017-01-15 21:06:24 +13:00
export function getArtist( uri, artist, mbid = false ){
2016-11-07 21:21:15 +13:00
return (dispatch, getState) => {
if( mbid ){
var params = 'method=artist.getInfo&mbid='+mbid
}else{
artist = encodeURIComponent( artist );
var params = 'method=artist.getInfo&artist='+artist
}
sendRequest(dispatch, getState, params)
.then(
response => {
if( response.artist ){
dispatch({
2017-01-15 21:06:24 +13:00
type: 'ARTIST_LOADED',
2017-01-19 16:13:47 +13:00
key: uri,
2017-01-15 21:06:24 +13:00
artist: {
images: response.artist.image,
2017-02-08 20:05:37 +13:00
bio: response.artist.bio,
listeners: parseInt(response.artist.stats.listeners),
play_count: parseInt(response.artist.stats.playcount),
on_tour: response.artist.stats.ontour
2017-01-15 21:06:24 +13:00
}
2016-11-07 21:21:15 +13:00
});
}
}
)
}
}
export function getAlbum( artist, album, mbid = false ){
return (dispatch, getState) => {
dispatch({ type: 'LASTFM_ALBUM_LOADED', data: false });
if( mbid ){
var params = 'method=album.getInfo&mbid='+mbid
}else{
artist = encodeURIComponent( artist )
album = encodeURIComponent( album )
var params = 'method=album.getInfo&album='+album+'&artist='+artist
}
sendRequest(dispatch, getState, params)
.then(
response => {
if( response.album ){
dispatch({
type: 'LASTFM_ALBUM_LOADED',
data: response.album
});
}
}
)
}
}
export function getTrack( artist, track ){
return (dispatch, getState) => {
dispatch({ type: 'LASTFM_TRACK_LOADED', data: false });
artist = encodeURIComponent( artist );
sendRequest(dispatch, getState, 'method=track.getInfo&track='+track+'&artist='+artist)
.then(
response => {
if( response.track ){
dispatch({
type: 'LASTFM_TRACK_LOADED',
data: response.track
});
}
}
)
}
}