<commit_before>{ defer, extend, before, isEqual } = require 'underscore'
Backbone = require 'backbone'
{ AUCTION, CURRENT_USER } = require('sharify').data
Form = require '../../../../components/form/index.coffee'
openMultiPageModal = require '../../../../components/multi_page_modal/index.coffee'
openBuyersPremiumModal = require './components/buyers_premium/index.coffee'
mediator = require '../../../../lib/mediator.coffee'
inquire = require '../../lib/inquire.coffee'
acquire = require '../../lib/acquire.coffee'
helpers = require './helpers.coffee'
metaphysics = require '../../../../../lib/metaphysics.coffee'
Serializer = require '../../../../components/form/serializer.coffee'
CurrentUser = require '../../../../models/current_user.coffee'
template = -> require('./templates/index.jade') arguments...

LOT_STANDING_MAX_POLLS = 10
LOT_STANDING_POLL_INTERVAL = 1000

module.exports = class ArtworkAuctionView extends Backbone.View
  className: 'artwork-auction'

  events:
    'click .js-artwork-auction-buyers-premium': 'openBuyersPremium'
    'click .js-artwork-auction-bid-button': 'submit'
    'click .js-artwork-auction-help-modal': 'openHelpModal'
    'click .js-artwork-auction-ask-specialist': 'inquire'
    'click .js-artwork-auction-buy-now': 'acquire'
    'change .js-artwork-auction-max-bid': 'setMaxBid'

  initialize: ({ @data }) -> #
    # Defer until next tick. This really just gets the sequence right for
    # testing to work. This doesn't feel like a great justification, though.
    setTimeout (=> @updateBidLabel LOT_STANDING_MAX_POLLS), 0

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

    openBuyersPremiumModal AUCTION.id

  parseBid: (amount = '') ->
    [dollars, cents] = String amount
      .split '.'

    dollars = dollars.replace /\D/g,''

    (parseInt(dollars) * 100) + parseInt(cents or 0)

  validBid: (cents) ->
    cents >= AUCTION.minimum_next_bid.cents

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

    id = $(e.currentTarget).data 'id'

    openMultiPageModal 'auction-faqs', ({ view }) ->
      view.subView.state
        .set 'active', id

  inquire: (e) ->
    e.preventDefault()
    inquire AUCTION.artwork_id

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

    # Show the new buy now flow if you have the lab feature enabled
    loggedInUser = CurrentUser.orNull()
    if loggedInUser?.hasLabFeature('New Buy Now Flow')
      serializer = new Serializer @$('.js-artwork-auction-buy-now-form')
      data = serializer.data()

      createOrder
        artworkId: @artwork.get('_id')
        editionSetId: data.edition_set_id
        quantity: 1
        user: loggedInUser
      .then (data) ->
        order = data?.createOrderWithArtwork?.orderOrError?.order
        location.assign("/order2/#{order.id}/shipping")

    # Legacy purchase flow flow
    else
      $target = $(e.currentTarget)
      $target.attr 'data-state', 'loading'

      acquire AUCTION.artwork_id
        .catch ->
          $target.attr 'data-state', 'error'
          location.reload()

  redirectTo: (path) ->
    location.assign path

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

    form = new Form $form: @$('.js-artwork-auction-bid')

    if not CURRENT_USER?
      return mediator.trigger 'open:auth',
        intent: 'bid'
        signupIntent: 'bid'
        mode: 'signup'
        copy: 'Sign up to bid'<commit_msg>Get artwork from data<commit_after>{ defer, extend, before, isEqual } = require 'underscore'
Backbone = require 'backbone'
{ AUCTION, CURRENT_USER } = require('sharify').data
Form = require '../../../../components/form/index.coffee'
openMultiPageModal = require '../../../../components/multi_page_modal/index.coffee'
openBuyersPremiumModal = require './components/buyers_premium/index.coffee'
mediator = require '../../../../lib/mediator.coffee'
inquire = require '../../lib/inquire.coffee'
acquire = require '../../lib/acquire.coffee'
helpers = require './helpers.coffee'
metaphysics = require '../../../../../lib/metaphysics.coffee'
Serializer = require '../../../../components/form/serializer.coffee'
CurrentUser = require '../../../../models/current_user.coffee'
template = -> require('./templates/index.jade') arguments...

LOT_STANDING_MAX_POLLS = 10
LOT_STANDING_POLL_INTERVAL = 1000

module.exports = class ArtworkAuctionView extends Backbone.View
  className: 'artwork-auction'

  events:
    'click .js-artwork-auction-buyers-premium': 'openBuyersPremium'
    'click .js-artwork-auction-bid-button': 'submit'
    'click .js-artwork-auction-help-modal': 'openHelpModal'
    'click .js-artwork-auction-ask-specialist': 'inquire'
    'click .js-artwork-auction-buy-now': 'acquire'
    'change .js-artwork-auction-max-bid': 'setMaxBid'

  initialize: ({ @data }) -> #
    # Defer until next tick. This really just gets the sequence right for
    # testing to work. This doesn't feel like a great justification, though.
    setTimeout (=> @updateBidLabel LOT_STANDING_MAX_POLLS), 0

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

    openBuyersPremiumModal AUCTION.id

  parseBid: (amount = '') ->
    [dollars, cents] = String amount
      .split '.'

    dollars = dollars.replace /\D/g,''

    (parseInt(dollars) * 100) + parseInt(cents or 0)

  validBid: (cents) ->
    cents >= AUCTION.minimum_next_bid.cents

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

    id = $(e.currentTarget).data 'id'

    openMultiPageModal 'auction-faqs', ({ view }) ->
      view.subView.state
        .set 'active', id

  inquire: (e) ->
    e.preventDefault()
    inquire AUCTION.artwork_id

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

    # Show the new buy now flow if you have the lab feature enabled
    loggedInUser = CurrentUser.orNull()
    if loggedInUser?.hasLabFeature('New Buy Now Flow')
      serializer = new Serializer @$('.js-artwork-auction-buy-now-form')
      data = serializer.data()

      createOrder
        artworkId: @data.artwork.get('_id')
        editionSetId: data.edition_set_id
        quantity: 1
        user: loggedInUser
      .then (data) ->
        order = data?.createOrderWithArtwork?.orderOrError?.order
        location.assign("/order2/#{order.id}/shipping")

    # Legacy purchase flow flow
    else
      $target = $(e.currentTarget)
      $target.attr 'data-state', 'loading'

      acquire AUCTION.artwork_id
        .catch ->
          $target.attr 'data-state', 'error'
          location.reload()

  redirectTo: (path) ->
    location.assign path

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

    form = new Form $form: @$('.js-artwork-auction-bid')

    if not CURRENT_USER?
      return mediator.trigger 'open:auth',
        intent: 'bid'
        signupIntent: 'bid'
        mode: 'signup'
        copy: 'Sign up to bid'
{'fraction_non_alphanumeric': 0.15218363392457893, 'fraction_numerical': 0.0038753912654643017, 'mean_word_length': 2.889855072463768, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '5932778', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2141, 'n_words': 474}