Immediately blow up if the user passes incorrect query params

Summary: If state.selectedId is null, or otherwise cannot find item, don't throw exception

Test Plan: Run tests

Reviewers: evan

Reviewed By: evan

Differential Revision: https://review.inboxapp.com/D1373
This commit is contained in:
Ben Gotow 2015-03-30 17:25:08 -07:00
parent f19f178e3c
commit 6ebe1118eb
3 changed files with 17 additions and 4 deletions

View file

@ -88,6 +88,8 @@ DraftList = React.createClass
_onShiftSelectedIndex: (delta) ->
item = _.find @state.items, (draft) => draft.id is @state.selectedId
return unless item
index = if item then @state.items.indexOf(item) else -1
index = Math.max(0, Math.min(index + delta, @state.items.length-1))
@setState
@ -95,6 +97,7 @@ DraftList = React.createClass
_onDeleteSelected: ->
item = _.find @state.items, (draft) => draft.id is @state.selectedId
return unless item
DatabaseStore.localIdForModel(item).then (localId) ->
Actions.destroyDraft(localId)

View file

@ -28,7 +28,7 @@ Mixin =
@setState getViewsByName(@components)
componentWillUnmount: ->
@_componentUnlistener()
@_componentUnlistener() if @_componentUnlistener
# Internal representation of components
class Component

View file

@ -288,16 +288,20 @@ DatabaseStore = Reflux.createStore
# ActiveRecord-style Querying
find: (klass, id) ->
throw (new Error "find takes a string Id. You may have intended to use findBy.") unless _.isString(id)
throw new Error("You must provide a class to findByLocalId") unless klass
throw new Error("find takes a string id. You may have intended to use findBy.") unless _.isString(id)
new ModelQuery(klass, @).where({id:id}).one()
findBy: (klass, predicates = []) ->
throw new Error("You must provide a class to findBy") unless klass
new ModelQuery(klass, @).where(predicates).one()
findAll: (klass, predicates = []) ->
throw new Error("You must provide a class to findAll") unless klass
new ModelQuery(klass, @).where(predicates)
count: (klass, predicates = []) ->
throw new Error("You must provide a class to count") unless klass
new ModelQuery(klass, @).where(predicates).count()
# Support for Local IDs
@ -306,12 +310,17 @@ DatabaseStore = Reflux.createStore
# (like body, stored in a separate table) are always included.
#
findByLocalId: (klass, localId) ->
return Promise.reject(new Error("You must provide a class to findByLocalId")) unless klass
return Promise.reject(new Error("You must provide a local Id to findByLocalId")) unless localId
new Promise (resolve, reject) =>
@find(LocalLink, localId).then (link) =>
return reject("Find by local ID lookup failed") unless link
query = @find(klass, link.objectId).includeAll().then(resolve)
bindToLocalId: (model, localId) ->
return Promise.reject(new Error("You must provide a model to bindToLocalId")) unless model
new Promise (resolve, reject) =>
unless localId
if isTempId(model.id)
@ -325,10 +334,11 @@ DatabaseStore = Reflux.createStore
.catch(reject)
localIdForModel: (model) ->
return Promise.reject(new Error("You must provide a model to localIdForModel")) unless model
new Promise (resolve, reject) =>
if @_localIdLookupCache[model.id]
resolve(@_localIdLookupCache[model.id])
return
return resolve(@_localIdLookupCache[model.id])
@findBy(LocalLink, {objectId: model.id}).then (link) =>
if link