Files
Iris/src/js/bootstrap.js

100 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-10-03 15:22:15 +13:00
import { createStore, applyMiddleware, combineReducers } from 'redux'
2016-10-27 10:02:12 +13:00
import ui from './services/ui/reducer'
2016-10-27 16:58:11 +13:00
import pusher from './services/pusher/reducer'
import mopidy from './services/mopidy/reducer'
2016-11-07 21:21:15 +13:00
import lastfm from './services/lastfm/reducer'
import spotify from './services/spotify/reducer'
import thunk from 'redux-thunk'
2016-11-11 16:47:32 +13:00
import uiMiddleware from './services/ui/middleware'
2016-10-27 16:58:11 +13:00
import pusherMiddleware from './services/pusher/middleware'
import mopidyMiddleware from './services/mopidy/middleware'
import spotifyMiddleware from './services/spotify/middleware'
import localstorageMiddleware from './services/localstorage/middleware'
let reducers = combineReducers({
2016-10-27 10:02:12 +13:00
ui,
2016-10-27 16:58:11 +13:00
pusher,
mopidy,
2016-11-07 21:21:15 +13:00
lastfm,
spotify
});
2016-10-03 15:22:15 +13:00
2016-10-18 08:56:53 +13:00
// set application defaults
// TODO: Look at using propTypes in the component for these falsy initial states
2016-10-18 08:56:53 +13:00
var initialState = {
mopidy: {
connected: false,
2016-10-18 08:56:53 +13:00
host: window.location.hostname,
port: (window.location.port ? window.location.port : (window.location.protocol === 'https:' ? '443' : '80')),
2017-05-19 11:33:06 +12:00
mute: false,
volume: 0,
progress: 0,
play_state: false
},
2016-10-27 16:58:11 +13:00
pusher: {
connected: false,
2017-01-04 15:24:13 +13:00
username: 'Anonymous',
2017-02-19 21:27:43 +13:00
connections: {},
version: {
current: '0.0.0'
}
2016-10-27 16:58:11 +13:00
},
2016-11-07 21:21:15 +13:00
lastfm: {
album: {},
artist: {},
track: {}
},
spotify: {
connected: false,
2017-05-12 05:22:57 +12:00
me: false,
autocomplete_results: {}
2016-10-27 10:02:12 +13:00
},
ui: {
slim_mode: false,
current_tracklist: [],
2016-11-09 15:49:25 +13:00
current_tltrack: false,
2017-07-21 22:56:28 +12:00
selected_tracks: [],
2017-06-12 18:04:00 +12:00
notifications: [],
config: {
authorization_url: 'https://jamesbarnsley.co.nz/auth_v2.php'
2017-06-12 18:04:00 +12:00
}
2016-10-18 08:56:53 +13:00
}
};
// if we've got a stored version of mopidy state, load and merge
if( localStorage.getItem('mopidy') ){
var storedMopidy = JSON.parse( localStorage.getItem('mopidy') );
2016-10-19 14:09:34 +13:00
initialState.mopidy = Object.assign(initialState.mopidy, storedMopidy );
}
2016-10-27 16:58:11 +13:00
// if we've got a stored version of pusher state, load and merge
if( localStorage.getItem('pusher') ){
var storedPusher = JSON.parse( localStorage.getItem('pusher') );
initialState.pusher = Object.assign(initialState.pusher, storedPusher );
}
// if we've got a stored version of spotify state, load and merge
if( localStorage.getItem('spotify') ){
var storedSpotify = JSON.parse( localStorage.getItem('spotify') );
2016-10-19 14:09:34 +13:00
initialState.spotify = Object.assign(initialState.spotify, storedSpotify );
}
2016-11-21 16:26:47 +13:00
// if we've got a stored version of spotify state, load and merge
if( localStorage.getItem('ui') ){
var storedUi = JSON.parse( localStorage.getItem('ui') );
initialState.ui = Object.assign(initialState.ui, storedUi );
}
console.log('Bootstrapping', initialState)
let store = createStore(
reducers,
initialState,
2016-11-11 16:47:32 +13:00
applyMiddleware( thunk, localstorageMiddleware, uiMiddleware, mopidyMiddleware, pusherMiddleware, spotifyMiddleware )
);
2016-10-03 15:22:15 +13:00
export default store;