codecombat/app/views/modal/employer_signup_modal.coffee

111 lines
3.4 KiB
CoffeeScript
Raw Normal View History

View = require 'views/kinds/ModalView'
template = require 'templates/modal/employer_signup_modal'
2014-04-24 18:27:37 -04:00
forms = require('lib/forms')
User = require 'models/User'
auth = require('lib/auth')
me = auth.me
module.exports = class EmployerSignupView extends View
id: "employer-signup"
template: template
closeButton: true
2014-04-24 18:27:37 -04:00
2014-04-23 14:25:36 -04:00
2014-04-24 18:27:37 -04:00
subscriptions:
"server-error": "onServerError"
"created-user-without-reload": "linkedInAuth"
events:
"click #contract-agreement-button": "agreeToContract"
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
window.contractCallback = =>
@authorizedWithLinkedIn = IN?.User?.isAuthorized()
@render()
2014-04-25 10:48:59 -04:00
onServerError: (e) ->
2014-04-24 18:27:37 -04:00
@disableModalInProgress(@$el)
2014-04-23 14:25:36 -04:00
afterInsert: ->
super()
2014-04-24 18:27:37 -04:00
linkedInButtonParentElement = document.getElementById("linkedInAuthButton")?.parentNode
if linkedInButtonParentElement
IN.parse()
if me.get('anonymous')
$(".IN-widget").get(0).addEventListener('click', @createAccount, true)
console.log "Parsed linkedin button element!"
console.log linkedInButtonParentElement
2014-04-23 14:25:36 -04:00
getRenderData: ->
context = super()
context.userIsAuthorized = @authorizedWithLinkedIn
2014-04-25 10:48:59 -04:00
context.userHasSignedContract = "employer" in me.get("permissions")
2014-04-24 18:27:37 -04:00
context.userIsAnonymous = context.me.get('anonymous')
2014-04-23 14:25:36 -04:00
context
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
type: "POST"
success: @handleAgreementSuccess
error: @handleAgreementFailure
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()
handleAgreementFailure: (error) ->
2014-04-25 10:48:59 -04:00
alert "There was an error signing the contract. Please contact support with this error: #{error}"
2014-04-24 20:36:07 -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-04-24 18:27:37 -04:00
console.log "Tried to create account!"
e.stopPropagation()
forms.clearFormAlerts(@$el)
userObject = forms.formToObject @$el
delete userObject.subscribe
for key, val of me.attributes when key in ["preferredLanguage", "testGroupNumber", "dateCreated", "wizardColor1", "name", "music", "volume", "emails"]
userObject[key] ?= val
subscribe = true
#TODO: Enable all email subscriptions
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
window.tracker?.trackEvent 'Finished Signup'
@enableModalInProgress(@$el)
auth.createUserWithoutReload userObject, null
linkedInAuth: (e) =>
console.log "Authorizing with linkedin"
@listenTo me,"sync", ->
IN.User.authorize(@recordUserDetails, @)
me.fetch()
@reloadWhenClosed = true
recordUserDetails: (e) =>
2014-04-24 20:36:07 -04:00
#TODO: refactor this out
2014-04-24 18:27:37 -04:00
@render()
destroy: ->
reloadWhenClosed = @reloadWhenClosed
super()
if reloadWhenClosed
window.location.reload()
2014-04-23 14:25:36 -04:00