AccountStore no longer tracks index, Actions.selectAccount gone

This commit is contained in:
Ben Gotow 2016-01-11 14:11:25 -08:00
parent 69f3cc441a
commit 7b6f122119
6 changed files with 76 additions and 92 deletions

View file

@ -46,7 +46,6 @@ class AccountSidebarStore extends NylasStore
@listenTo WorkspaceStore, @_updateSections @listenTo WorkspaceStore, @_updateSections
@listenTo ThreadCountsStore, @_updateSections @listenTo ThreadCountsStore, @_updateSections
@listenTo FocusedPerspectiveStore, => @trigger() @listenTo FocusedPerspectiveStore, => @trigger()
@listenTo Actions.selectAccount, @_onSelectAccount
@configSubscription = NylasEnv.config.observe( @configSubscription = NylasEnv.config.observe(
'core.workspace.showUnreadForAllCategories', 'core.workspace.showUnreadForAllCategories',
@_updateSections @_updateSections
@ -60,10 +59,10 @@ class AccountSidebarStore extends NylasStore
Categories.user(@_currentAccount).subscribe(@_onUserCategoriesChanged) Categories.user(@_currentAccount).subscribe(@_onUserCategoriesChanged)
] ]
_onSelectAccount: (accountId)=> # _onSelectAccount: (accountId)=>
@_account = AccountStore.accountForId(accountId) # @_account = AccountStore.accountForId(accountId)
@_registerObservables() # @_registerObservables()
@trigger() # @trigger()
_onStandardCategoriesChanged: (categories) -> _onStandardCategoriesChanged: (categories) ->
@_standardCategories = categories @_standardCategories = categories

View file

@ -118,7 +118,7 @@ class AccountSwitcher extends React.Component
@setState(showing: false) @setState(showing: false)
_onSwitchAccount: (account) => _onSwitchAccount: (account) =>
Actions.selectAccount(account.id) Actions.focusDefaultMailboxPerspectiveForAccount(account.id)
@setState(showing: false) @setState(showing: false)
_onManageAccounts: => _onManageAccounts: =>

View file

@ -1,6 +1,7 @@
_ = require 'underscore' _ = require 'underscore'
AccountStore = require '../../src/flux/stores/account-store' AccountStore = require '../../src/flux/stores/account-store'
Account = require '../../src/flux/models/account' Account = require '../../src/flux/models/account'
Actions = require '../../src/flux/actions'
describe "AccountStore", -> describe "AccountStore", ->
beforeEach -> beforeEach ->
@ -29,29 +30,14 @@ describe "AccountStore", ->
}] }]
spyOn(NylasEnv.config, 'get').andCallFake (key) -> spyOn(NylasEnv.config, 'get').andCallFake (key) ->
if key is 'nylas.accounts' return accounts if key is 'nylas.accounts'
return accounts return null
else if key is 'nylas.currentAccountIndex'
return 1
@instance = new @constructor @instance = new @constructor
expect(@instance.items()).toEqual([ expect(@instance.accounts()).toEqual([
(new Account).fromJSON(accounts[0]), (new Account).fromJSON(accounts[0]),
(new Account).fromJSON(accounts[1]) (new Account).fromJSON(accounts[1])
]) ])
expect(@instance.current() instanceof Account).toBe(true)
expect(@instance.current().id).toEqual(accounts[1]['id'])
expect(@instance.current().emailAddress).toEqual(accounts[1]['email_address'])
it "should initialize current() to null if data is not present", ->
spyOn(NylasEnv.config, 'get').andCallFake -> null
@instance = new @constructor
expect(@instance.current()).toEqual(null)
it "should initialize current() to null if data is invalid", ->
spyOn(NylasEnv.config, 'get').andCallFake -> "this isn't an object"
@instance = new @constructor
expect(@instance.current()).toEqual(null)
describe "adding account from json", -> describe "adding account from json", ->
beforeEach -> beforeEach ->
@ -66,7 +52,7 @@ describe "AccountStore", ->
"auth_token": "auth-123" "auth_token": "auth-123"
"organization_unit": "label" "organization_unit": "label"
@instance = new @constructor @instance = new @constructor
spyOn(@instance, "_onSelectAccount").andCallThrough() spyOn(Actions, 'focusDefaultMailboxPerspectiveForAccount')
spyOn(@instance, "trigger") spyOn(@instance, "trigger")
@instance.addAccountFromJSON(@json) @instance.addAccountFromJSON(@json)
@ -80,12 +66,10 @@ describe "AccountStore", ->
it "saves the config", -> it "saves the config", ->
expect(NylasEnv.config.save).toHaveBeenCalled() expect(NylasEnv.config.save).toHaveBeenCalled()
expect(NylasEnv.config.set.calls.length).toBe 4 expect(NylasEnv.config.set.calls.length).toBe 2
it "selects the account", -> it "selects the account", ->
expect(@instance._index).toBe 0 expect(Actions.focusDefaultMailboxPerspectiveForAccount).toHaveBeenCalledWith("1234")
expect(@instance._onSelectAccount).toHaveBeenCalledWith("1234")
expect(@instance._onSelectAccount.calls.length).toBe 1
it "triggers", -> it "triggers", ->
expect(@instance.trigger).toHaveBeenCalled() expect(@instance.trigger).toHaveBeenCalled()

View file

@ -145,13 +145,6 @@ class Actions
### ###
@clearDeveloperConsole: ActionScopeWindow @clearDeveloperConsole: ActionScopeWindow
###
Public: Select the provided account ID in the current window.
*Scope: Window*
###
@selectAccount: ActionScopeWindow
### ###
Public: Remove the selected account Public: Remove the selected account
@ -218,7 +211,7 @@ class Actions
@setFocus: ActionScopeWindow @setFocus: ActionScopeWindow
### ###
Public: Focus the interface on a specific {Category}. Public: Focus the interface on a specific {MailboxPerspective}.
*Scope: Window* *Scope: Window*
@ -228,6 +221,14 @@ class Actions
### ###
@focusMailboxPerspective: ActionScopeWindow @focusMailboxPerspective: ActionScopeWindow
###
Public: Focus the interface on the default mailbox perspective for the provided
account id.
*Scope: Window*
###
@focusDefaultMailboxPerspectiveForAccount: ActionScopeWindow
### ###
Public: If the message with the provided id is currently beign displayed in the Public: If the message with the provided id is currently beign displayed in the
thread view, this action toggles whether it's full content or snippet is shown. thread view, this action toggles whether it's full content or snippet is shown.

View file

@ -10,7 +10,6 @@ CoffeeHelpers = require '../coffee-helpers'
saveObjectsKey = "nylas.accounts" saveObjectsKey = "nylas.accounts"
saveTokensKey = "nylas.accountTokens" saveTokensKey = "nylas.accountTokens"
saveIndexKey = "nylas.currentAccountIndex"
### ###
Public: The AccountStore listens to changes to the available accounts in Public: The AccountStore listens to changes to the available accounts in
@ -26,78 +25,33 @@ class AccountStore
constructor: -> constructor: ->
@_load() @_load()
@listenTo Actions.selectAccount, @_onSelectAccount
@listenTo Actions.removeAccount, @_onRemoveAccount @listenTo Actions.removeAccount, @_onRemoveAccount
@listenTo Actions.updateAccount, @_onUpdateAccount @listenTo Actions.updateAccount, @_onUpdateAccount
NylasEnv.config.observe saveTokensKey, (updatedTokens) => NylasEnv.config.observe saveTokensKey, (updatedTokens) =>
return if _.isEqual(updatedTokens, @_tokens) return if _.isEqual(updatedTokens, @_tokens)
newAccountIds = _.keys(_.omit(updatedTokens, _.keys(@_tokens))) newAccountIds = _.keys(_.omit(updatedTokens, _.keys(@_tokens)))
@_load() @_load()
if newAccountIds.length > 0 if newAccountIds.length > 0
Actions.selectAccount(newAccountIds[0]) Actions.focusDefaultMailboxPerspectiveForAccount(newAccountIds[0])
if NylasEnv.isComposerWindow() if NylasEnv.isComposerWindow()
NylasEnv.config.observe saveObjectsKey, => @_load() NylasEnv.config.observe saveObjectsKey, => @_load()
@_setupFastAccountCommands()
_setupFastAccountCommands: ->
commands = {}
[0..8].forEach (index) =>
key = "application:select-account-#{index}"
commands[key] = _.partial(@_selectAccountByIndex, index)
NylasEnv.commands.add('body', commands)
_setupFastAccountMenu: ->
windowMenu = _.find NylasEnv.menu.template, ({label}) -> MenuHelpers.normalizeLabel(label) is 'Window'
return unless windowMenu
submenu = _.reject windowMenu.submenu, (item) -> item.account
return unless submenu
idx = _.findIndex submenu, ({type}) -> type is 'separator'
return unless idx > 0
accountMenuItems = @accounts().map (item, idx) =>
{
label: item.emailAddress,
command: "application:select-account-#{idx}",
account: true
}
submenu.splice(idx + 1, 0, accountMenuItems...)
windowMenu.submenu = submenu
NylasEnv.menu.update()
_selectAccountByIndex: (index) =>
require('electron').ipcRenderer.send('command', 'application:show-main-window')
index = Math.min(@_accounts.length - 1, Math.max(0, index))
Actions.selectAccount(@_accounts[index].id)
_load: => _load: =>
@_accounts = [] @_accounts = []
for json in NylasEnv.config.get(saveObjectsKey) || [] for json in NylasEnv.config.get(saveObjectsKey) || []
@_accounts.push((new Account).fromJSON(json)) @_accounts.push((new Account).fromJSON(json))
index = NylasEnv.config.get(saveIndexKey) || 0
@_index = Math.min(@_accounts.length - 1, Math.max(0, index))
@_tokens = NylasEnv.config.get(saveTokensKey) || {} @_tokens = NylasEnv.config.get(saveTokensKey) || {}
@_setupFastAccountMenu()
@trigger() @trigger()
_save: => _save: =>
NylasEnv.config.set(saveObjectsKey, @_accounts) NylasEnv.config.set(saveObjectsKey, @_accounts)
NylasEnv.config.set(saveIndexKey, @_index)
NylasEnv.config.set(saveTokensKey, @_tokens) NylasEnv.config.set(saveTokensKey, @_tokens)
NylasEnv.config.save() NylasEnv.config.save()
# Inbound Events # Inbound Events
_onSelectAccount: (id) =>
idx = _.findIndex @_accounts, (a) -> a.id is id
return if idx is -1 or @_index is idx
NylasEnv.config.set(saveIndexKey, idx)
@_index = idx
@trigger()
_onUpdateAccount: (id, updated) => _onUpdateAccount: (id, updated) =>
idx = _.findIndex @_accounts, (a) -> a.id is id idx = _.findIndex @_accounts, (a) -> a.id is id
account = @_accounts[idx] account = @_accounts[idx]
@ -119,8 +73,6 @@ class AccountStore
ipc = require('electron').ipcRenderer ipc = require('electron').ipcRenderer
ipc.send('command', 'application:reset-config-and-relaunch') ipc.send('command', 'application:reset-config-and-relaunch')
else else
if @_index is idx
Actions.selectAccount(@_accounts[0].id)
@trigger() @trigger()
addAccountFromJSON: (json) => addAccountFromJSON: (json) =>
@ -130,9 +82,13 @@ class AccountStore
throw new Error("Returned account data is invalid") throw new Error("Returned account data is invalid")
return if @_tokens[json.id] return if @_tokens[json.id]
@_tokens[json.id] = json.auth_token @_tokens[json.id] = json.auth_token
@_accounts.push((new Account).fromJSON(json))
account = (new Account).fromJSON(json)
@_accounts.push(account)
@_save() @_save()
@_onSelectAccount(json.id)
@trigger()
Actions.focusDefaultMailboxPerspectiveForAccount(account.id)
# Exposed Data # Exposed Data
@ -163,7 +119,6 @@ class AccountStore
# Public: Returns the currently active {Account}. # Public: Returns the currently active {Account}.
current: => current: =>
throw new Error("I can't haz the account") throw new Error("I can't haz the account")
@_accounts[@_index] || null
# Private: This method is going away soon, do not rely on it. # Private: This method is going away soon, do not rely on it.
# #
@ -245,7 +200,7 @@ class AccountStore
t.persistModels(threads) t.persistModels(threads)
]) ])
.then => .then =>
Actions.selectAccount account.id Actions.focusDefaultMailboxPerspectiveForAccount(account.id)
.then -> new Promise (resolve, reject) -> setTimeout(resolve, 1000) .then -> new Promise (resolve, reject) -> setTimeout(resolve, 1000)
module.exports = new AccountStore() module.exports = new AccountStore()

View file

@ -1,6 +1,8 @@
_ = require 'underscore'
NylasStore = require 'nylas-store' NylasStore = require 'nylas-store'
WorkspaceStore = require './workspace-store' WorkspaceStore = require './workspace-store'
AccountStore = require './account-store' AccountStore = require './account-store'
Account = require '../models/account'
MailboxPerspective = require '../../mailbox-perspective' MailboxPerspective = require '../../mailbox-perspective'
CategoryStore = require './category-store' CategoryStore = require './category-store'
Actions = require '../actions' Actions = require '../actions'
@ -8,11 +10,21 @@ Actions = require '../actions'
class FocusedPerspectiveStore extends NylasStore class FocusedPerspectiveStore extends NylasStore
constructor: -> constructor: ->
@listenTo CategoryStore, @_onCategoryStoreChanged @listenTo CategoryStore, @_onCategoryStoreChanged
@listenTo AccountStore, @_onAccountStoreChanged
@listenTo Actions.focusMailboxPerspective, @_onFocusMailView @listenTo Actions.focusMailboxPerspective, @_onFocusMailView
@listenTo Actions.focusDefaultMailboxPerspectiveForAccount, @_onFocusAccount
@listenTo Actions.searchQueryCommitted, @_onSearchQueryCommitted @listenTo Actions.searchQueryCommitted, @_onSearchQueryCommitted
@_onCategoryStoreChanged() @_onCategoryStoreChanged()
@_setupFastAccountCommands()
@_setupFastAccountMenu()
# Inbound Events # Inbound Events
_onAccountStoreChanged: ->
@_setupFastAccountMenu()
_onCategoryStoreChanged: -> _onCategoryStoreChanged: ->
if not @_current if not @_current
@_setPerspective(@_defaultPerspective()) @_setPerspective(@_defaultPerspective())
@ -29,6 +41,13 @@ class FocusedPerspectiveStore extends NylasStore
Actions.searchQueryCommitted('') Actions.searchQueryCommitted('')
@_setPerspective(filter) @_setPerspective(filter)
_onFocusAccount: (accountId) =>
account = AccountStore.accountForId(accountId) unless account instanceof Account
return unless account
category = CategoryStore.getStandardCategory(account, "inbox")
return unless category
@_setPerspective(MailboxPerspective.forCategory(account, category))
_onSearchQueryCommitted: (query="", account) -> _onSearchQueryCommitted: (query="", account) ->
if typeof(query) != "string" if typeof(query) != "string"
query = query[0].all query = query[0].all
@ -45,7 +64,7 @@ class FocusedPerspectiveStore extends NylasStore
_defaultPerspective: (account = AccountStore.accounts()[0])-> _defaultPerspective: (account = AccountStore.accounts()[0])->
category = CategoryStore.getStandardCategory(account, "inbox") category = CategoryStore.getStandardCategory(account, "inbox")
return null unless category return null unless category
MailboxPerspective.forCategory(account, category) return MailboxPerspective.forCategory(account, category)
# MailboxPerspective.unified() # MailboxPerspective.unified()
_setPerspective: (filter) -> _setPerspective: (filter) ->
@ -53,6 +72,32 @@ class FocusedPerspectiveStore extends NylasStore
@_current = filter @_current = filter
@trigger() @trigger()
_setupFastAccountCommands: ->
commands = {}
[0..8].forEach (index) =>
key = "application:select-account-#{index}"
commands[key] = => @_onFocusAccount(AccountStore.accounts()[index])
NylasEnv.commands.add('body', commands)
_setupFastAccountMenu: ->
windowMenu = _.find NylasEnv.menu.template, ({label}) -> MenuHelpers.normalizeLabel(label) is 'Window'
return unless windowMenu
submenu = _.reject windowMenu.submenu, (item) -> item.account
return unless submenu
idx = _.findIndex submenu, ({type}) -> type is 'separator'
return unless idx > 0
accountMenuItems = AccountStore.accounts().map (item, idx) =>
{
label: item.emailAddress,
command: "application:select-account-#{idx}",
account: true
}
submenu.splice(idx + 1, 0, accountMenuItems...)
windowMenu.submenu = submenu
NylasEnv.menu.update()
# Public Methods # Public Methods
current: -> @_current ? null current: -> @_current ? null