React = require 'react'
_ = require 'underscore'
{AccountStore, DatabaseStore, EdgehillAPI} = require 'nylas-exports'
{RetinaImg, Flexbox} = require 'nylas-component-kit'
class PreferencesAccounts extends React.Component
@displayName: 'PreferencesAccounts'
@propTypes:
config: React.PropTypes.object.isRequired
constructor: (@props) ->
@state = @getStateFromStores()
componentDidMount: =>
@unsubscribe = AccountStore.listen @_onAccountChange
componentWillUnmount: =>
@unsubscribe?()
render: =>
{@_renderAccounts()}
{@_renderLinkedAccounts()}
_renderAccounts: =>
return false unless @state.accounts
allowUnlinking = @state.accounts.length > 1
Accounts:
{ @state.accounts.map (account) =>
{account.emailAddress}
{account.name || "No name provided."} ({account.displayProvider()})
}
_renderLinkedAccounts: =>
tokens = @getSecondaryTokens()
return false unless tokens.length > 0
Linked Accounts:
{ tokens.map (token) =>
{@_renderLinkedAccount(token)}
}
_renderLinkedAccount: (token) =>
getStateFromStores: =>
accounts: AccountStore.items()
getSecondaryTokens: =>
return [] unless @props.config
tokens = @props.config.get('tokens') || []
tokens = tokens.filter (token) -> token.provider isnt 'nylas'
tokens
_onAddAccount: =>
require('remote').getGlobal('application').windowManager.newOnboardingWindow()
_onAccountChange: =>
@setState(@getStateFromStores())
_onUnlinkAccount: (account) =>
return [] unless @props.config
tokens = @props.config.get('tokens') || []
token = _.find tokens, (token) ->
token.provider is 'nylas' and token.identifier is account.emailAddress
tokens = _.without(tokens, token)
DatabaseStore.unpersistModel(account).then =>
# TODO: Delete other mail data
EdgehillAPI.unlinkToken(token)
_onUnlinkToken: (token) =>
EdgehillAPI.unlinkToken(token)
return
_onLogout: =>
atom.logout()
module.exports = PreferencesAccounts