RootView = require 'views/core/RootView' forms = require 'core/forms' TrialRequest = require 'models/TrialRequest' TrialRequests = require 'collections/TrialRequests' AuthModal = require 'views/core/AuthModal' storage = require 'core/storage' errors = require 'core/errors' ConfirmModal = require 'views/editor/modal/ConfirmModal' FORM_KEY = 'request-quote-form' SIGNUP_REDIRECT = '/teachers' module.exports = class RequestQuoteView extends RootView id: 'request-quote-view' template: require 'templates/teachers/request-quote-view' logoutRedirectURL: null events: 'change #request-form': 'onChangeRequestForm' 'submit #request-form': 'onSubmitRequestForm' 'click #email-exists-login-link': 'onClickEmailExistsLoginLink' 'submit #signup-form': 'onSubmitSignupForm' 'click #logout-link': -> me.logout() 'click #gplus-signup-btn': 'onClickGPlusSignupButton' 'click #facebook-signup-btn': 'onClickFacebookSignupButton' initialize: -> @trialRequest = new TrialRequest() @trialRequests = new TrialRequests() @trialRequests.fetchOwn() @supermodel.trackCollection(@trialRequests) onLoaded: -> if @trialRequests.size() @trialRequest = @trialRequests.first() 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'] super() afterRender: -> super() # apply existing trial request on form properties = @trialRequest.get('properties') if properties forms.objectToForm(@$('#request-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) # apply changes from local storage obj = storage.load(FORM_KEY) if obj @$('#other-education-level-checkbox').attr('checked', obj.otherChecked) @$('#other-education-level-input').val(obj.otherInput) forms.objectToForm(@$('#request-form'), obj, { overwriteExisting: true }) onChangeRequestForm: -> # save changes to local storage obj = forms.formToObject(@$('form')) obj.otherChecked = @$('#other-education-level-checkbox').is(':checked') obj.otherInput = @$('#other-education-level-input').val() storage.save(FORM_KEY, obj, 10) onSubmitRequestForm: (e) -> e.preventDefault() form = @$('#request-form') attrs = forms.formToObject(form) # custom other input logic (also used in form local storage save/restore) if @$('#other-education-level-checkbox').is(':checked') val = @$('#other-education-level-input').val() attrs.educationLevel.push(val) if val forms.clearFormAlerts(form) requestFormSchema = if me.isAnonymous() then requestFormSchemaAnonymous else requestFormSchemaLoggedIn result = tv4.validateMultiple(attrs, requestFormSchemaAnonymous) error = false if not result.valid forms.applyErrorsToForm(form, result.errors) error = true if not forms.validateEmail(attrs.email) forms.setErrorToProperty(form, 'email', 'Invalid email.') error = true if not _.size(attrs.educationLevel) forms.setErrorToProperty(form, 'educationLevel', 'Include at least one.') error = true if error forms.scrollToFirstError() return attrs['siteOrigin'] = 'demo request' @trialRequest = new TrialRequest({ type: 'course' properties: attrs }) if me.get('role') is 'student' and not me.isAnonymous() modal = new ConfirmModal({ title: '' body: "
#{$.i18n.t('teachers_quote.conversion_warning')}
#{$.i18n.t('teachers_quote.learn_more_modal')}
" confirm: $.i18n.t('common.continue') decline: $.i18n.t('common.cancel') }) @openModalView(modal) modal.once 'confirm', @saveTrialRequest, @ else @saveTrialRequest() saveTrialRequest: -> @trialRequest.notyErrors = false @$('#submit-request-btn').text('Sending').attr('disabled', true) @trialRequest.save() @trialRequest.on 'sync', @onTrialRequestSubmit, @ @trialRequest.on 'error', @onTrialRequestError, @ onTrialRequestError: (model, jqxhr) -> @$('#submit-request-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($("