diff --git a/internal_packages/search-bar/lib/search-bar.cjsx b/internal_packages/search-bar/lib/search-bar.cjsx index 3b3e7e356..8db746792 100644 --- a/internal_packages/search-bar/lib/search-bar.cjsx +++ b/internal_packages/search-bar/lib/search-bar.cjsx @@ -1,6 +1,6 @@ React = require 'react/addons' classNames = require 'classnames' -{Actions} = require 'nylas-exports' +{Actions, WorkspaceStore} = require 'nylas-exports' {Menu, RetinaImg} = require 'nylas-component-kit' SearchSuggestionStore = require './search-suggestion-store' _ = require 'underscore' @@ -16,7 +16,10 @@ class SearchBar extends React.Component committedQuery: null componentDidMount: => - @unsubscribe = SearchSuggestionStore.listen @_onChange + @usub = [] + @usub.push SearchSuggestionStore.listen @_onChange + @usub.push WorkspaceStore.listen => + @setState(focused: false) if @state.focused @body_unsubscriber = atom.commands.add 'body', { 'application:focus-search': @_onFocusSearch } @@ -28,7 +31,7 @@ class SearchBar extends React.Component # atom events before it unmounts. Thank you event-kit # This can be fixed via a Reflux mixin componentWillUnmount: => - @unsubscribe() + usub() for usub in @usub @body_unsubscriber.dispose() @search_unsubscriber.dispose() diff --git a/spec-nylas/stores/focused-mail-view-store-spec.coffee b/spec-nylas/stores/focused-mail-view-store-spec.coffee index 3d8dedfbf..2654b35d3 100644 --- a/spec-nylas/stores/focused-mail-view-store-spec.coffee +++ b/spec-nylas/stores/focused-mail-view-store-spec.coffee @@ -1,5 +1,6 @@ _ = require 'underscore' +Actions = require '../../src/flux/actions' Label = require '../../src/flux/models/label' Folder = require '../../src/flux/models/folder' MailViewFilter = require '../../src/mail-view-filter' @@ -58,6 +59,12 @@ describe "FocusedMailViewStore", -> FocusedMailViewStore._onFocusMailView(@inboxFilter) expect(FocusedMailViewStore._setMailView).not.toHaveBeenCalled() + it "should clear existing searches if any other category is focused", -> + spyOn(Actions, 'searchQueryCommitted') + FocusedMailViewStore._onSearchQueryCommitted('bla') + FocusedMailViewStore._onFocusMailView(@userFilter) + expect(Actions.searchQueryCommitted).toHaveBeenCalledWith('') + describe 'when using labels', -> beforeEach -> atom.testOrganizationUnit = 'label' diff --git a/src/flux/stores/focused-mail-view-store.coffee b/src/flux/stores/focused-mail-view-store.coffee index 3f06ba4f6..a8cb1b934 100644 --- a/src/flux/stores/focused-mail-view-store.coffee +++ b/src/flux/stores/focused-mail-view-store.coffee @@ -20,8 +20,7 @@ class FocusedMailViewStore extends NylasStore _onFocusMailView: (filter) -> return if filter.isEqual(@_mailView) - if @_mailView is null and filter - Actions.searchQueryCommitted('') + Actions.searchQueryCommitted('') @_setMailView(filter) _onSearchQueryCommitted: (query="") -> diff --git a/src/flux/stores/search-view.coffee b/src/flux/stores/search-view.coffee index 550140ac8..ebde82ba2 100644 --- a/src/flux/stores/search-view.coffee +++ b/src/flux/stores/search-view.coffee @@ -60,25 +60,27 @@ class SearchView extends ModelView accountId: @_accountId json: true returnsModel: false - error: => - page.loading = false - @_emitter.emit('trigger') - success: (json) => - objects = [] + .then (json) => + objects = [] - @_queryResultTotal = json.length + @_queryResultTotal = json.length - for resultJSON in json - obj = (new Thread).fromJSON(resultJSON) - objects.push(obj) + for resultJSON in json + obj = (new Thread).fromJSON(resultJSON) + objects.push(obj) - DatabaseStore.persistModels(objects) if objects.length > 0 + DatabaseStore.persistModels(objects) if objects.length > 0 - page.items = objects - page.loading = false - @_emitter.emit('trigger') + page.items = objects + page.loading = false + @_emitter.emit('trigger') - console.log("Search view fetched #{idx} in #{Date.now() - start} msec.") + console.log("Search view fetched #{idx} in #{Date.now() - start} msec.") + .catch (error) => + @_queryResultTotal = 0 + page.items = [] + page.loading = false + @_emitter.emit('trigger') module.exports = SearchView