Files
Iris/src/js/services/localstorage/middleware.js

191 lines
5.7 KiB
JavaScript
Raw Normal View History

2017-11-23 17:43:29 +13:00
var helpers = require('../../helpers.js')
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);
// 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
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)
}
}
2017-10-10 13:05:28 +13:00
switch(action.type){
2016-10-31 09:25:15 +13:00
case 'PUSHER_CONNECTED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'pusher',
{
2017-02-19 21:27:43 +13:00
connection_id: action.connection_id
2016-10-28 15:58:07 +13:00
}
);
break;
2016-10-31 09:25:15 +13:00
case 'PUSHER_SET_PORT':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'pusher',
{
port: action.port
}
);
break;
2017-02-19 21:27:43 +13:00
case 'PUSHER_USERNAME_CHANGED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'pusher',
{
username: action.username
}
);
2016-10-28 15:58:07 +13:00
break;
case 'MOPIDY_SET_CONFIG':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'mopidy',
{
host: action.config.host,
port: action.config.port
}
);
break;
case 'MOPIDY_URISCHEMES_FILTERED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'mopidy',
{
uri_schemes: action.data
}
);
break;
case 'SPOTIFY_SET_CONFIG':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'spotify',
{
authentication_provider: action.config.authentication_provider,
country: action.config.country,
locale: action.config.locale
}
);
break;
case 'SPOTIFY_IMPORT_AUTHORIZATION':
case 'SPOTIFY_AUTHORIZATION_GRANTED':
if (action.authorization !== undefined){
var authorization = action.authorization;
} else if (action.data){
var authorization = action.data;
}
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'spotify',
{
authorization: authorization,
access_token: authorization.access_token,
refresh_token: authorization.refresh_token,
token_expiry: authorization.token_expiry
}
);
break;
2016-10-28 15:58:07 +13:00
case 'SPOTIFY_AUTHORIZATION_REVOKED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'spotify',
{
authorization: false,
2016-10-28 15:58:07 +13:00
access_token: false,
refresh_token: false,
token_expiry: false
}
);
break;
case 'SPOTIFY_TOKEN_REFRESHED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'spotify',
{
access_token: action.data.access_token,
token_expiry: action.data.token_expiry,
provider: action.provider
2016-10-19 14:09:34 +13:00
}
);
break;
2016-10-28 15:58:07 +13:00
case 'SPOTIFY_ME_LOADED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'spotify',
{
me: action.data
}
2016-10-28 15:58:07 +13:00
);
break;
2016-11-21 16:26:47 +13:00
case 'CORE_SET':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'core',
action.data
);
break
2016-12-09 18:47:10 +13:00
case 'UI_SET':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'ui',
action.data
);
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 }
);
localStorage.setItem('ui', JSON.stringify(ui))
2017-05-29 16:29:49 +12:00
break
case 'LASTFM_AUTHORIZATION_GRANTED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'lastfm',
{
session: action.data.session
}
);
break;
2017-11-06 16:59:21 +13:00
case 'LASTFM_AUTHORIZATION_REVOKED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'lastfm',
2017-11-06 16:59:21 +13:00
{
session: null
}
);
break;
}
}
})();
export default localstorageMiddleware