2016-01-21 17:41:57 -05:00
|
|
|
RootView = require 'views/core/RootView'
|
|
|
|
forms = require 'core/forms'
|
|
|
|
TrialRequest = require 'models/TrialRequest'
|
|
|
|
TrialRequests = require 'collections/TrialRequests'
|
|
|
|
AuthModal = require 'views/core/AuthModal'
|
2016-02-25 18:24:16 -05:00
|
|
|
CreateAccountModal = require 'views/core/CreateAccountModal'
|
2016-02-16 17:58:47 -05:00
|
|
|
storage = require 'core/storage'
|
2016-01-21 17:41:57 -05:00
|
|
|
|
|
|
|
formSchema = {
|
|
|
|
type: 'object'
|
|
|
|
required: ['name', 'email', 'organization', 'role', 'numStudents']
|
|
|
|
properties:
|
|
|
|
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' }
|
|
|
|
educationLevel: {
|
|
|
|
type: 'array'
|
|
|
|
items: { type: 'string' }
|
|
|
|
}
|
|
|
|
notes: { type: 'string' }
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = class RequestQuoteView extends RootView
|
|
|
|
id: 'request-quote-view'
|
|
|
|
template: require 'templates/request-quote-view'
|
2016-02-02 15:48:19 -05:00
|
|
|
|
2016-01-21 17:41:57 -05:00
|
|
|
events:
|
2016-02-16 17:58:47 -05:00
|
|
|
'change form': 'onChangeForm'
|
2016-01-21 17:41:57 -05:00
|
|
|
'submit form': 'onSubmitForm'
|
|
|
|
'click #login-btn': 'onClickLoginButton'
|
|
|
|
'click #signup-btn': 'onClickSignupButton'
|
2016-02-16 17:58:47 -05:00
|
|
|
'click #email-exists-login-link': 'onClickEmailExistsLoginLink'
|
2016-02-02 15:48:19 -05:00
|
|
|
|
2016-01-21 17:41:57 -05:00
|
|
|
initialize: ->
|
|
|
|
@trialRequest = new TrialRequest()
|
|
|
|
@trialRequests = new TrialRequests()
|
|
|
|
@trialRequests.fetchOwn()
|
|
|
|
@supermodel.loadCollection(@trialRequests)
|
2016-02-02 15:48:19 -05:00
|
|
|
|
2016-01-21 17:41:57 -05:00
|
|
|
onLoaded: ->
|
|
|
|
if @trialRequests.size()
|
|
|
|
@trialRequest = @trialRequests.first()
|
2016-02-05 13:49:46 -05:00
|
|
|
if @trialRequest and @trialRequest.get('status') isnt 'submitted' and @trialRequest.get('status') isnt 'approved'
|
|
|
|
window.tracker?.trackEvent 'View Trial Request', category: 'Teachers', label: 'View Trial Request', ['Mixpanel']
|
2016-01-21 17:41:57 -05:00
|
|
|
super()
|
2016-02-16 17:58:47 -05:00
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
obj = storage.load('request-quote-form')
|
|
|
|
if obj
|
|
|
|
@$('#other-education-level-checkbox').attr('checked', obj.otherChecked)
|
|
|
|
@$('#other-education-level-input').val(obj.otherInput)
|
|
|
|
forms.objectToForm(@$('form'), obj)
|
|
|
|
|
|
|
|
onChangeForm: ->
|
|
|
|
obj = forms.formToObject(@$('form'))
|
|
|
|
obj.otherChecked = @$('#other-education-level-checkbox').is(':checked')
|
|
|
|
obj.otherInput = @$('#other-education-level-input').val()
|
|
|
|
storage.save('request-quote-form', obj, 10)
|
2016-02-02 15:48:19 -05:00
|
|
|
|
2016-01-21 17:41:57 -05:00
|
|
|
onSubmitForm: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
form = @$('form')
|
|
|
|
attrs = forms.formToObject(form)
|
2016-02-16 17:58:47 -05:00
|
|
|
|
|
|
|
# custom other input logic (also used in form local storage save/restore)
|
2016-01-21 17:41:57 -05:00
|
|
|
if @$('#other-education-level-checkbox').is(':checked')
|
|
|
|
attrs.educationLevel.push(@$('#other-education-level-input').val())
|
2016-02-16 17:58:47 -05:00
|
|
|
|
2016-01-21 17:41:57 -05:00
|
|
|
forms.clearFormAlerts(form)
|
|
|
|
result = tv4.validateMultiple(attrs, formSchema)
|
2016-02-01 18:51:29 -05:00
|
|
|
error = true
|
2016-01-21 17:41:57 -05:00
|
|
|
if not result.valid
|
2016-02-01 18:51:29 -05:00
|
|
|
forms.applyErrorsToForm(form, result.errors)
|
|
|
|
else if not /^.+@.+\..+$/.test(attrs.email)
|
|
|
|
forms.setErrorToProperty(form, 'email', 'Invalid email.')
|
|
|
|
else if not _.size(attrs.educationLevel)
|
2016-01-21 17:41:57 -05:00
|
|
|
return forms.setErrorToProperty(form, 'educationLevel', 'Check at least one.')
|
2016-02-01 18:51:29 -05:00
|
|
|
else
|
|
|
|
error = false
|
|
|
|
if error
|
|
|
|
forms.scrollToFirstError()
|
|
|
|
return
|
2016-01-21 17:41:57 -05:00
|
|
|
@trialRequest = new TrialRequest({
|
|
|
|
type: 'course'
|
|
|
|
properties: attrs
|
|
|
|
})
|
2016-02-16 17:58:47 -05:00
|
|
|
@trialRequest.notyErrors = false
|
2016-01-21 17:41:57 -05:00
|
|
|
@$('#submit-request-btn').text('Sending').attr('disabled', true)
|
|
|
|
@trialRequest.save()
|
|
|
|
@trialRequest.on 'sync', @onTrialRequestSubmit, @
|
|
|
|
@trialRequest.on 'error', @onTrialRequestError, @
|
2016-02-02 15:48:19 -05:00
|
|
|
me.setRole attrs.role.toLowerCase(), true
|
2016-01-21 17:41:57 -05:00
|
|
|
|
2016-02-16 17:58:47 -05:00
|
|
|
onTrialRequestError: (model, jqxhr) ->
|
|
|
|
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 id='email-exists-login-link'>#{logIn}</a>"))
|
2016-01-21 17:41:57 -05:00
|
|
|
@$('#submit-request-btn').text('Submit').attr('disabled', false)
|
2016-02-22 19:37:41 -05:00
|
|
|
forms.scrollToFirstError()
|
2016-01-21 17:41:57 -05:00
|
|
|
|
2016-02-16 17:58:47 -05:00
|
|
|
onClickEmailExistsLoginLink: ->
|
2016-02-25 18:24:16 -05:00
|
|
|
modal = new AuthModal({ initialValues: { email: @trialRequest.get('properties')?.email } })
|
2016-02-16 17:58:47 -05:00
|
|
|
@openModalView(modal)
|
|
|
|
|
2016-01-21 17:41:57 -05:00
|
|
|
onTrialRequestSubmit: ->
|
|
|
|
@$('form, #form-submit-success').toggleClass('hide')
|
2016-02-05 13:49:46 -05:00
|
|
|
window.tracker?.trackEvent 'Submit Trial Request', category: 'Teachers', label: 'Trial Request', ['Mixpanel']
|
2016-01-21 17:41:57 -05:00
|
|
|
|
|
|
|
onClickLoginButton: ->
|
2016-02-25 18:24:16 -05:00
|
|
|
modal = new AuthModal({ initialValues: { email: @trialRequest.get('properties')?.email } })
|
2016-01-21 17:41:57 -05:00
|
|
|
@openModalView(modal)
|
|
|
|
window.nextURL = '/courses/teachers' unless @trialRequest.isNew()
|
2016-02-02 15:48:19 -05:00
|
|
|
|
2016-01-21 17:41:57 -05:00
|
|
|
onClickSignupButton: ->
|
|
|
|
props = @trialRequest.get('properties') or {}
|
|
|
|
me.set('name', props.name)
|
2016-02-25 18:24:16 -05:00
|
|
|
modal = new CreateAccountModal({
|
2016-02-02 15:48:19 -05:00
|
|
|
initialValues: {
|
2016-01-21 17:41:57 -05:00
|
|
|
email: props.email
|
|
|
|
schoolName: props.organization
|
|
|
|
}
|
|
|
|
})
|
|
|
|
@openModalView(modal)
|
2016-02-02 15:48:19 -05:00
|
|
|
window.nextURL = '/courses/teachers' unless @trialRequest.isNew()
|