codecombat/app/views/modal/signup_modal.coffee

65 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
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
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).prop 'disabled', not $(e.target).prop('checked')
2014-03-31 16:56:13 -04:00
getRenderData: ->
c = super()
c.showRequiredError = @options.showRequiredError
c
2014-01-03 13:32:13 -05:00
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", "emailSubscriptions"]
2014-01-03 13:32:13 -05:00
userObject[key] ?= val
subscribe = @$el.find('#signup-subscribe').prop('checked')
2014-01-06 13:33:52 -05:00
userObject.emailSubscriptions ?= []
if subscribe
2014-01-06 13:33:52 -05:00
userObject.emailSubscriptions.push 'announcement' unless 'announcement' in userObject.emailSubscriptions
2014-03-10 16:20:00 -04:00
userObject.emailSubscriptions.push 'notification' unless 'notification' in userObject.emailSubscriptions
else
2014-03-10 16:20:00 -04:00
userObject.emailSubscriptions = _.without (userObject.emailSubscriptions ? []), 'announcement', 'notification'
2014-01-03 13:32:13 -05:00
res = tv4.validateMultiple userObject, User.schema.attributes
return forms.applyErrorsToForm(@$el, res.errors) unless res.valid
window.tracker?.trackEvent 'Finished Signup'
@enableModalInProgress(@$el)
2014-03-31 16:56:13 -04:00
createUser userObject, null, window.nextLevelURL