Persistent storage; Authorization via spotify

This commit is contained in:
James Barnsley
2016-10-17 10:39:11 +13:00
parent f738766898
commit 4bff7ee0a9
23 changed files with 409 additions and 143 deletions

View File

@ -3,9 +3,22 @@
* Actions and Action Creators
**/
export function authorize(){
export function startAuthorization(){
return {
type: 'SPOTIFY_AUTHORIZING'
type: 'SPOTIFY_START_AUTHORIZATION'
}
}
export function removeAuthorization(){
return {
type: 'SPOTIFY_REMOVE_AUTHORIZATION'
}
}
export function completeAuthorization( data ){
return {
type: 'SPOTIFY_COMPLETE_AUTHORIZATION',
data: data
}
}
@ -28,3 +41,10 @@ export function loadAlbum( uri ){
}
}
export function loadArtist( uri ){
return {
type: 'SPOTIFY_LOAD_ARTIST',
uri: uri
}
}

View File

@ -3,30 +3,56 @@ import actions from './actions'
const SpotifyMiddleware = (function(){
return store => next => action => {
/**
* Send an ajax request to the Spotify API
*
* @param endpoint string = the url to query (ie /albums/:uri)
* @param method string
* @param data mixed = request payload
* @return ajax promise
**/
const sendRequest = (endpoint, method = 'GET', data = false) => {
return $.ajax({
method: method,
cache: true,
url: 'https://api.spotify.com/v1/'+endpoint,
data: data
});
}
/**
* The actual middleware inteceptor
**/
return store => next => action => {
switch(action.type) {
case 'SPOTIFY_LOAD_ALBUM':
// clear the current album
store.dispatch({ type: 'SPOTIFY_ALBUM_LOADED', data: false })
var id = action.uri.replace('spotify:album:','');
$.ajax({
method: 'GET',
cache: true,
url: 'https://api.spotify.com/v1/albums/'+id,
success: function(album){
store.dispatch({ type: 'SPOTIFY_ALBUM_LOADED', data: album })
}
});
sendRequest('albums/'+id)
.then( (response) => {
store.dispatch({ type: 'SPOTIFY_ALBUM_LOADED', data: response })
});
break;
case 'SPOTIFY_LOAD_ARTIST':
store.dispatch({ type: 'SPOTIFY_ARTIST_LOADED', data: false })
var id = action.uri.replace('spotify:artist:','');
sendRequest('artists/'+id)
.then( (response) => {
store.dispatch({ type: 'SPOTIFY_ARTIST_LOADED', data: response })
});
break;
case 'SPOTIFY_CONNECT':
console.log('Spotify wants to connect')
break;
case 'SPOTIFY_DISCONNECT':

View File

@ -5,9 +5,15 @@ export default function reducer(spotify = {}, action){
case 'SPOTIFY_CONNECTING':
return Object.assign({}, spotify, { connected: false, connecting: true });
case 'SPOTIFY_AUTHORIZING':
case 'SPOTIFY_START_AUTHORIZATION':
return Object.assign({}, spotify, { authorizing: true });
case 'SPOTIFY_COMPLETE_AUTHORIZATION':
return Object.assign({}, spotify, { authorizing: false, authorization: action.data });
case 'SPOTIFY_REMOVE_AUTHORIZATION':
return Object.assign({}, spotify, { authorizing: false, authorization: false });
case 'SPOTIFY_CONNECTED':
return Object.assign({}, spotify, { connected: true, connecting: false });
@ -17,6 +23,12 @@ export default function reducer(spotify = {}, action){
case 'SPOTIFY_ALBUM_LOADED':
return Object.assign({}, spotify, { album: action.data });
case 'SPOTIFY_ARTIST_LOADED':
return Object.assign({}, spotify, { artist: action.data });
case 'SPOTIFY_LIBRARY_ARTISTS_LOADED':
return Object.assign({}, spotify, { library_artists: action.data });
default:
return spotify
}