Skip to content

Commit

Permalink
Fix refresh logic to be based on 'nearbyItems' not 'items'
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonpage committed Nov 16, 2016
1 parent e017ca0 commit 6ae2815
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
13 changes: 9 additions & 4 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,10 @@ class App extends React.Component {
*/
onRefresh() {
debug('on refresh');
const { dispatch } = this.props;
track.pullRefresh();
this.stopScanning();
this.props.clearItems();
dispatch(actions.refreshItems());
this.startScanning();
}

Expand Down Expand Up @@ -378,7 +379,7 @@ App.propTypes = {
itemFound: React.PropTypes.func,
itemLost: React.PropTypes.func,
itemOpened: React.PropTypes.func,
clearItems: React.PropTypes.func,
dispatch: React.PropTypes.func,

// state
indicateScanning: React.PropTypes.bool,
Expand Down Expand Up @@ -416,14 +417,18 @@ function mapStateToProps(store) {
* @return {Object}
*/
function mapDispatchToProps(dispatch) {
return bindActionCreators({
const bound = bindActionCreators({
itemFound: actions.itemFound,
itemLost: actions.itemLost,
itemOpened: actions.itemOpened,
clearItems: actions.clearItems,
setUserFlag: actions.setUserFlag,
setIndicateScanning: actions.setIndicateScanning,
}, dispatch);

return {
...bound,
dispatch,
};
}

/**
Expand Down
1 change: 0 additions & 1 deletion lib/scanner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
const Emitter = require('events').EventEmitter;
const debug = require('../debug')('Scanner');
const ReactNative = require('react-native');
const track = require('../utils/tracker');
const config = require('../../config');

const {
Expand Down
21 changes: 19 additions & 2 deletions lib/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,30 @@ const {

const actions = {
itemFound(id, value) {
return (dispatch, getState) => {
const exists = getState().itemsNearby.indexOf(id) > -1;
if (!exists) dispatch(actions.newItemFound(id, value));
else dispatch(actions.itemUpdate(id, value));
};
},

newItemFound(id, value) {
track.itemFound(id);
return {
type: 'ITEM_FOUND',
id,
value,
};
},

itemUpdate(id, value) {
return {
type: 'ITEM_UPDATED',
id,
value,
};
},

itemLost(id) {
return {
type: 'ITEM_LOST',
Expand Down Expand Up @@ -102,9 +119,9 @@ const actions = {
};
},

clearItems() {
refreshItems() {
return {
type: 'CLEAR_ITEMS',
type: 'REFRESH_ITEMS',
};
},

Expand Down
19 changes: 13 additions & 6 deletions lib/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = (state, action) => {

switch (action.type) {
case 'ITEM_FOUND': return itemFound(state, action.id, action.value);
case 'ITEM_UPDATED': return itemUpdated(state, action.id, action.value);
case 'ITEM_LOST': return itemLost(state, action.id);
case 'ITEM_OPENED': return itemOpened(state, action.id);
case 'ITEM_CLOSED': return itemClosed(state, action.id);
Expand Down Expand Up @@ -130,16 +131,13 @@ function itemFetchError(state, id, value) {
function itemFound(state, id, { distance }) {
debug('item found', id);
const items = state.items;
const previous = findItem(items, id);

// just update the distance if item already exists
if (previous) return itemDistanceUpdated(state, id, distance);
const item = items[id] || createItem(id, { distance });

return {
...state,
items: {
...items,
[id]: createItem(id, { distance }),
[id]: { ...item },
},

itemsNearby: [
Expand All @@ -149,11 +147,13 @@ function itemFound(state, id, { distance }) {
};
}

function itemDistanceUpdated(state, id, { distance }) {
function itemUpdated(state, id, { distance }) {
debug('item found', id);
const previous = findItem(state.items, id);
const items = state.items;

if (!previous) return state;

const item = {
...previous,
value: {
Expand Down Expand Up @@ -203,6 +203,13 @@ function itemClosed(state) {
};
}

function refreshItems(state) {
return {
...state,
itemsNearby: [],
};
}

function setUserFlag(state, key, value) {
debug('set user flag', key, value);
var previous = state.userFlags[key];
Expand Down

0 comments on commit 6ae2815

Please sign in to comment.