2016-10-17 10:39:11 +13:00
|
|
|
|
|
|
|
|
const localstorageMiddleware = (function(){
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The actual middleware inteceptor
|
|
|
|
|
**/
|
|
|
|
|
return store => next => action => {
|
|
|
|
|
|
|
|
|
|
// proceed as normal first
|
|
|
|
|
// this way, any reducers and middleware do their thing BEFORE we store our new state
|
|
|
|
|
next(action);
|
|
|
|
|
|
2016-11-07 18:09:42 +13:00
|
|
|
// append our state to a global variable. This gives us access to debug the store at any point
|
|
|
|
|
window._store = store
|
2016-12-20 16:58:20 +13:00
|
|
|
console.log(action)
|
2016-11-07 18:09:42 +13:00
|
|
|
|
2016-10-18 16:53:33 +13:00
|
|
|
switch( action.type ){
|
|
|
|
|
|
2016-10-31 09:25:15 +13:00
|
|
|
case 'PUSHER_CONNECTED':
|
2016-10-28 15:58:07 +13:00
|
|
|
var pusher = JSON.parse( localStorage.getItem('pusher') );
|
|
|
|
|
if( !pusher ) pusher = {};
|
|
|
|
|
Object.assign(
|
|
|
|
|
pusher,{
|
2016-10-31 09:25:15 +13:00
|
|
|
username: action.connection.username,
|
|
|
|
|
connectionid: action.connection.connectionid
|
2016-10-28 15:58:07 +13:00
|
|
|
}
|
|
|
|
|
);
|
2016-10-28 09:14:29 +13:00
|
|
|
localStorage.setItem('pusher', JSON.stringify(pusher));
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-31 09:25:15 +13:00
|
|
|
case 'PUSHER_SET_PORT':
|
2016-10-28 09:14:29 +13:00
|
|
|
var pusher = JSON.parse( localStorage.getItem('pusher') );
|
|
|
|
|
if( !pusher ) pusher = {};
|
2016-10-31 09:25:15 +13:00
|
|
|
Object.assign( pusher, { port: action.port } );
|
2016-10-28 09:14:29 +13:00
|
|
|
localStorage.setItem('pusher', JSON.stringify(pusher));
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-28 15:58:07 +13:00
|
|
|
case 'PUSHER_CHANGE_USERNAME':
|
2016-11-17 21:24:43 +13:00
|
|
|
var stored_pusher = JSON.parse( localStorage.getItem('pusher') )
|
|
|
|
|
var pusher = Object.assign({}, stored_pusher, { username: action.data.connection.username })
|
|
|
|
|
localStorage.setItem('pusher', JSON.stringify(pusher))
|
2016-10-28 15:58:07 +13:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 16:53:33 +13:00
|
|
|
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-11-07 18:09:42 +13:00
|
|
|
case 'MOPIDY_URISCHEMES_FILTERED':
|
|
|
|
|
var mopidy = JSON.parse( localStorage.getItem('mopidy') );
|
|
|
|
|
if( !mopidy ) mopidy = {};
|
|
|
|
|
Object.assign( mopidy, { uri_schemes: action.data });
|
|
|
|
|
localStorage.setItem('mopidy', JSON.stringify(mopidy));
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 21:57:29 +13:00
|
|
|
case 'SPOTIFY_SET_CONFIG':
|
2016-12-12 09:07:47 +13:00
|
|
|
var spotify = JSON.parse( localStorage.getItem('spotify') );
|
|
|
|
|
if( !spotify ) spotify = {};
|
|
|
|
|
Object.assign(
|
|
|
|
|
spotify,{
|
|
|
|
|
country: action.config.country,
|
|
|
|
|
locale: action.config.locale
|
|
|
|
|
}
|
|
|
|
|
);
|
2016-10-18 21:57:29 +13:00
|
|
|
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,
|
2016-12-12 09:07:47 +13:00
|
|
|
authorization: action.data,
|
2016-10-19 14:09:34 +13:00
|
|
|
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;
|
|
|
|
|
|
2016-10-28 15:58:07 +13:00
|
|
|
case 'SPOTIFY_AUTHORIZATION_REVOKED':
|
|
|
|
|
var spotify = JSON.parse( localStorage.getItem('spotify') );
|
|
|
|
|
if( !spotify ) spotify = {};
|
|
|
|
|
Object.assign(
|
|
|
|
|
spotify,{
|
|
|
|
|
authorized: false,
|
|
|
|
|
access_token: false,
|
|
|
|
|
refresh_token: false,
|
|
|
|
|
token_expiry: false
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
localStorage.setItem('spotify', JSON.stringify(spotify));
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-21 16:34:16 +13:00
|
|
|
case 'SPOTIFY_TOKEN_REFRESHED':
|
|
|
|
|
var spotify = JSON.parse( localStorage.getItem('spotify') );
|
|
|
|
|
if( !spotify ) spotify = {};
|
|
|
|
|
Object.assign(
|
|
|
|
|
spotify,{
|
|
|
|
|
access_token: action.data.access_token,
|
2016-11-30 09:32:46 +13:00
|
|
|
token_expiry: action.data.token_expiry,
|
|
|
|
|
provider: action.provider
|
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-28 15:58:07 +13:00
|
|
|
|
|
|
|
|
case 'SPOTIFY_ME_LOADED':
|
|
|
|
|
var spotify = JSON.parse( localStorage.getItem('spotify') );
|
|
|
|
|
if( !spotify ) spotify = {};
|
|
|
|
|
Object.assign(
|
2016-12-12 09:07:47 +13:00
|
|
|
spotify,
|
|
|
|
|
{ me: action.data }
|
2016-10-28 15:58:07 +13:00
|
|
|
);
|
|
|
|
|
localStorage.setItem('spotify', JSON.stringify(spotify));
|
|
|
|
|
break;
|
2016-11-21 16:26:47 +13:00
|
|
|
|
2016-12-09 18:47:10 +13:00
|
|
|
case 'UI_SET':
|
2016-11-21 16:26:47 +13:00
|
|
|
var ui = JSON.parse( localStorage.getItem('ui') );
|
|
|
|
|
if( !ui ) ui = {};
|
2016-12-09 18:47:10 +13:00
|
|
|
Object.assign( ui, action.data );
|
2016-11-21 16:26:47 +13:00
|
|
|
localStorage.setItem('ui', JSON.stringify(ui));
|
|
|
|
|
break;
|
2016-10-18 16:53:33 +13:00
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
export default localstorageMiddleware
|