2016-05-09 18:16:54 -04:00
|
|
|
ModalView = require 'views/core/ModalView'
|
|
|
|
State = require 'models/State'
|
|
|
|
TrialRequests = require 'collections/TrialRequests'
|
|
|
|
forms = require 'core/forms'
|
|
|
|
contact = require 'core/contact'
|
|
|
|
|
|
|
|
module.exports = class TeachersContactModal extends ModalView
|
|
|
|
id: 'teachers-contact-modal'
|
|
|
|
template: require 'templates/teachers/teachers-contact-modal'
|
2016-06-17 18:40:03 -04:00
|
|
|
|
2016-05-09 18:16:54 -04:00
|
|
|
events:
|
|
|
|
'submit form': 'onSubmitForm'
|
2016-06-17 18:40:03 -04:00
|
|
|
|
2016-05-09 18:16:54 -04:00
|
|
|
initialize: (options={}) ->
|
|
|
|
@state = new State({
|
|
|
|
formValues: {
|
2016-06-17 18:40:03 -04:00
|
|
|
name: ''
|
2016-05-09 18:16:54 -04:00
|
|
|
email: ''
|
2016-06-26 17:49:09 -04:00
|
|
|
licensesNeeded: ''
|
2016-05-09 18:16:54 -04:00
|
|
|
message: ''
|
|
|
|
}
|
|
|
|
formErrors: {}
|
|
|
|
sendingState: 'standby' # 'sending', 'sent', 'error'
|
|
|
|
})
|
|
|
|
@trialRequests = new TrialRequests()
|
|
|
|
@supermodel.trackRequest @trialRequests.fetchOwn()
|
|
|
|
@state.on 'change', @render, @
|
|
|
|
|
|
|
|
onLoaded: ->
|
|
|
|
trialRequest = @trialRequests.first()
|
|
|
|
props = trialRequest?.get('properties') or {}
|
2016-06-17 18:40:03 -04:00
|
|
|
name = if props.firstName and props.lastName then "#{props.firstName} #{props.lastName}" else me.get('name') ? ''
|
|
|
|
email = props.email or me.get('email') or ''
|
2016-05-09 18:16:54 -04:00
|
|
|
message = """
|
2016-06-17 18:40:03 -04:00
|
|
|
Hi CodeCombat! I want to learn more about the Classroom experience and get licenses so that my students can access Computer Science 2 and on.
|
|
|
|
|
2016-05-09 18:16:54 -04:00
|
|
|
Name of School/District: #{props.organization or ''}
|
2016-06-17 18:40:03 -04:00
|
|
|
Role: #{props.role or ''}
|
|
|
|
Phone Number: #{props.phoneNumber or ''}
|
2016-05-09 18:16:54 -04:00
|
|
|
"""
|
2016-06-26 17:49:09 -04:00
|
|
|
@state.set('formValues', { name, email, message })
|
2016-05-09 18:16:54 -04:00
|
|
|
super()
|
|
|
|
|
|
|
|
onSubmitForm: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
return if @state.get('sendingState') is 'sending'
|
2016-06-17 18:40:03 -04:00
|
|
|
|
2016-05-09 18:16:54 -04:00
|
|
|
formValues = forms.formToObject @$el
|
|
|
|
@state.set('formValues', formValues)
|
2016-06-17 18:40:03 -04:00
|
|
|
|
2016-05-09 18:16:54 -04:00
|
|
|
formErrors = {}
|
2016-06-17 18:40:03 -04:00
|
|
|
unless formValues.name
|
|
|
|
formErrors.name = 'Name required.'
|
|
|
|
unless forms.validateEmail(formValues.email)
|
2016-05-09 18:16:54 -04:00
|
|
|
formErrors.email = 'Invalid email.'
|
2016-06-17 18:40:03 -04:00
|
|
|
unless parseInt(formValues.licensesNeeded) > 0
|
|
|
|
formErrors.licensesNeeded = 'Licenses needed is required.'
|
|
|
|
unless formValues.message
|
2016-05-09 18:16:54 -04:00
|
|
|
formErrors.message = 'Message required.'
|
|
|
|
@state.set({ formErrors, formValues, sendingState: 'standby' })
|
|
|
|
return unless _.isEmpty(formErrors)
|
2016-06-17 18:40:03 -04:00
|
|
|
|
2016-05-09 18:16:54 -04:00
|
|
|
@state.set('sendingState', 'sending')
|
2016-06-23 09:12:51 -04:00
|
|
|
data = _.extend({ country: me.get('country') }, formValues)
|
2016-05-09 18:16:54 -04:00
|
|
|
contact.send({
|
|
|
|
data
|
|
|
|
context: @
|
2016-05-23 13:26:34 -04:00
|
|
|
success: ->
|
|
|
|
@state.set({ sendingState: 'sent' })
|
|
|
|
me.set('enrollmentRequestSent', true)
|
2016-06-17 18:40:03 -04:00
|
|
|
setTimeout(=>
|
2016-05-23 13:26:34 -04:00
|
|
|
@hide?()
|
|
|
|
, 3000)
|
2016-05-09 18:16:54 -04:00
|
|
|
error: -> @state.set({ sendingState: 'error' })
|
|
|
|
})
|
|
|
|
|