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
|
2017-01-16 18:31:06 +13:00
|
|
|
|
|
|
|
|
// if debug enabled
|
2017-06-07 08:28:54 +12:00
|
|
|
if (store.getState().ui.log_actions){
|
|
|
|
|
|
|
|
|
|
var ignored_actions = [
|
|
|
|
|
'START_LOADING',
|
|
|
|
|
'STOP_LOADING'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
// Show non-ignored actions
|
|
|
|
|
if (!ignored_actions.includes(action.type)){
|
|
|
|
|
console.log(action)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-07 18:09:42 +13:00
|
|
|
|
2017-10-10 13:05:28 +13:00
|
|
|
switch(action.type){
|
2016-10-18 16:53:33 +13:00
|
|
|
|
2016-10-31 09:25:15 +13:00
|
|
|
case 'PUSHER_CONNECTED':
|
2017-10-10 13:05:28 +13:00
|
|
|
var pusher = JSON.parse(localStorage.getItem('pusher') );
|
|
|
|
|
if (!pusher ) pusher = {};
|
2016-10-28 15:58:07 +13:00
|
|
|
Object.assign(
|
|
|
|
|
pusher,{
|
2017-02-19 21:27:43 +13:00
|
|
|
connection_id: action.connection_id
|
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':
|
2017-10-10 13:05:28 +13:00
|
|
|
var pusher = JSON.parse(localStorage.getItem('pusher') );
|
|
|
|
|
if (!pusher ) pusher = {};
|
|
|
|
|
Object.assign(pusher, { port: action.port } );
|
2016-10-28 09:14:29 +13:00
|
|
|
localStorage.setItem('pusher', JSON.stringify(pusher));
|
|
|
|
|
break;
|
|
|
|
|
|
2017-02-19 21:27:43 +13:00
|
|
|
case 'PUSHER_USERNAME_CHANGED':
|
2017-10-10 13:05:28 +13:00
|
|
|
var stored_pusher = JSON.parse(localStorage.getItem('pusher') )
|
2017-02-19 21:27:43 +13:00
|
|
|
var pusher = Object.assign({}, stored_pusher, { username: action.username })
|
2016-11-17 21:24:43 +13:00
|
|
|
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':
|
2017-10-10 13:05:28 +13:00
|
|
|
var mopidy = JSON.parse(localStorage.getItem('mopidy') );
|
|
|
|
|
if (!mopidy ) mopidy = {};
|
|
|
|
|
Object.assign(mopidy, { uri_schemes: action.data });
|
2016-11-07 18:09:42 +13:00
|
|
|
localStorage.setItem('mopidy', JSON.stringify(mopidy));
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 21:57:29 +13:00
|
|
|
case 'SPOTIFY_SET_CONFIG':
|
2017-10-10 13:05:28 +13:00
|
|
|
var spotify = JSON.parse(localStorage.getItem('spotify') );
|
|
|
|
|
if (!spotify ) spotify = {};
|
2016-12-12 09:07:47 +13:00
|
|
|
Object.assign(
|
|
|
|
|
spotify,{
|
2017-08-04 09:19:19 +12:00
|
|
|
authentication_provider: action.config.authentication_provider,
|
2016-12-12 09:07:47 +13:00
|
|
|
country: action.config.country,
|
|
|
|
|
locale: action.config.locale
|
|
|
|
|
}
|
|
|
|
|
);
|
2016-10-18 21:57:29 +13:00
|
|
|
localStorage.setItem('spotify', JSON.stringify(spotify));
|
|
|
|
|
break;
|
|
|
|
|
|
2017-09-08 14:40:43 +12:00
|
|
|
case 'SPOTIFY_IMPORT_AUTHORIZATION':
|
2016-10-21 16:34:16 +13:00
|
|
|
case 'SPOTIFY_AUTHORIZATION_GRANTED':
|
2017-09-08 14:40:43 +12:00
|
|
|
if (action.authorization !== undefined){
|
|
|
|
|
var authorization = action.authorization;
|
|
|
|
|
} else if (action.data){
|
|
|
|
|
var authorization = action.data;
|
|
|
|
|
}
|
2017-10-10 13:05:28 +13:00
|
|
|
var spotify = JSON.parse(localStorage.getItem('spotify') );
|
2017-08-04 22:33:49 +12:00
|
|
|
spotify = Object.assign(
|
|
|
|
|
{},
|
|
|
|
|
(spotify ? spotify : {}),
|
|
|
|
|
{
|
2017-09-08 14:40:43 +12:00
|
|
|
authorization: authorization,
|
|
|
|
|
access_token: authorization.access_token,
|
|
|
|
|
refresh_token: authorization.refresh_token,
|
|
|
|
|
token_expiry: authorization.token_expiry
|
2016-10-21 16:34:16 +13:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
localStorage.setItem('spotify', JSON.stringify(spotify));
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-28 15:58:07 +13:00
|
|
|
case 'SPOTIFY_AUTHORIZATION_REVOKED':
|
2017-10-10 13:05:28 +13:00
|
|
|
var spotify = JSON.parse(localStorage.getItem('spotify') );
|
2017-08-04 22:33:49 +12:00
|
|
|
spotify = Object.assign(
|
|
|
|
|
{},
|
|
|
|
|
(spotify ? spotify : {}),
|
|
|
|
|
{
|
|
|
|
|
authorization: false,
|
2016-10-28 15:58:07 +13:00
|
|
|
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':
|
2017-10-10 13:05:28 +13:00
|
|
|
var spotify = JSON.parse(localStorage.getItem('spotify') );
|
|
|
|
|
if (!spotify ) spotify = {};
|
2016-10-21 16:34:16 +13:00
|
|
|
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':
|
2017-10-10 13:05:28 +13:00
|
|
|
var spotify = JSON.parse(localStorage.getItem('spotify') );
|
|
|
|
|
if (!spotify ) spotify = {};
|
2016-10-28 15:58:07 +13:00
|
|
|
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
|
|
|
|
2017-08-05 22:25:25 +12:00
|
|
|
case 'CORE_SET':
|
2017-10-10 13:05:28 +13:00
|
|
|
var core = JSON.parse(localStorage.getItem('core') );
|
|
|
|
|
if (!core ) core = {};
|
|
|
|
|
Object.assign(core, action.data );
|
2017-08-05 22:25:25 +12:00
|
|
|
localStorage.setItem('core', JSON.stringify(core));
|
|
|
|
|
break
|
|
|
|
|
|
2016-12-09 18:47:10 +13:00
|
|
|
case 'UI_SET':
|
2017-10-10 13:05:28 +13:00
|
|
|
var ui = JSON.parse(localStorage.getItem('ui') );
|
|
|
|
|
if (!ui ) ui = {};
|
|
|
|
|
Object.assign(ui, action.data );
|
2016-11-21 16:26:47 +13:00
|
|
|
localStorage.setItem('ui', JSON.stringify(ui));
|
2017-05-29 16:29:49 +12:00
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case 'SUPPRESS_BROADCAST':
|
|
|
|
|
var ui = JSON.parse(localStorage.getItem('ui'))
|
|
|
|
|
if (!ui) ui = {}
|
|
|
|
|
|
|
|
|
|
var suppressed_broadcasts = (typeof(ui.suppressed_broadcasts) !== 'undefined' ? ui.suppressed_broadcasts : [])
|
|
|
|
|
suppressed_broadcasts.push(action.key)
|
|
|
|
|
|
|
|
|
|
Object.assign(
|
|
|
|
|
ui,
|
|
|
|
|
{ suppressed_broadcasts: suppressed_broadcasts }
|
|
|
|
|
);
|
2017-05-30 09:06:59 +12:00
|
|
|
localStorage.setItem('ui', JSON.stringify(ui))
|
2017-05-29 16:29:49 +12:00
|
|
|
break
|
2016-10-18 16:53:33 +13:00
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
export default localstorageMiddleware
|