2016-03-09 17:40:52 -05:00
|
|
|
RootView = require 'views/core/RootView'
|
|
|
|
forms = require 'core/forms'
|
|
|
|
TrialRequest = require 'models/TrialRequest'
|
|
|
|
TrialRequests = require 'collections/TrialRequests'
|
|
|
|
AuthModal = require 'views/core/AuthModal'
|
|
|
|
errors = require 'core/errors'
|
|
|
|
User = require 'models/User'
|
2016-04-20 13:29:59 -04:00
|
|
|
algolia = require 'core/services/algolia'
|
2016-03-09 17:40:52 -05:00
|
|
|
|
|
|
|
FORM_KEY = 'request-quote-form'
|
2016-03-30 19:20:37 -04:00
|
|
|
SIGNUP_REDIRECT = '/teachers/classes'
|
2016-04-20 13:29:59 -04:00
|
|
|
NCES_KEYS = ['id', 'name', 'district', 'district_id', 'district_schools', 'district_students', 'students', 'phone']
|
2016-03-09 17:40:52 -05:00
|
|
|
|
|
|
|
module.exports = class CreateTeacherAccountView extends RootView
|
|
|
|
id: 'create-teacher-account-view'
|
|
|
|
template: require 'templates/teachers/create-teacher-account-view'
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click .login-link': 'onClickLoginLink'
|
|
|
|
'change form': 'onChangeForm'
|
|
|
|
'submit form': 'onSubmitForm'
|
|
|
|
'click #gplus-signup-btn': 'onClickGPlusSignupButton'
|
|
|
|
'click #facebook-signup-btn': 'onClickFacebookSignupButton'
|
2016-04-20 13:29:59 -04:00
|
|
|
'change input[name="city"]': 'invalidateNCES'
|
|
|
|
'change input[name="state"]': 'invalidateNCES'
|
|
|
|
'change input[name="district"]': 'invalidateNCES'
|
|
|
|
'change input[name="country"]': 'invalidateNCES'
|
2016-03-09 17:40:52 -05:00
|
|
|
|
|
|
|
initialize: ->
|
|
|
|
@trialRequest = new TrialRequest()
|
|
|
|
@trialRequests = new TrialRequests()
|
|
|
|
@trialRequests.fetchOwn()
|
|
|
|
@supermodel.trackCollection(@trialRequests)
|
2016-06-08 09:24:59 -04:00
|
|
|
window.tracker?.trackEvent 'Teachers Create Account Loaded', category: 'Teachers', ['Mixpanel']
|
2016-03-09 17:40:52 -05:00
|
|
|
|
2016-04-19 13:20:56 -04:00
|
|
|
onLeaveMessage: ->
|
|
|
|
if @formChanged
|
|
|
|
return 'Your account has not been created! If you continue, your changes will be lost.'
|
|
|
|
|
2016-03-09 17:40:52 -05:00
|
|
|
onLoaded: ->
|
|
|
|
if @trialRequests.size()
|
|
|
|
@trialRequest = @trialRequests.first()
|
|
|
|
super()
|
2016-04-20 13:29:59 -04:00
|
|
|
|
|
|
|
invalidateNCES: ->
|
|
|
|
for key in NCES_KEYS
|
|
|
|
@$('input[name="nces_' + key + '"]').val ''
|
2016-03-09 17:40:52 -05:00
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
|
|
|
|
# apply existing trial request on form
|
|
|
|
properties = @trialRequest.get('properties')
|
|
|
|
if properties
|
|
|
|
forms.objectToForm(@$('form'), properties)
|
|
|
|
commonLevels = _.map @$('[name="educationLevel"]'), (el) -> $(el).val()
|
|
|
|
submittedLevels = properties.educationLevel or []
|
|
|
|
otherLevel = _.first(_.difference(submittedLevels, commonLevels)) or ''
|
|
|
|
@$('#other-education-level-checkbox').attr('checked', !!otherLevel)
|
|
|
|
@$('#other-education-level-input').val(otherLevel)
|
|
|
|
|
2016-04-21 14:21:11 -04:00
|
|
|
$("#organization-control").algolia_autocomplete({hint: false}, [
|
2016-04-20 13:29:59 -04:00
|
|
|
source: (query, callback) ->
|
|
|
|
algolia.schoolsIndex.search(query, { hitsPerPage: 5, aroundLatLngViaIP: false }).then (answer) ->
|
|
|
|
callback answer.hits
|
|
|
|
, ->
|
|
|
|
callback []
|
|
|
|
displayKey: 'name',
|
|
|
|
templates:
|
|
|
|
suggestion: (suggestion) ->
|
|
|
|
hr = suggestion._highlightResult
|
|
|
|
"<div class='school'> #{hr.name.value} </div>" +
|
|
|
|
"<div class='district'>#{hr.district.value}, " +
|
|
|
|
"<span>#{hr.city?.value}, #{hr.state.value}</span></div>"
|
|
|
|
|
|
|
|
]).on 'autocomplete:selected', (event, suggestion, dataset) =>
|
|
|
|
@$('input[name="city"]').val suggestion.city
|
|
|
|
@$('input[name="state"]').val suggestion.state
|
|
|
|
@$('input[name="district"]').val suggestion.district
|
|
|
|
@$('input[name="country"]').val 'USA'
|
|
|
|
|
|
|
|
for key in NCES_KEYS
|
|
|
|
@$('input[name="nces_' + key + '"]').val suggestion[key]
|
|
|
|
|
2016-04-19 13:20:56 -04:00
|
|
|
@onChangeForm()
|
2016-04-20 13:37:00 -04:00
|
|
|
|
2016-03-09 17:40:52 -05:00
|
|
|
onClickLoginLink: ->
|
|
|
|
modal = new AuthModal({ initialValues: { email: @trialRequest.get('properties')?.email } })
|
|
|
|
@openModalView(modal)
|
|
|
|
|
2016-04-19 13:20:56 -04:00
|
|
|
onChangeForm: ->
|
2016-06-08 09:24:59 -04:00
|
|
|
unless @formChanged
|
|
|
|
window.tracker?.trackEvent 'Teachers Create Account Form Started', category: 'Teachers', ['Mixpanel']
|
2016-04-19 13:20:56 -04:00
|
|
|
@formChanged = true
|
2016-03-09 17:40:52 -05:00
|
|
|
|
|
|
|
onSubmitForm: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
# Creating Trial Request first, validate user attributes but do not use them
|
|
|
|
form = @$('form')
|
|
|
|
allAttrs = forms.formToObject(form)
|
|
|
|
trialRequestAttrs = _.omit(allAttrs, 'name', 'password1', 'password2')
|
|
|
|
|
|
|
|
if @$('#other-education-level-checkbox').is(':checked')
|
|
|
|
val = @$('#other-education-level-input').val()
|
|
|
|
trialRequestAttrs.educationLevel.push(val) if val
|
|
|
|
|
|
|
|
forms.clearFormAlerts(form)
|
|
|
|
|
|
|
|
result = tv4.validateMultiple(trialRequestAttrs, formSchema)
|
|
|
|
error = false
|
|
|
|
if not result.valid
|
|
|
|
forms.applyErrorsToForm(form, result.errors)
|
|
|
|
error = true
|
|
|
|
if not forms.validateEmail(trialRequestAttrs.email)
|
|
|
|
forms.setErrorToProperty(form, 'email', 'Invalid email.')
|
|
|
|
error = true
|
|
|
|
if not _.size(trialRequestAttrs.educationLevel)
|
|
|
|
forms.setErrorToProperty(form, 'educationLevel', 'Include at least one.')
|
|
|
|
error = true
|
|
|
|
unless @gplusAttrs or @facebookAttrs
|
|
|
|
if not allAttrs.password1
|
|
|
|
forms.setErrorToProperty(form, 'password1', 'Required field')
|
|
|
|
error = true
|
|
|
|
else if not allAttrs.password2
|
|
|
|
forms.setErrorToProperty(form, 'password2', 'Required field')
|
|
|
|
error = true
|
|
|
|
else if allAttrs.password1 isnt allAttrs.password2
|
|
|
|
forms.setErrorToProperty(form, 'password1', 'Password fields are not equivalent')
|
|
|
|
error = true
|
|
|
|
if error
|
|
|
|
forms.scrollToFirstError()
|
|
|
|
return
|
2016-04-07 15:13:48 -04:00
|
|
|
trialRequestAttrs['siteOrigin'] = 'create teacher'
|
2016-03-09 17:40:52 -05:00
|
|
|
@trialRequest = new TrialRequest({
|
|
|
|
type: 'course'
|
|
|
|
properties: trialRequestAttrs
|
|
|
|
})
|
|
|
|
@trialRequest.notyErrors = false
|
|
|
|
@$('#create-account-btn').text('Sending').attr('disabled', true)
|
|
|
|
@trialRequest.save()
|
|
|
|
@trialRequest.on 'sync', @onTrialRequestSubmit, @
|
|
|
|
@trialRequest.on 'error', @onTrialRequestError, @
|
|
|
|
|
|
|
|
onTrialRequestError: (model, jqxhr) ->
|
|
|
|
@$('#create-account-btn').text('Submit').attr('disabled', false)
|
|
|
|
if jqxhr.status is 409
|
|
|
|
userExists = $.i18n.t('teachers_quote.email_exists')
|
|
|
|
logIn = $.i18n.t('login.log_in')
|
|
|
|
@$('#email-form-group')
|
|
|
|
.addClass('has-error')
|
|
|
|
.append($("<div class='help-block error-help-block'>#{userExists} <a class='login-link'>#{logIn}</a>"))
|
|
|
|
forms.scrollToFirstError()
|
|
|
|
else
|
|
|
|
errors.showNotyNetworkError(arguments...)
|
|
|
|
|
|
|
|
onClickEmailExistsLoginLink: ->
|
|
|
|
modal = new AuthModal({ initialValues: { email: @trialRequest.get('properties')?.email } })
|
|
|
|
@openModalView(modal)
|
|
|
|
|
|
|
|
onTrialRequestSubmit: ->
|
2016-06-08 09:24:59 -04:00
|
|
|
window.tracker?.trackEvent 'Teachers Create Account Submitted', category: 'Teachers', ['Mixpanel']
|
2016-04-19 13:20:56 -04:00
|
|
|
@formChanged = false
|
2016-03-09 17:40:52 -05:00
|
|
|
attrs = _.pick(forms.formToObject(@$('form')), 'name', 'email', 'role')
|
2016-03-30 19:20:37 -04:00
|
|
|
attrs.role = attrs.role.toLowerCase()
|
2016-03-09 17:40:52 -05:00
|
|
|
options = {}
|
|
|
|
newUser = new User(attrs)
|
|
|
|
if @gplusAttrs
|
|
|
|
newUser.set('_id', me.id)
|
|
|
|
options.url = "/db/user?gplusID=#{@gplusAttrs.gplusID}&gplusAccessToken=#{application.gplusHandler.accessToken.access_token}"
|
|
|
|
options.type = 'PUT'
|
|
|
|
newUser.set(@gplusAttrs)
|
|
|
|
else if @facebookAttrs
|
|
|
|
newUser.set('_id', me.id)
|
|
|
|
options.url = "/db/user?facebookID=#{@facebookAttrs.facebookID}&facebookAccessToken=#{application.facebookHandler.authResponse.accessToken}"
|
|
|
|
options.type = 'PUT'
|
|
|
|
newUser.set(@facebookAttrs)
|
|
|
|
else
|
|
|
|
newUser.set('password', @$('input[name="password1"]').val())
|
|
|
|
newUser.save(null, options)
|
|
|
|
newUser.once 'sync', ->
|
|
|
|
application.router.navigate(SIGNUP_REDIRECT, { trigger: true })
|
|
|
|
application.router.reload()
|
|
|
|
newUser.once 'error', errors.showNotyNetworkError
|
|
|
|
|
|
|
|
# GPlus signup
|
|
|
|
|
|
|
|
onClickGPlusSignupButton: ->
|
|
|
|
btn = @$('#gplus-signup-btn')
|
|
|
|
btn.attr('disabled', true)
|
|
|
|
application.gplusHandler.loadAPI({
|
|
|
|
success: =>
|
|
|
|
btn.attr('disabled', false)
|
|
|
|
application.gplusHandler.connect({
|
|
|
|
success: =>
|
|
|
|
btn.find('.sign-in-blurb').text($.i18n.t('signup.creating'))
|
|
|
|
btn.attr('disabled', true)
|
|
|
|
application.gplusHandler.loadPerson({
|
|
|
|
success: (@gplusAttrs) =>
|
|
|
|
existingUser = new User()
|
|
|
|
existingUser.fetchGPlusUser(@gplusAttrs.gplusID, {
|
|
|
|
error: (user, jqxhr) =>
|
|
|
|
if jqxhr.status is 404
|
|
|
|
@onGPlusConnected()
|
|
|
|
else
|
|
|
|
errors.showNotyNetworkError(jqxhr)
|
|
|
|
success: =>
|
|
|
|
me.loginGPlusUser(@gplusAttrs.gplusID, {
|
|
|
|
success: ->
|
2016-04-19 13:20:56 -04:00
|
|
|
application.router.navigate('/teachers/update-account', {trigger: true})
|
2016-03-09 17:40:52 -05:00
|
|
|
error: errors.showNotyNetworkError
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
onGPlusConnected: ->
|
2016-04-19 13:20:56 -04:00
|
|
|
@formChanged = true
|
2016-03-09 17:40:52 -05:00
|
|
|
forms.objectToForm(@$('form'), @gplusAttrs)
|
|
|
|
for field in ['email', 'firstName', 'lastName']
|
|
|
|
input = @$("input[name='#{field}']")
|
|
|
|
if input.val()
|
|
|
|
input.attr('disabled', true)
|
|
|
|
@$('input[type="password"]').attr('disabled', true)
|
|
|
|
@$('#gplus-logged-in-row, #social-network-signups').toggleClass('hide')
|
|
|
|
|
|
|
|
|
|
|
|
# Facebook signup
|
|
|
|
|
|
|
|
onClickFacebookSignupButton: ->
|
|
|
|
btn = @$('#facebook-signup-btn')
|
|
|
|
btn.attr('disabled', true)
|
|
|
|
application.facebookHandler.loadAPI({
|
|
|
|
success: =>
|
|
|
|
btn.attr('disabled', false)
|
|
|
|
application.facebookHandler.connect({
|
|
|
|
success: =>
|
|
|
|
btn.find('.sign-in-blurb').text($.i18n.t('signup.creating'))
|
|
|
|
btn.attr('disabled', true)
|
|
|
|
application.facebookHandler.loadPerson({
|
|
|
|
success: (@facebookAttrs) =>
|
|
|
|
existingUser = new User()
|
|
|
|
existingUser.fetchFacebookUser(@facebookAttrs.facebookID, {
|
|
|
|
error: (user, jqxhr) =>
|
|
|
|
if jqxhr.status is 404
|
|
|
|
@onFacebookConnected()
|
|
|
|
else
|
|
|
|
errors.showNotyNetworkError(jqxhr)
|
|
|
|
success: =>
|
|
|
|
me.loginFacebookUser(@facebookAttrs.facebookID, {
|
|
|
|
success: ->
|
2016-04-19 13:20:56 -04:00
|
|
|
application.router.navigate('/teachers/update-account', {trigger: true})
|
2016-03-09 17:40:52 -05:00
|
|
|
error: errors.showNotyNetworkError
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
onFacebookConnected: ->
|
2016-04-19 13:20:56 -04:00
|
|
|
@formChanged = true
|
2016-03-09 17:40:52 -05:00
|
|
|
forms.objectToForm(@$('form'), @facebookAttrs)
|
|
|
|
for field in ['email', 'firstName', 'lastName']
|
|
|
|
input = @$("input[name='#{field}']")
|
|
|
|
if input.val()
|
|
|
|
input.attr('disabled', true)
|
|
|
|
@$('input[type="password"]').attr('disabled', true)
|
|
|
|
@$('#facebook-logged-in-row, #social-network-signups').toggleClass('hide')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
formSchema = {
|
|
|
|
type: 'object'
|
2016-04-19 13:20:56 -04:00
|
|
|
required: [
|
|
|
|
'firstName', 'lastName', 'email', 'organization', 'role', 'numStudents', 'city'
|
|
|
|
'state', 'country'
|
|
|
|
]
|
2016-03-09 17:40:52 -05:00
|
|
|
properties:
|
|
|
|
password1: { type: 'string' }
|
|
|
|
password2: { type: 'string' }
|
|
|
|
firstName: { type: 'string' }
|
|
|
|
lastName: { type: 'string' }
|
|
|
|
name: { type: 'string', minLength: 1 }
|
|
|
|
email: { type: 'string', format: 'email' }
|
|
|
|
phoneNumber: { type: 'string' }
|
|
|
|
role: { type: 'string' }
|
|
|
|
organization: { type: 'string' }
|
|
|
|
city: { type: 'string' }
|
|
|
|
state: { type: 'string' }
|
|
|
|
country: { type: 'string' }
|
|
|
|
numStudents: { type: 'string' }
|
2016-04-19 13:20:56 -04:00
|
|
|
numStudentsTotal: { type: 'string' }
|
2016-03-09 17:40:52 -05:00
|
|
|
educationLevel: {
|
|
|
|
type: 'array'
|
|
|
|
items: { type: 'string' }
|
|
|
|
}
|
|
|
|
notes: { type: 'string' }
|
|
|
|
}
|
2016-04-20 13:29:59 -04:00
|
|
|
|
|
|
|
for key in NCES_KEYS
|
|
|
|
formSchema['nces_' + key] = type: 'string'
|