mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-28 10:06:08 -05:00
56 lines
2 KiB
CoffeeScript
56 lines
2 KiB
CoffeeScript
|
View = require 'views/kinds/ModalView'
|
||
|
template = require 'templates/modal/signup'
|
||
|
{createUser, me} = require('lib/auth')
|
||
|
forms = require('lib/forms')
|
||
|
User = require 'models/User'
|
||
|
|
||
|
filterKeyboardEvents = (allowedEvents, func) ->
|
||
|
return (splat...) ->
|
||
|
e = splat[0]
|
||
|
return unless e.keyCode in allowedEvents or not e.keyCode
|
||
|
return func(splat...)
|
||
|
|
||
|
module.exports = class SignupModalView extends View
|
||
|
id: "signup-modal"
|
||
|
template: template
|
||
|
modalTitle: "Create Account to Save Progress"
|
||
|
|
||
|
events:
|
||
|
"click #signup-confirm-age": "checkAge"
|
||
|
"click #signup-button": "createAccount"
|
||
|
"keydown input": "createAccount"
|
||
|
|
||
|
subscriptions:
|
||
|
'server-error': 'onServerError'
|
||
|
'logging-in-with-facebook': 'onLoggingInWithFacebook'
|
||
|
|
||
|
onServerError: (e) -> # TODO: work error handling into a separate forms system
|
||
|
@disableModalInProgress(@$el)
|
||
|
|
||
|
constructor: (options) ->
|
||
|
@createAccount = filterKeyboardEvents([13], @createAccount) # TODO: part of forms
|
||
|
super options
|
||
|
window.tracker?.trackEvent 'Started Signup'
|
||
|
|
||
|
onLoggingInWithFacebook: (e) ->
|
||
|
modal = $('.modal:visible', @$el)
|
||
|
@enableModalInProgress(modal) # TODO: part of forms
|
||
|
|
||
|
checkAge: (e) ->
|
||
|
$("#signup-button", @$el).attr 'disabled', not $(e.target).attr('checked')
|
||
|
|
||
|
createAccount: (e) =>
|
||
|
forms.clearFormAlerts(@$el)
|
||
|
userObject = forms.formToObject @$el
|
||
|
delete userObject.subscribe
|
||
|
delete userObject["confirm-age"]
|
||
|
for key, val of me.attributes when key in ["preferredLanguage", "testGroupNumber", "dateCreated", "wizardColor1", "name", "music", "volume"]
|
||
|
userObject[key] ?= val
|
||
|
subscribe = @$el.find('#signup-subscribe').attr('checked')
|
||
|
userObject.emailSubscriptions = if subscribe then ['announcement'] else []
|
||
|
res = tv4.validateMultiple userObject, User.schema.attributes
|
||
|
return forms.applyErrorsToForm(@$el, res.errors) unless res.valid
|
||
|
window.tracker?.trackEvent 'Finished Signup'
|
||
|
@enableModalInProgress(@$el)
|
||
|
createUser(userObject)
|