Skip to content

Commit

Permalink
Moved network status logic into redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonpage committed Nov 16, 2016
1 parent d48b076 commit e017ca0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const SubscriptionsScene = require('./views/subscriptions-scene');
const SettingsScene = require('./views/settings-scene');
const { HEALTHY } = require('./store/constants');
const notification = require('./notifications');
const ItemScene = require('./views/item/scene');
const ListScene = require('./views/list/scene');
Expand Down Expand Up @@ -76,9 +77,6 @@ class App extends React.Component {
this.setupNotificationListeners();
});

// when network is bad, the scanner errors
this.scanner.on('networkerror', this.onScannerNetworkError.bind(this));

// respond to app background/foreground changes
AppState.addEventListener('change', this.onAppStateChanged.bind(this));

Expand All @@ -104,6 +102,7 @@ class App extends React.Component {
componentWillReceiveProps(next) {
var current = this.props;
if (next.userFlags !== current.userFlags) this.onUserFlagsChange(next.userFlags);
if (next.network !== current.network) this.onNetworkStatusChange(next.network);
}

render() {
Expand Down Expand Up @@ -304,8 +303,9 @@ class App extends React.Component {
}
}

onScannerNetworkError() {
onNetworkStatusChange(network) {
debug('on network error');
if (network.status === HEALTHY) return;
if (this.networkAlertOpen) return;
this.networkAlertOpen = true;

Expand Down Expand Up @@ -403,6 +403,7 @@ function mapStateToProps(store) {
return {
indicateScanning: store.indicateScanning,
userFlags: store.userFlags,
network: store.network,
items: store.items,
};
}
Expand Down
16 changes: 15 additions & 1 deletion lib/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

const { bindActionCreators } = require('redux');
const fetchItem = require('../api/fetch-item');
const { isNetworkError } = require('../utils');
const debug = require('../debug')('actions');
const track = require('../utils/tracker');
const { dispatch } = require('.');
const api = require('../api');

const {
INCOMPLETE,
COMPLETE,
ERROR,
HEALTHY,
} = require('./constants');

/**
Expand Down Expand Up @@ -59,12 +62,13 @@ const actions = {

return fetchItem(id)
.then(item => {
dispatch(actions.networkStatusUpdate(HEALTHY));
dispatch(actions.itemFetched(id, item));
})

.catch(e => {
console.log('item resolve errored', e);
dispatch(actions.itemFetchErrored(id, e));
if (isNetworkError(e)) dispatch(actions.networkStatusUpdate(ERROR));
});
};
},
Expand Down Expand Up @@ -211,6 +215,16 @@ const actions = {
subscription,
};
},

networkStatusUpdate(value) {
return {
type: 'NETWORK_STATUS_UPDATE',
value: {
status: value,
timestamp: Date.now(),
},
};
},
};

function shouldFetchItem(state, id) {
Expand Down
1 change: 1 addition & 0 deletions lib/store/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
INCOMPLETE: 'incomplete',
COMPLETE: 'complete',
ERROR: 'error',
HEALTHY: 'healthy',
};
16 changes: 16 additions & 0 deletions lib/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ const {
INCOMPLETE,
COMPLETE,
ERROR,
HEALTHY,
} = require('./constants');

const initialState = {
items: {},
itemsNearby: [],
openItem: null,
network: {
status: HEALTHY,
timestamp: Date.now(),
},

indicateScanning: undefined,
userFlags: config.userFlags,
Expand Down Expand Up @@ -48,6 +53,7 @@ module.exports = (state, action) => {

case 'SET_USER_FLAG': return setUserFlag(state, action.key, action.value);
case 'SET_INDICATE_SCANNING': return { ...state, indicateScanning: action.value };
case 'NETWORK_STATUS_UPDATE': return networkStatusUpdate(state, action.value);

case 'CHANNELS_FETCHING': return channelsFetching(state);
case 'CHANNELS_FETCHED': return channelsFetched(state, action.value);
Expand Down Expand Up @@ -349,6 +355,16 @@ function subscriptionUpdated(state, { subscription }) {
};
}

function networkStatusUpdate(state, { status, timestamp }) {
return {
...state,
network: {
status,
timestamp,
},
};
}

/**
* Utils
*/
Expand Down

0 comments on commit e017ca0

Please sign in to comment.