codecombat/app/views/core/AuthModal.coffee

168 lines
5.6 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'
module.exports = class AuthModal extends ModalView
2014-06-30 22:16:26 -04:00
id: 'auth-modal'
template: template
mode: 'signup' # or 'login'
2014-06-30 22:16:26 -04:00
events:
# login buttons
2014-06-30 22:16:26 -04:00
'click #switch-to-signup-button': 'onSignupInstead'
'click #switch-to-login-button': 'onLoginInstead'
2014-08-15 10:20:45 -04:00
'click #github-login-button': 'onGitHubLoginClicked'
'submit': 'onSubmitForm' # handles both submit buttons
'keyup #name': 'onNameChange'
'click #gplus-login-button': 'onClickGPlusLogin'
'click #close-modal': 'hide'
'click #facebook-login-btn': 'onClickFacebookLoginButton'
subscriptions:
'errors:server-error': 'onServerError'
'auth:logging-in-with-facebook': 'onLoggingInWithFacebook'
2014-06-30 22:16:26 -04:00
constructor: (options) ->
options ?= {}
@onNameChange = _.debounce @checkNameExists, 500
super options
@mode = options.mode if options.mode
getRenderData: ->
c = super()
c.showRequiredError = @options.showRequiredError
c.showSignupRationale = @options.showSignupRationale
c.mode = @mode
c.formValues = @previousFormInputs or {}
c.me = me
c
afterRender: ->
super()
@$el.toggleClass('signup', @mode is 'signup').toggleClass('login', @mode is 'login')
2015-11-19 12:05:00 -05:00
@playSound 'game-menu-open'
afterInsert: ->
super()
_.delay (=> application.router.renderLoginButtons()), 500
_.delay (=> $('input:visible:first', @$el).focus()), 500
2014-06-30 22:16:26 -04:00
onSignupInstead: (e) ->
@playSound 'menu-button-click'
@mode = 'signup'
@previousFormInputs = forms.formToObject @$el
@render()
_.delay application.router.renderLoginButtons, 500
2014-06-30 22:16:26 -04:00
onLoginInstead: (e) ->
@playSound 'menu-button-click'
@mode = 'login'
@previousFormInputs = forms.formToObject @$el
@render()
_.delay application.router.renderLoginButtons, 500
onSubmitForm: (e) ->
@playSound 'menu-button-click'
e.preventDefault()
if @mode is 'login' then @loginAccount() else @createAccount()
false
loginAccount: ->
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
2015-06-30 13:31:52 -04:00
emailCheck: ->
2015-06-30 12:35:45 -04:00
email = $('#email', @$el).val()
filter = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,63}$/i # https://news.ycombinator.com/item?id=5763990
2015-06-30 12:35:45 -04:00
unless filter.test(email)
2015-06-30 13:31:52 -04:00
forms.setErrorToProperty @$el, 'email', 'Please enter a valid email address', true
2015-06-30 12:35:45 -04:00
return false
return true
createAccount: ->
forms.clearFormAlerts(@$el)
2015-06-30 13:31:52 -04:00
return unless @emailCheck()
userObject = forms.formToObject @$el
delete userObject.subscribe
delete userObject.name if userObject.name is ''
delete userObject.schoolName if userObject.schoolName is ''
userObject.name = @suggestedName if @suggestedName
2015-12-01 20:32:02 -05:00
for key, val of me.attributes when key in ['preferredLanguage', 'testGroupNumber', 'dateCreated', 'wizardColor1', 'name', 'music', 'volume', 'emails', 'schoolName']
userObject[key] ?= val
subscribe = @$el.find('#subscribe').prop('checked')
userObject.emails ?= {}
userObject.emails.generalNews ?= {}
userObject.emails.generalNews.enabled = subscribe
res = tv4.validateMultiple userObject, User.schema
return forms.applyErrorsToForm(@$el, res.errors) unless res.valid
Backbone.Mediator.publish "auth:signed-up", {}
2014-12-08 18:00:49 -05:00
window.tracker?.trackEvent 'Finished Signup', label: 'CodeCombat'
@enableModalInProgress(@$el)
createUser userObject, null, window.nextURL
onLoggingInWithFacebook: (e) ->
modal = $('.modal:visible', @$el)
@enableModalInProgress(modal) # TODO: part of forms
onServerError: (e) -> # TODO: work error handling into a separate forms system
2014-06-30 22:16:26 -04:00
@disableModalInProgress(@$el)
checkNameExists: =>
name = $('#name', @$el).val()
return forms.clearFormAlerts(@$el) if name is ''
User.getUnconflictedName name, (newName) =>
forms.clearFormAlerts(@$el)
if name is newName
@suggestedName = undefined
else
@suggestedName = newName
forms.setErrorToProperty @$el, 'name', "That name is taken! How about #{newName}?", true
2014-08-15 10:20:45 -04:00
onGitHubLoginClicked: ->
@playSound 'menu-button-click'
Backbone.Mediator.publish 'auth:log-in-with-github', {}
gplusAuthSteps: [
{ i18n: 'login.authenticate_gplus', done: false }
{ i18n: 'login.load_profile', done: false }
{ i18n: 'login.finishing', done: false }
]
onClickGPlusLogin: ->
@playSound 'menu-button-click'
step.done = false for step in @gplusAuthSteps
handler = application.gplusHandler
@listenToOnce handler, 'logged-in', ->
@gplusAuthSteps[0].done = true
@renderGPlusAuthChecklist()
handler.loginCodeCombat()
@listenToOnce handler, 'person-loaded', ->
@gplusAuthSteps[1].done = true
@renderGPlusAuthChecklist()
@listenToOnce handler, 'logging-into-codecombat', ->
2015-02-13 14:18:00 -05:00
@gplusAuthSteps[2].done = true
@renderGPlusAuthChecklist()
renderGPlusAuthChecklist: ->
2014-12-03 19:40:28 -05:00
template = require 'templates/core/auth-modal-gplus-checklist'
el = $(template({steps: @gplusAuthSteps}))
el.i18n()
@$el.find('.modal-body:visible').empty().append(el)
@$el.find('.modal-footer').remove()
2015-11-19 12:05:00 -05:00
onClickFacebookLoginButton: ->
application.facebookHandler.loginThroughFacebook()
2015-11-19 12:05:00 -05:00
onHidden: ->
super()
@playSound 'game-menu-close'