codecombat/app/views/core/AuthModal.coffee

144 lines
4.3 KiB
CoffeeScript
Raw Normal View History

ModalView = require 'views/core/ModalView'
template = require 'templates/core/auth'
{loginUser, createUser, me} = require 'core/auth'
forms = require 'core/forms'
User = require 'models/User'
application = require 'core/application'
errors = require 'core/errors'
module.exports = class AuthModal extends ModalView
2014-06-30 22:16:26 -04:00
id: 'auth-modal'
template: template
2014-06-30 22:16:26 -04:00
events:
'click #switch-to-signup-btn': 'onSignupInstead'
2014-08-15 10:20:45 -04:00
'click #github-login-button': 'onGitHubLoginClicked'
'submit form': 'onSubmitForm'
'keyup #name': 'onNameChange'
'click #gplus-login-btn': 'onClickGPlusLoginButton'
'click #facebook-login-btn': 'onClickFacebookLoginButton'
'click #close-modal': 'hide'
subscriptions:
'errors:server-error': 'onServerError'
2014-06-30 22:16:26 -04:00
# Initialization
initialize: (options={}) ->
@previousFormInputs = options.initialValues or {}
getRenderData: ->
c = super()
c.showRequiredError = @options.showRequiredError
c.showSignupRationale = @options.showSignupRationale
c.mode = @mode
c.formValues = @previousFormInputs or {}
c.me = me
c
afterRender: ->
super()
2015-11-19 12:05:00 -05:00
@playSound 'game-menu-open'
afterInsert: ->
super()
_.delay (=> $('input:visible:first', @$el).focus()), 500
2014-06-30 22:16:26 -04:00
onSignupInstead: (e) ->
CreateAccountModal = require('./CreateAccountModal')
modal = new CreateAccountModal({initialValues: forms.formToObject @$el})
currentView.openModalView(modal)
onSubmitForm: (e) ->
@playSound 'menu-button-click'
e.preventDefault()
forms.clearFormAlerts(@$el)
userObject = forms.formToObject @$el
res = tv4.validateMultiple userObject, User.schema
return forms.applyErrorsToForm(@$el, res.errors) unless res.valid
@enableModalInProgress(@$el) # TODO: part of forms
2015-02-11 19:12:26 -05:00
loginUser userObject, null, window.nextURL
onServerError: (e) -> # TODO: work error handling into a separate forms system
2014-06-30 22:16:26 -04:00
@disableModalInProgress(@$el)
# Google Plus
onClickGPlusLoginButton: ->
btn = @$('#gplus-login-btn')
btn.attr('disabled', true)
application.gplusHandler.loadAPI({
context: @
success: ->
btn.attr('disabled', false)
application.gplusHandler.connect({
context: @
success: ->
btn.find('.sign-in-blurb').text($.i18n.t('login.logging_in'))
btn.attr('disabled', true)
application.gplusHandler.loadPerson({
context: @
success: (gplusAttrs) ->
existingUser = new User()
existingUser.fetchGPlusUser(gplusAttrs.gplusID, {
success: =>
me.loginGPlusUser(gplusAttrs.gplusID, {
success: -> window.location.reload()
error: @onGPlusLoginError
})
error: @onGPlusLoginError
})
})
})
})
onGPlusLoginError: =>
btn = @$('#gplus-login-btn')
btn.find('.sign-in-blurb').text($.i18n.t('login.sign_in_with_gplus'))
btn.attr('disabled', false)
errors.showNotyNetworkError(arguments...)
# Facebook
2015-11-19 12:05:00 -05:00
onClickFacebookLoginButton: ->
btn = @$('#facebook-login-btn')
btn.attr('disabled', true)
application.facebookHandler.loadAPI({
context: @
success: ->
btn.attr('disabled', false)
application.facebookHandler.connect({
context: @
success: ->
btn.find('.sign-in-blurb').text($.i18n.t('login.logging_in'))
btn.attr('disabled', true)
application.facebookHandler.loadPerson({
context: @
success: (facebookAttrs) ->
existingUser = new User()
existingUser.fetchFacebookUser(facebookAttrs.facebookID, {
success: =>
me.loginFacebookUser(facebookAttrs.facebookID, {
success: -> window.location.reload()
error: @onFacebookLoginError
})
error: @onFacebookLoginError
})
})
})
})
onFacebookLoginError: =>
btn = @$('#facebook-login-btn')
btn.find('.sign-in-blurb').text($.i18n.t('login.sign_in_with_facebook'))
btn.attr('disabled', false)
errors.showNotyNetworkError(arguments...)
2015-11-19 12:05:00 -05:00
onHidden: ->
super()
@playSound 'game-menu-close'