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

View file

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

View file

@ -1,6 +1,7 @@
_ = require 'underscore'
AccountStore = require '../../src/flux/stores/account-store'
Account = require '../../src/flux/models/account'
Actions = require '../../src/flux/actions'
describe "AccountStore", ->
beforeEach ->
@ -29,29 +30,14 @@ describe "AccountStore", ->
}]
spyOn(NylasEnv.config, 'get').andCallFake (key) ->
if key is 'nylas.accounts'
return accounts
else if key is 'nylas.currentAccountIndex'
return 1
return accounts if key is 'nylas.accounts'
return null
@instance = new @constructor
expect(@instance.items()).toEqual([
expect(@instance.accounts()).toEqual([
(new Account).fromJSON(accounts[0]),
(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", ->
beforeEach ->
@ -66,7 +52,7 @@ describe "AccountStore", ->
"auth_token": "auth-123"
"organization_unit": "label"
@instance = new @constructor
spyOn(@instance, "_onSelectAccount").andCallThrough()
spyOn(Actions, 'focusDefaultMailboxPerspectiveForAccount')
spyOn(@instance, "trigger")
@instance.addAccountFromJSON(@json)
@ -80,12 +66,10 @@ describe "AccountStore", ->
it "saves the config", ->
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", ->
expect(@instance._index).toBe 0
expect(@instance._onSelectAccount).toHaveBeenCalledWith("1234")
expect(@instance._onSelectAccount.calls.length).toBe 1
expect(Actions.focusDefaultMailboxPerspectiveForAccount).toHaveBeenCalledWith("1234")
it "triggers", ->
expect(@instance.trigger).toHaveBeenCalled()

View file

@ -145,13 +145,6 @@ class Actions
###
@clearDeveloperConsole: ActionScopeWindow
###
Public: Select the provided account ID in the current window.
*Scope: Window*
###
@selectAccount: ActionScopeWindow
###
Public: Remove the selected account
@ -218,7 +211,7 @@ class Actions
@setFocus: ActionScopeWindow
###
Public: Focus the interface on a specific {Category}.
Public: Focus the interface on a specific {MailboxPerspective}.
*Scope: Window*
@ -228,6 +221,14 @@ class Actions
###
@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
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"
saveTokensKey = "nylas.accountTokens"
saveIndexKey = "nylas.currentAccountIndex"
###
Public: The AccountStore listens to changes to the available accounts in
@ -26,78 +25,33 @@ class AccountStore
constructor: ->
@_load()
@listenTo Actions.selectAccount, @_onSelectAccount
@listenTo Actions.removeAccount, @_onRemoveAccount
@listenTo Actions.updateAccount, @_onUpdateAccount
NylasEnv.config.observe saveTokensKey, (updatedTokens) =>
return if _.isEqual(updatedTokens, @_tokens)
newAccountIds = _.keys(_.omit(updatedTokens, _.keys(@_tokens)))
@_load()
if newAccountIds.length > 0
Actions.selectAccount(newAccountIds[0])
Actions.focusDefaultMailboxPerspectiveForAccount(newAccountIds[0])
if NylasEnv.isComposerWindow()
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: =>
@_accounts = []
for json in NylasEnv.config.get(saveObjectsKey) || []
@_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) || {}
@_setupFastAccountMenu()
@trigger()
_save: =>
NylasEnv.config.set(saveObjectsKey, @_accounts)
NylasEnv.config.set(saveIndexKey, @_index)
NylasEnv.config.set(saveTokensKey, @_tokens)
NylasEnv.config.save()
# 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) =>
idx = _.findIndex @_accounts, (a) -> a.id is id
account = @_accounts[idx]
@ -119,8 +73,6 @@ class AccountStore
ipc = require('electron').ipcRenderer
ipc.send('command', 'application:reset-config-and-relaunch')
else
if @_index is idx
Actions.selectAccount(@_accounts[0].id)
@trigger()
addAccountFromJSON: (json) =>
@ -130,9 +82,13 @@ class AccountStore
throw new Error("Returned account data is invalid")
return if @_tokens[json.id]
@_tokens[json.id] = json.auth_token
@_accounts.push((new Account).fromJSON(json))
account = (new Account).fromJSON(json)
@_accounts.push(account)
@_save()
@_onSelectAccount(json.id)
@trigger()
Actions.focusDefaultMailboxPerspectiveForAccount(account.id)
# Exposed Data
@ -163,7 +119,6 @@ class AccountStore
# Public: Returns the currently active {Account}.
current: =>
throw new Error("I can't haz the account")
@_accounts[@_index] || null
# Private: This method is going away soon, do not rely on it.
#
@ -245,7 +200,7 @@ class AccountStore
t.persistModels(threads)
])
.then =>
Actions.selectAccount account.id
Actions.focusDefaultMailboxPerspectiveForAccount(account.id)
.then -> new Promise (resolve, reject) -> setTimeout(resolve, 1000)
module.exports = new AccountStore()

View file

@ -1,6 +1,8 @@
_ = require 'underscore'
NylasStore = require 'nylas-store'
WorkspaceStore = require './workspace-store'
AccountStore = require './account-store'
Account = require '../models/account'
MailboxPerspective = require '../../mailbox-perspective'
CategoryStore = require './category-store'
Actions = require '../actions'
@ -8,11 +10,21 @@ Actions = require '../actions'
class FocusedPerspectiveStore extends NylasStore
constructor: ->
@listenTo CategoryStore, @_onCategoryStoreChanged
@listenTo AccountStore, @_onAccountStoreChanged
@listenTo Actions.focusMailboxPerspective, @_onFocusMailView
@listenTo Actions.focusDefaultMailboxPerspectiveForAccount, @_onFocusAccount
@listenTo Actions.searchQueryCommitted, @_onSearchQueryCommitted
@_onCategoryStoreChanged()
@_setupFastAccountCommands()
@_setupFastAccountMenu()
# Inbound Events
_onAccountStoreChanged: ->
@_setupFastAccountMenu()
_onCategoryStoreChanged: ->
if not @_current
@_setPerspective(@_defaultPerspective())
@ -29,6 +41,13 @@ class FocusedPerspectiveStore extends NylasStore
Actions.searchQueryCommitted('')
@_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) ->
if typeof(query) != "string"
query = query[0].all
@ -45,7 +64,7 @@ class FocusedPerspectiveStore extends NylasStore
_defaultPerspective: (account = AccountStore.accounts()[0])->
category = CategoryStore.getStandardCategory(account, "inbox")
return null unless category
MailboxPerspective.forCategory(account, category)
return MailboxPerspective.forCategory(account, category)
# MailboxPerspective.unified()
_setPerspective: (filter) ->
@ -53,6 +72,32 @@ class FocusedPerspectiveStore extends NylasStore
@_current = filter
@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
current: -> @_current ? null