codecombat/app/views/modal/employer_signup_modal.coffee

139 lines
4.5 KiB
CoffeeScript
Raw Normal View History

2014-07-17 20:22:26 -04:00
ModalView = require 'views/kinds/ModalView'
template = require 'templates/modal/employer_signup_modal'
2014-06-30 22:16:26 -04:00
forms = require 'lib/forms'
2014-04-24 18:27:37 -04:00
User = require 'models/User'
2014-06-30 22:16:26 -04:00
auth = require 'lib/auth'
2014-04-24 18:27:37 -04:00
me = auth.me
2014-07-17 20:22:26 -04:00
module.exports = class EmployerSignupView extends ModalView
2014-06-30 22:16:26 -04:00
id: 'employer-signup'
template: template
closeButton: true
2014-04-24 18:27:37 -04:00
subscriptions:
2014-06-30 22:16:26 -04:00
'server-error': 'onServerError'
'linkedin-loaded': 'onLinkedInLoaded'
2014-06-30 22:16:26 -04:00
'created-user-without-reload': 'createdAccount'
2014-04-25 13:46:43 -04:00
2014-04-24 18:27:37 -04:00
events:
2014-06-30 22:16:26 -04:00
'click #contract-agreement-button': 'agreeToContract'
'click #create-account-button': 'createAccount'
'click #more-info-button': 'submitMoreInfoEmail'
'click .login-link': 'setHashToOpenModalAutomatically'
'keydown': 'checkForFormSubmissionEnterPress'
2014-04-25 13:46:43 -04:00
2014-04-23 14:25:36 -04:00
constructor: (options) ->
super(options)
@authorizedWithLinkedIn = IN?.User?.isAuthorized()
2014-04-25 10:48:59 -04:00
window.tracker?.trackEvent 'Started Employer Signup'
2014-04-24 18:27:37 -04:00
@reloadWhenClosed = false
@linkedinLoaded = Boolean(IN.parse)
@waitingForLinkedIn = false
@sentMoreInfoEmail = false
2014-04-25 13:46:43 -04:00
window.contractCallback = =>
2014-04-24 18:27:37 -04:00
@authorizedWithLinkedIn = IN?.User?.isAuthorized()
@render()
2014-04-25 13:46:43 -04:00
onLinkedInLoaded: =>
@linkedinLoaded = true
if @waitingForLinkedIn
@renderLinkedInButton()
renderLinkedInButton: =>
IN.parse()
2014-04-25 13:46:43 -04:00
onServerError: (e) ->
2014-04-24 18:27:37 -04:00
@disableModalInProgress(@$el)
2014-04-25 13:46:43 -04:00
2014-04-23 14:25:36 -04:00
afterInsert: ->
super()
2014-06-30 22:16:26 -04:00
linkedInButtonParentElement = document.getElementById('linkedInAuthButton')
2014-04-24 18:27:37 -04:00
if linkedInButtonParentElement
if @linkedinLoaded
@renderLinkedInButton()
else
@waitingForLinkedIn = true
2014-04-25 13:46:43 -04:00
2014-04-23 14:25:36 -04:00
getRenderData: ->
context = super()
context.userIsAuthorized = @authorizedWithLinkedIn
2014-06-30 22:16:26 -04:00
context.userHasSignedContract = 'employer' in me.get('permissions')
2014-04-24 18:27:37 -04:00
context.userIsAnonymous = context.me.get('anonymous')
context.sentMoreInfoEmail = @sentMoreInfoEmail
2014-04-23 14:25:36 -04:00
context
2014-04-25 13:46:43 -04:00
2014-04-24 18:27:37 -04:00
agreeToContract: ->
2014-04-24 20:36:07 -04:00
application.linkedinHandler.constructEmployerAgreementObject (err, profileData) =>
if err? then return handleAgreementFailure err
$.ajax
url: "/db/user/#{me.id}/agreeToEmployerAgreement"
data: profileData
2014-06-30 22:16:26 -04:00
type: 'POST'
2014-04-24 20:36:07 -04:00
success: @handleAgreementSuccess
error: @handleAgreementFailure
2014-04-25 13:46:43 -04:00
2014-04-24 20:36:07 -04:00
handleAgreementSuccess: (result) ->
2014-04-25 10:48:59 -04:00
window.tracker?.trackEvent 'Employer Agreed to Contract'
2014-04-24 20:36:07 -04:00
me.fetch()
window.location.reload()
2014-04-25 13:46:43 -04:00
2014-04-24 20:36:07 -04:00
handleAgreementFailure: (error) ->
2014-04-25 10:50:29 -04:00
alert "There was an error signing the contract. Please contact team@codecombat.com with this error: #{error.responseText}"
2014-04-25 13:46:43 -04:00
2014-05-07 16:33:15 -04:00
checkForFormSubmissionEnterPress: (e) ->
if e.which is 13
2014-06-30 22:16:26 -04:00
if $('#signup-email').val() isnt '' and $('#signup-password').val() isnt ''
@createAccount(e)
2014-06-30 22:16:26 -04:00
else if $('#more-info-email').val() isnt ''
@submitMoreInfoEmail e
2014-05-07 16:33:15 -04:00
2014-04-24 18:27:37 -04:00
createAccount: (e) =>
2014-04-25 10:48:59 -04:00
window.tracker?.trackEvent 'Finished Employer Signup'
2014-06-30 22:16:26 -04:00
el = $('#signup-form')
2014-04-24 18:27:37 -04:00
e.stopPropagation()
forms.clearFormAlerts(el)
userObject = forms.formToObject el
2014-04-24 18:27:37 -04:00
delete userObject.subscribe
2014-06-30 22:16:26 -04:00
for key, val of me.attributes when key in ['preferredLanguage', 'testGroupNumber', 'dateCreated', 'wizardColor1', 'name', 'music', 'volume', 'emails']
2014-04-24 18:27:37 -04:00
userObject[key] ?= val
2014-04-25 13:55:37 -04:00
userObject.emails ?= {}
2014-04-25 13:46:43 -04:00
userObject.emails.employerNotes = {enabled: true}
2014-04-24 18:27:37 -04:00
res = tv4.validateMultiple userObject, User.schema
return forms.applyErrorsToForm(el, res.errors) unless res.valid
@enableModalInProgress(el)
2014-04-24 18:27:37 -04:00
auth.createUserWithoutReload userObject, null
submitMoreInfoEmail: (e) =>
2014-06-30 22:16:26 -04:00
emailAddress = $('#more-info-email').val()
window.tracker?.trackEvent 'Employer requested more information.'
successFunc = =>
@sentMoreInfoEmail = true
@render()
errorFunc = =>
2014-06-30 22:16:26 -04:00
alert('Something went wrong! Please contact team@codecombat.com for more information and inform them of this error.')
$.ajax
2014-06-30 22:16:26 -04:00
type: 'POST'
url: '/contact'
data:
email: emailAddress
message: "THIS IS AN AUTOMATED MESSAGE FROM THE EMPLOYER SIGNUP FORM \n Please send me more info about hiring CodeCombat players."
success: successFunc
error: errorFunc
2014-06-30 22:16:26 -04:00
$.post '/stacklead', email: emailAddress
setHashToOpenModalAutomatically: (e) ->
2014-06-30 22:16:26 -04:00
window.location.hash = 'employerSignupLoggingIn'
createdAccount: ->
2014-04-24 18:27:37 -04:00
@reloadWhenClosed = true
2014-06-30 22:16:26 -04:00
@listenTo me, 'sync', =>
@render()
IN.parse()
me.fetch()
2014-04-25 13:46:43 -04:00
2014-04-24 18:27:37 -04:00
destroy: ->
reloadWhenClosed = @reloadWhenClosed
super()
if reloadWhenClosed
window.location.reload()