Mailspring/internal_packages/sidebar-inbox-internal/lib/internal-admin-store.coffee
Robert McQueen 6e94f38ada Adding ATag component which prevents <a> tags from taking over edgehill when clicked.
Summary: Applying the new ATag to the admin sidebar per the bug T2075

Test Plan: Tested locally. I'm unsure how to test how an ATag can open a browser window, so I forego'd a unit test

Reviewers: bengotow

Reviewed By: bengotow

Maniphest Tasks: T2075

Differential Revision: https://phab.nylas.com/D1686
2015-06-25 10:28:31 -07:00

88 lines
2.7 KiB
CoffeeScript

_ = require 'underscore'
Reflux = require 'reflux'
request = require 'request'
{FocusedContactsStore,
NamespaceStore,
PriorityUICoordinator} = require 'nylas-exports'
module.exports =
# The InternalAdminStore manages the data that backs the admin sidebar and emits
# a "trigger" event that the view listens to.
#
# If the Admin sidebar allowed you to take actions, like modifying someone's
# Nylas account, the InternalAdminStore would also listen for those user actions
# and perform business logic.
#
InternalAdminStore = Reflux.createStore
init: ->
@_accountCache = null
@_enabled = false
@_error = null
# Stores often listen to other stores to vend correct data to their views.
# Since we serve information about a contact we listen for changes to the
# focused contact. Since we only want to be enabled for @nylas.com emails,
# we listen for changes to available Namespaces.
@listenTo FocusedContactsStore, @_onFocusedContacts
@listenTo NamespaceStore, @_onNamespaceChanged
@_onNamespaceChanged()
dataForFocusedContact: ->
return {loading: true} if @_accountCache is null
contact = FocusedContactsStore.focusedContact()
return {} unless contact
account = _.find @_accountCache, (account) -> account.email is contact.email
apps = undefined
apps = account.applications if account
# Coffeescript shorthand for {account: account, apps: apps}
{account, apps}
enabled: ->
@_enabled
error: ->
@_error
_onFocusedContacts: ->
# When the user focuses on a contact, we don't need to do any work because we
# cache everything. Just trigger so that our view updates and calls
# `dataForFocusedContact`.
@trigger(@)
_onNamespaceChanged: ->
clearInterval(@_fetchInterval) if @_fetchInterval
@_fetchInterval = null
# We only want to enable this package for users with nylas.com email addresses.
n = NamespaceStore.current()
if n and n.emailAddress.indexOf('@nylas.com') > 0
@_fetchInterval = setInterval(( => @_fetchAPIData()), 5 * 60 * 1000)
@_fetchAPIData()
@_enabled = true
else
@_accountCache = null
@_enabled = false
@trigger(@)
_fetchAPIData: ->
# Make a HTTP request to the Admin service using the `request` library. Using
# the priority UI coordinator ensures that the expensive JSON.parse operation
# doesn't happen while an animation is running.
request 'https://admin.nylas.com/api/status/accounts', (err, resp, data) =>
PriorityUICoordinator.settle.then =>
if err
@_error = err
else
@_error = null
try
@_accountCache = JSON.parse(data)
catch err
@_error = err
@_accountCache = null
@trigger(@)