React = require 'react'
_ = require 'underscore'
{RetinaImg, TimeoutTransitionGroup} = require 'nylas-component-kit'
{Utils} = require 'nylas-exports'
TokenAuthAPI = require './token-auth-api'
OnboardingActions = require './onboarding-actions'
PageRouterStore = require './page-router-store'
Providers = require './account-types'
url = require 'url'
class TokenAuthPage extends React.Component
@displayName: "TokenAuthPage"
constructor: (@props) ->
@state =
token: ""
tokenValidityError: null
tokenAuthInflight: false
tokenAuthEnabled: PageRouterStore.tokenAuthEnabled()
tokenAuthEnabledError: PageRouterStore.tokenAuthEnabledError()
componentDidMount: ->
@_usub = PageRouterStore.listen(@_onTokenAuthChange)
_onTokenAuthChange: =>
@setState
tokenAuthEnabled: PageRouterStore.tokenAuthEnabled()
tokenAuthEnabledError: PageRouterStore.tokenAuthEnabledError()
@_resize()
componentWillUnmount: ->
@_usub?()
render: =>
if @state.tokenAuthEnabled is "unknown"
{@_renderWaitingForTokenAuthAnswer()}
else if @state.tokenAuthEnabled is "yes"
OnboardingActions.closeWindow() }>
Due to overwhelming interest, you need an invitation code to connect
an account to N1. Enter your invitation code below, or
request one here .
{@_renderContinueError()}
{@_renderInput()}
{@_renderContinueButton()}
else
_renderWaitingForTokenAuthAnswer: =>
if @state.tokenAuthEnabledError
{@state.tokenAuthEnabledError}
Try Again
else
_renderInput: =>
if @state.errorMessage
else
_renderContinueButton: =>
if @state.tokenAuthInflight
Checking…
else
Continue
_renderContinueError: =>
if @state.tokenValidityError
{@state.tokenValidityError}
else
_onTokenChange: (event) =>
@setState(token: event.target.value)
_onContinue: =>
if @state.tokenAuthInflight
return
if not @state.token
@setState({
tokenAuthInflight: false,
tokenValidityError: "Please enter an invitation code."
})
@_resize()
return
@setState({tokenAuthInflight: true})
TokenAuthAPI.request
path: "/token/#{@state.token}"
returnsModel: false
timeout: 30000
success: (json) =>
atom.config.set("invitationCode", @state.token)
OnboardingActions.moveToPage("account-choose")
error: (err) =>
_.delay =>
@setState
tokenValidityError: err.message
tokenAuthInflight: false
@_resize()
, 400
_resize: =>
setTimeout( =>
@props.onResize?()
,10)
module.exports = TokenAuthPage