2016-10-17 10:39:11 +13:00
|
|
|
|
|
|
|
|
const localstorageMiddleware = (function(){
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The actual middleware inteceptor
|
|
|
|
|
**/
|
|
|
|
|
return store => next => action => {
|
2016-10-19 14:09:34 +13:00
|
|
|
|
|
|
|
|
console.log(action)
|
2016-10-17 10:39:11 +13:00
|
|
|
|
|
|
|
|
// proceed as normal first
|
|
|
|
|
// this way, any reducers and middleware do their thing BEFORE we store our new state
|
|
|
|
|
next(action);
|
|
|
|
|
|
2016-10-18 16:53:33 +13:00
|
|
|
switch( action.type ){
|
|
|
|
|
|
|
|
|
|
case 'MOPIDY_SET_CONFIG':
|
|
|
|
|
var mopidy = {
|
2016-10-18 21:57:29 +13:00
|
|
|
host: action.config.host,
|
|
|
|
|
port: action.config.port
|
2016-10-18 16:53:33 +13:00
|
|
|
};
|
|
|
|
|
localStorage.setItem('mopidy', JSON.stringify(mopidy));
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 21:57:29 +13:00
|
|
|
case 'SPOTIFY_SET_CONFIG':
|
|
|
|
|
var spotify = {
|
|
|
|
|
country: action.config.country,
|
|
|
|
|
locale: action.config.locale
|
|
|
|
|
};
|
|
|
|
|
localStorage.setItem('spotify', JSON.stringify(spotify));
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-21 16:34:16 +13:00
|
|
|
case 'SPOTIFY_AUTHORIZATION_GRANTED':
|
2016-10-19 14:09:34 +13:00
|
|
|
var spotify = JSON.parse( localStorage.getItem('spotify') );
|
|
|
|
|
if( !spotify ) spotify = {};
|
|
|
|
|
Object.assign(
|
|
|
|
|
spotify,{
|
|
|
|
|
authorized: true,
|
|
|
|
|
access_token: action.data.access_token,
|
2016-10-21 16:34:16 +13:00
|
|
|
refresh_token: action.data.refresh_token,
|
|
|
|
|
token_expiry: action.data.token_expiry
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
localStorage.setItem('spotify', JSON.stringify(spotify));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'SPOTIFY_TOKEN_REFRESHED':
|
|
|
|
|
var spotify = JSON.parse( localStorage.getItem('spotify') );
|
|
|
|
|
if( !spotify ) spotify = {};
|
|
|
|
|
Object.assign(
|
|
|
|
|
spotify,{
|
|
|
|
|
access_token: action.data.access_token,
|
|
|
|
|
token_expiry: action.data.token_expiry
|
2016-10-19 14:09:34 +13:00
|
|
|
}
|
|
|
|
|
);
|
2016-10-18 16:53:33 +13:00
|
|
|
localStorage.setItem('spotify', JSON.stringify(spotify));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
export default localstorageMiddleware
|