<commit_before>Promise = require 'bluebird-q'
{ stringify } = require 'qs'
{ invoke, pick, last } = require 'underscore'
{ View, Model } = require 'backbone'
{ isPhoneLike } = require '../../../../../components/util/device.coffee'
analyticsHooks = require '../../../../../lib/analytics_hooks.coffee'
Form = require '../../../../../components/form/index.coffee'
LocationSearchView = require '../../../../../components/location_search/index.coffee'
confirmation = require '../../../../../components/confirmation/index.coffee'
templates =
  page: -> require('../templates/page.jade') arguments...
  profession: -> require('../templates/profession.jade') arguments...

module.exports = class ProfessionalBuyerCompleteView extends View
  events:
    'change .js-profession select': 'setProfession'
    'click button': 'submit'

  initialize: ({ @user }) ->
    @listenTo @user, 'change:profession', @renderProfession

  setProfession: (e) ->
    @user.set 'profession', $(e.currentTarget).val()

  renderProfession: ->
    @$('.js-profession')
      .html templates.profession user: @user

  submit: (e) ->
    e.preventDefault()

    form = new Form $form: @$('form')
    return unless form.isReady()

    form.state 'loading'

    data = form.data location: @user.related().location.toJSON()

    if data.price_range?
      [ data.price_range_min = min
        data.price_range_max = max ] = data.price_range.split ':'

    Promise
      .all [
        @user.save pick data, [
          'profession'
          'location'
          'price_range_min'
          'price_range_max'
        ]...

        @user.related().collectorProfile.save pick data, [
          'professional_buyer'
          'company_website'
          'company_name'
        ]...<commit_msg>Make `company_website` pass validation if the field is left the default value<commit_after>Promise = require 'bluebird-q'
{ stringify } = require 'qs'
{ invoke, pick, last } = require 'underscore'
{ View, Model } = require 'backbone'
{ isPhoneLike } = require '../../../../../components/util/device.coffee'
analyticsHooks = require '../../../../../lib/analytics_hooks.coffee'
Form = require '../../../../../components/form/index.coffee'
LocationSearchView = require '../../../../../components/location_search/index.coffee'
confirmation = require '../../../../../components/confirmation/index.coffee'
templates =
  page: -> require('../templates/page.jade') arguments...
  profession: -> require('../templates/profession.jade') arguments...

module.exports = class ProfessionalBuyerCompleteView extends View
  events:
    'change .js-profession select': 'setProfession'
    'click button': 'submit'

  initialize: ({ @user }) ->
    @listenTo @user, 'change:profession', @renderProfession

  setProfession: (e) ->
    @user.set 'profession', $(e.currentTarget).val()

  renderProfession: ->
    @$('.js-profession')
      .html templates.profession user: @user

  submit: (e) ->
    e.preventDefault()

    # If the URL is the default value; remove
    # the http:// so that it passes validation
    if ($url = @$('input[name="company_website"]')).val() is 'http://'
      $url.val ''

    form = new Form $form: @$('form')
    return unless form.isReady()

    form.state 'loading'

    data = form.data location: @user.related().location.toJSON()

    if data.price_range?
      [ data.price_range_min = min
        data.price_range_max = max ] = data.price_range.split ':'

    Promise
      .all [
        @user.save pick data, [
          'profession'
          'location'
          'price_range_min'
          'price_range_max'
        ]...

        @user.related().collectorProfile.save pick data, [
          'professional_buyer'
          'company_website'
          'company_name'
        ]...
{'fraction_non_alphanumeric': 0.17380889007186584, 'fraction_numerical': 0.0, 'mean_word_length': 2.599616858237548, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6098444', 'n_tokens_mistral': 1148, 'n_tokens_neox': 1111, 'n_words': 244}