From 64e5472e2c008d48986d07a81299f761610b67de Mon Sep 17 00:00:00 2001 From: James Barnsley Date: Fri, 28 Oct 2016 10:48:55 +1300 Subject: [PATCH] Receiving connection updates --- src/js/helpers.js | 12 ++++++++++++ src/js/services/pusher/middleware.js | 5 +++++ src/js/services/pusher/reducer.js | 12 ++++++++++-- src/js/views/Settings.js | 13 +------------ 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/js/helpers.js b/src/js/helpers.js index 3910d919..867b5e63 100755 --- a/src/js/helpers.js +++ b/src/js/helpers.js @@ -36,4 +36,16 @@ export let generateGuid = function(){ var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); +} + + +export let getCurrentPusherConnection = function( connections, connectionid ){ + function isCurrentConnection(connection){ + return connection.connectionid == newProps.pusher.connectionid; + } + + var currentConnection = newProps.pusher.connections.find(isCurrentConnection); + if( !currentConnection ) return false; + + return currentConnection; } \ No newline at end of file diff --git a/src/js/services/pusher/middleware.js b/src/js/services/pusher/middleware.js index c22cb33e..79e60b6e 100755 --- a/src/js/services/pusher/middleware.js +++ b/src/js/services/pusher/middleware.js @@ -68,6 +68,11 @@ const PusherMiddleware = (function(){ return next(action); break; + case 'PUSHER_CLIENT_CONNECTED': + makeRequest({ action: 'get_connections' }); + return next(action); + break; + case 'PUSHER_INSTRUCT': makeRequest({ action: action.action, data: action.data }); break; diff --git a/src/js/services/pusher/reducer.js b/src/js/services/pusher/reducer.js index 9f2f8628..9aa478d1 100755 --- a/src/js/services/pusher/reducer.js +++ b/src/js/services/pusher/reducer.js @@ -14,8 +14,16 @@ export default function reducer(pusher = {}, action){ port: action.port }); - case 'PUSHER_CLIENT_CONNECTED': - return Object.assign({}, pusher, { connection: action.data }); + case 'PUSHER_CONNECTION_UPDATED': + function byID(connection){ + return connection.connectionid == action.data.connections.connectionid; + } + var connection = pusher.connections.find(byID); + var index = pusher.connections.indexOf(connection); + var connections = Object.assign([], pusher.connections); + connections[index] = action.data.connections; + + return Object.assign({}, pusher, { connections: connections }); case 'PUSHER_CONNECTIONS': return Object.assign({}, pusher, { connections: action.data.connections }); diff --git a/src/js/views/Settings.js b/src/js/views/Settings.js index 826f9a22..278cbad2 100755 --- a/src/js/views/Settings.js +++ b/src/js/views/Settings.js @@ -20,24 +20,13 @@ class Settings extends React.Component{ this.state = { mopidy_host: this.props.mopidy.host, mopidy_port: this.props.mopidy.port, - pusher_username: '', + pusher_username: this.props.pusher.username, pusher_port: this.props.pusher.port, spotify_country: this.props.spotify.country, spotify_locale: this.props.spotify.locale }; } - componentWillReceiveProps( newProps ){ - if( this.props.pusher.connectionid != newProps.pusher.connectionid ){ - - function isCurrentConnection(connection){ - return connection.connectionid == newProps.pusher.connectionid; - } - var currentConnection = newProps.pusher.connections.find(isCurrentConnection); - this.setState({ pusher_username: currentConnection.username }) - } - } - resetAllSettings(){ localStorage.clear(); window.location.reload(true);