2015-03-21 10:15:32 +08:00
|
|
|
_ = require 'underscore-plus'
|
|
|
|
Reflux = require 'reflux'
|
|
|
|
request = require 'request'
|
2015-03-22 02:59:47 +08:00
|
|
|
{FocusedContactsStore, NamespaceStore} = require 'inbox-exports'
|
2015-03-21 10:15:32 +08:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
FullContactStore = Reflux.createStore
|
|
|
|
|
|
|
|
init: ->
|
|
|
|
@_accountCache = null
|
|
|
|
@_applicationCache = null
|
2015-03-22 02:59:47 +08:00
|
|
|
@_enabled = false
|
|
|
|
@_error = null
|
|
|
|
|
2015-03-21 10:15:32 +08:00
|
|
|
@listenTo FocusedContactsStore, @_onFocusedContacts
|
2015-03-22 02:59:47 +08:00
|
|
|
@listenTo NamespaceStore, @_onNamespaceChanged
|
2015-03-21 10:15:32 +08:00
|
|
|
|
2015-03-22 02:59:47 +08:00
|
|
|
@_onNamespaceChanged()
|
2015-03-21 10:15:32 +08:00
|
|
|
|
|
|
|
dataForFocusedContact: ->
|
|
|
|
return {loading: true} if @_accountCache is null or @_applicationCache is null
|
|
|
|
contact = FocusedContactsStore.focusedContact()
|
2015-03-21 10:18:05 +08:00
|
|
|
return {} unless contact
|
2015-03-21 10:15:32 +08:00
|
|
|
account = _.find @_accountCache, (account) -> account.email is contact.email
|
|
|
|
apps = undefined
|
|
|
|
if account
|
|
|
|
apps = @_applicationCache.accounts["#{account.id}"]
|
|
|
|
{account, apps}
|
|
|
|
|
2015-03-22 02:59:47 +08:00
|
|
|
enabled: ->
|
|
|
|
@_enabled
|
|
|
|
|
|
|
|
error: ->
|
|
|
|
@_error
|
|
|
|
|
2015-03-21 10:15:32 +08:00
|
|
|
_onFocusedContacts: ->
|
|
|
|
@trigger(@)
|
|
|
|
|
2015-03-22 02:59:47 +08:00
|
|
|
_onNamespaceChanged: ->
|
|
|
|
clearInterval(@_fetchInterval) if @_fetchInterval
|
|
|
|
@_fetchInterval = null
|
|
|
|
|
|
|
|
n = NamespaceStore.current()
|
|
|
|
if n and n.emailAddress.indexOf('@nilas.com') > 0
|
|
|
|
@_fetchInterval = setInterval(( => @_fetchAPIData()), 5 * 60 * 1000)
|
|
|
|
@_fetchAPIData()
|
|
|
|
@_enabled = true
|
|
|
|
else
|
|
|
|
@_accountCache = null
|
|
|
|
@_applicationCache = null
|
|
|
|
@_enabled = false
|
|
|
|
@trigger(@)
|
|
|
|
|
2015-03-21 10:15:32 +08:00
|
|
|
_fetchAPIData: ->
|
|
|
|
console.log('Fetching Internal Admin Data')
|
|
|
|
# Swap the url's to see real data
|
|
|
|
request 'https://admin.inboxapp.com/api/status/accounts', (err, resp, data) =>
|
2015-03-22 02:59:47 +08:00
|
|
|
if err
|
|
|
|
@_error = err
|
|
|
|
else
|
|
|
|
@_error = null
|
|
|
|
@_accountCache = JSON.parse(data)
|
2015-03-21 10:15:32 +08:00
|
|
|
@trigger(@)
|
|
|
|
|
|
|
|
# Swap the url's to see real data
|
|
|
|
request 'https://admin.inboxapp.com/api/status/accounts/applications', (err, resp, data) =>
|
2015-03-22 02:59:47 +08:00
|
|
|
if err
|
|
|
|
@_error = err
|
|
|
|
else
|
|
|
|
@_error = null
|
|
|
|
@_applicationCache = JSON.parse(data)
|
2015-03-21 10:15:32 +08:00
|
|
|
@trigger(@)
|