From 6ae28151cfb0ae5078ecf4528cd13fc78b1f12de Mon Sep 17 00:00:00 2001 From: Wilson Page Date: Tue, 15 Nov 2016 16:49:21 +0000 Subject: [PATCH] Fix refresh logic to be based on 'nearbyItems' not 'items' --- lib/app.js | 13 +++++++++---- lib/scanner/index.js | 1 - lib/store/actions.js | 21 +++++++++++++++++++-- lib/store/reducer.js | 19 +++++++++++++------ 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/app.js b/lib/app.js index 8299fdd2..282b0dba 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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(); } @@ -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, @@ -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, + }; } /** diff --git a/lib/scanner/index.js b/lib/scanner/index.js index 8243b491..2d9dd61d 100644 --- a/lib/scanner/index.js +++ b/lib/scanner/index.js @@ -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 { diff --git a/lib/store/actions.js b/lib/store/actions.js index 477db97a..e5cc8c71 100644 --- a/lib/store/actions.js +++ b/lib/store/actions.js @@ -25,6 +25,15 @@ 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, @@ -32,6 +41,14 @@ const actions = { }; }, + itemUpdate(id, value) { + return { + type: 'ITEM_UPDATED', + id, + value, + }; + }, + itemLost(id) { return { type: 'ITEM_LOST', @@ -102,9 +119,9 @@ const actions = { }; }, - clearItems() { + refreshItems() { return { - type: 'CLEAR_ITEMS', + type: 'REFRESH_ITEMS', }; }, diff --git a/lib/store/reducer.js b/lib/store/reducer.js index bb917048..a31ff56c 100644 --- a/lib/store/reducer.js +++ b/lib/store/reducer.js @@ -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); @@ -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: [ @@ -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: { @@ -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];