2015-06-18 06:58:58 +08:00
|
|
|
Reflux = require 'reflux'
|
|
|
|
OnboardingActions = require './onboarding-actions'
|
2015-10-05 07:49:41 +08:00
|
|
|
TokenAuthAPI = require './token-auth-api'
|
2015-09-25 05:51:15 +08:00
|
|
|
{AccountStore} = require 'nylas-exports'
|
2015-06-18 06:58:58 +08:00
|
|
|
NylasStore = require 'nylas-store'
|
|
|
|
ipc = require 'ipc'
|
2015-09-23 11:11:51 +08:00
|
|
|
url = require 'url'
|
2015-06-18 06:58:58 +08:00
|
|
|
|
|
|
|
return unless atom.getWindowType() is "onboarding"
|
|
|
|
|
|
|
|
class PageRouterStore extends NylasStore
|
|
|
|
constructor: ->
|
2015-09-25 05:51:15 +08:00
|
|
|
atom.onWindowPropsReceived @_onWindowPropsChanged
|
2015-06-18 06:58:58 +08:00
|
|
|
|
|
|
|
@_page = atom.getWindowProps().page ? ''
|
|
|
|
@_pageData = atom.getWindowProps().pageData ? {}
|
|
|
|
@_pageStack = [{page: @_page, pageData: @_pageData}]
|
|
|
|
|
2015-10-05 07:49:41 +08:00
|
|
|
@_checkTokenAuthStatus()
|
|
|
|
|
2015-06-18 06:58:58 +08:00
|
|
|
@listenTo OnboardingActions.moveToPreviousPage, @_onMoveToPreviousPage
|
|
|
|
@listenTo OnboardingActions.moveToPage, @_onMoveToPage
|
2015-09-28 17:12:35 +08:00
|
|
|
@listenTo OnboardingActions.closeWindow, @_onCloseWindow
|
2015-09-25 05:51:15 +08:00
|
|
|
@listenTo OnboardingActions.accountJSONReceived, @_onAccountJSONReceived
|
2015-10-05 07:49:41 +08:00
|
|
|
@listenTo OnboardingActions.retryCheckTokenAuthStatus, @_checkTokenAuthStatus
|
2015-09-25 05:51:15 +08:00
|
|
|
|
|
|
|
_onAccountJSONReceived: (json) =>
|
|
|
|
isFirstAccount = AccountStore.items().length is 0
|
|
|
|
AccountStore.addAccountFromJSON(json)
|
2015-10-01 01:47:33 +08:00
|
|
|
ipc.send('new-account-added')
|
2015-09-25 05:51:15 +08:00
|
|
|
atom.displayWindow()
|
|
|
|
if isFirstAccount
|
|
|
|
@_onMoveToPage('initial-preferences', {account: json})
|
|
|
|
else
|
|
|
|
ipc.send('account-setup-successful')
|
|
|
|
|
|
|
|
_onWindowPropsChanged: ({page, pageData}={}) =>
|
2015-06-18 06:58:58 +08:00
|
|
|
@_onMoveToPage(page, pageData)
|
|
|
|
|
|
|
|
page: -> @_page
|
|
|
|
|
|
|
|
pageData: -> @_pageData
|
|
|
|
|
2015-10-05 07:49:41 +08:00
|
|
|
tokenAuthEnabled: -> @_tokenAuthEnabled
|
|
|
|
|
|
|
|
tokenAuthEnabledError: -> @_tokenAuthEnabledError
|
|
|
|
|
2015-06-18 06:58:58 +08:00
|
|
|
connectType: ->
|
|
|
|
@_connectType
|
|
|
|
|
|
|
|
_onMoveToPreviousPage: ->
|
|
|
|
current = @_pageStack.pop()
|
|
|
|
prev = @_pageStack.pop()
|
|
|
|
@_onMoveToPage(prev.page, prev.pageData)
|
|
|
|
|
|
|
|
_onMoveToPage: (page, pageData={}) ->
|
|
|
|
@_pageStack.push({page, pageData})
|
|
|
|
@_page = page
|
|
|
|
@_pageData = pageData
|
|
|
|
@trigger()
|
|
|
|
|
2015-09-28 17:12:35 +08:00
|
|
|
_onCloseWindow: ->
|
|
|
|
isFirstAccount = AccountStore.items().length is 0
|
|
|
|
if isFirstAccount
|
|
|
|
atom.quit()
|
|
|
|
else
|
|
|
|
atom.close()
|
|
|
|
|
2015-10-05 07:49:41 +08:00
|
|
|
_checkTokenAuthStatus: ->
|
|
|
|
@_tokenAuthEnabled = "unknown"
|
|
|
|
@_tokenAuthEnabledError = null
|
|
|
|
@trigger()
|
|
|
|
|
|
|
|
TokenAuthAPI.request
|
|
|
|
path: "/status"
|
|
|
|
returnsModel: false
|
|
|
|
timeout: 10000
|
|
|
|
success: (json) =>
|
|
|
|
if json.restricted
|
|
|
|
@_tokenAuthEnabled = "yes"
|
|
|
|
else
|
|
|
|
@_tokenAuthEnabled = "no"
|
|
|
|
@trigger()
|
|
|
|
error: (err) =>
|
2015-10-05 07:57:43 +08:00
|
|
|
if err.statusCode is 404
|
|
|
|
err.message = "Sorry, we could not reach the Nylas API. Please try again."
|
2015-10-05 07:49:41 +08:00
|
|
|
@_tokenAuthEnabledError = err.message
|
|
|
|
@trigger()
|
|
|
|
|
2015-06-18 06:58:58 +08:00
|
|
|
module.exports = new PageRouterStore()
|