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-12-09 09:05:59 +08:00
|
|
|
{AccountStore, Actions} = require 'nylas-exports'
|
2015-11-24 14:09:17 +08:00
|
|
|
{ipcRenderer} = require 'electron'
|
2015-06-18 06:58:58 +08:00
|
|
|
NylasStore = require 'nylas-store'
|
2015-09-23 11:11:51 +08:00
|
|
|
url = require 'url'
|
2015-06-18 06:58:58 +08:00
|
|
|
|
2015-11-12 02:25:11 +08:00
|
|
|
return unless NylasEnv.getWindowType() is "onboarding"
|
2015-06-18 06:58:58 +08:00
|
|
|
|
|
|
|
class PageRouterStore extends NylasStore
|
|
|
|
constructor: ->
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.onWindowPropsReceived @_onWindowPropsChanged
|
2015-06-18 06:58:58 +08:00
|
|
|
|
2015-11-12 02:25:11 +08:00
|
|
|
@_page = NylasEnv.getWindowProps().page ? ''
|
|
|
|
@_pageData = NylasEnv.getWindowProps().pageData ? {}
|
2015-06-18 06:58:58 +08:00
|
|
|
@_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-11-24 14:09:17 +08:00
|
|
|
ipcRenderer.send('new-account-added')
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.displayWindow()
|
2015-09-25 05:51:15 +08:00
|
|
|
if isFirstAccount
|
|
|
|
@_onMoveToPage('initial-preferences', {account: json})
|
2015-12-09 09:05:59 +08:00
|
|
|
Actions.recordUserEvent('First Account Linked')
|
2015-09-25 05:51:15 +08:00
|
|
|
else
|
2015-11-24 14:09:17 +08:00
|
|
|
ipcRenderer.send('account-setup-successful')
|
2015-09-25 05:51:15 +08:00
|
|
|
|
|
|
|
_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
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.quit()
|
2015-09-28 17:12:35 +08:00
|
|
|
else
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.close()
|
2015-09-28 17:12:35 +08:00
|
|
|
|
2015-10-05 07:49:41 +08:00
|
|
|
_checkTokenAuthStatus: ->
|
|
|
|
@_tokenAuthEnabled = "unknown"
|
|
|
|
@_tokenAuthEnabledError = null
|
|
|
|
@trigger()
|
|
|
|
|
|
|
|
TokenAuthAPI.request
|
2015-10-28 01:50:18 +08:00
|
|
|
path: "/status/"
|
2015-10-05 07:49:41 +08:00
|
|
|
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()
|