<commit_before>_ = require 'underscore'
Backbone = require 'backbone'
moment = require 'moment'
Sales = require '../../collections/sales.coffee'
Artwork = require '../../models/artwork.coffee'
SaleArtworks = require '../../collections/sale_artworks.coffee'
{ API_URL } = require('sharify').data
Cookies = require 'cookies-js'
analytics = require '../../lib/analytics.coffee'
track = analytics.track
auctionTemplate = -> require('./template.jade') arguments...
Sale = require '../../models/sale.coffee'
AuctionClock = require './clock/view.coffee'

class AuctionReminderModal extends Backbone.View

  events: ->
    'click .modal-close': 'close'
    'click .auction-reminder-container': 'clickAuctionReminder'

  initialize: ({ @auctionName, @auctionId, @auctionImage, @auctionEndat }) ->

    # Reminder doesn't show on auction page
    if window.location.pathname == @auctionId
      return
    # Reminder only shows if 24 hours until end
    diff = moment(@auctionEndat).diff(moment(),'hours')
    if diff > 23
      return

    @calculateOffsetEndAtMoment (calculatedOffsetEndAtMoment) =>
      # Reminder only shows if there are at least 10 seconds remaining
      if moment.duration(moment(calculatedOffsetEndAtMoment).diff(moment()))._milliseconds > 10000
        @offsetEndAtMoment = moment(calculatedOffsetEndAtMoment)
        @open()

    @$container = $('body')

  open: =>
    @$el.
      addClass("auction-reminder-modal").
      html auctionTemplate
        auctionId: @auctionId
        auctionImage: @auctionImage
        auctionName: @auctionName
    @$dialog = @$('.modal-dialog')
    @setupClock()
    @$container.append @$el

    $('.auction-reminder-clock').on 'auctionClosed', ->
      $('.modal-dialog').removeClass('is-spring-in')
      $('.modal-dialog').removeClass('is-static-open')
      $('.modal-dialog').addClass('is-close-out')

    # Transition based on if they've seen the reminder already
    if Cookies.get('firstAuctionReminderSeen')
      @$('.modal-dialog').addClass('is-static-open')
    else
      cookieValue = "#{@auctionName}|#{@auctionId}|#{@auctionImage}|#{@auctionEndat}"
      cookieExpiration = moment.duration(moment(@offsetEndAtMoment).diff(moment()))._milliseconds
      Cookies.set('firstAuctionReminderSeen', cookieValue, { expires: cookieExpiration })
      activate = => @$dialog.addClass("is-spring-in")
      _.delay(activate,5000)

  close: (cb) ->
    @$('.modal-dialog').removeClass('is-spring-in')
    $('.modal-dialog').removeClass('is-static-open')
    @$('.modal-dialog').addClass('is-close-out')

    Cookies.set('closeAuctionReminder', true)
    track.click 'Closed Auction Reminder'
    analytics.snowplowStruct 'auction_reminder', 'dismiss', @auctionId, 'feature'

  calculateOffsetEndAtMoment: (callback) ->
    new Backbone.Model().
      fetch
        url: "#{API_URL}/api/v1/system/time"
        success: (model) =>
          offset = moment().diff(model.get('time'))
          endat = moment(@auctionEndat).add(offset)<commit_msg>Finally silence moment deprecation warning<commit_after>_ = require 'underscore'
Backbone = require 'backbone'
moment = require 'moment'
Sales = require '../../collections/sales.coffee'
Artwork = require '../../models/artwork.coffee'
SaleArtworks = require '../../collections/sale_artworks.coffee'
{ API_URL } = require('sharify').data
Cookies = require 'cookies-js'
analytics = require '../../lib/analytics.coffee'
track = analytics.track
auctionTemplate = -> require('./template.jade') arguments...
Sale = require '../../models/sale.coffee'
AuctionClock = require './clock/view.coffee'

class AuctionReminderModal extends Backbone.View

  events: ->
    'click .modal-close': 'close'
    'click .auction-reminder-container': 'clickAuctionReminder'

  initialize: ({ @auctionName, @auctionId, @auctionImage, @auctionEndat }) ->

    # Reminder doesn't show on auction page
    if window.location.pathname == @auctionId
      return
    # Reminder only shows if 24 hours until end
    diff = moment(@auctionEndat).diff(moment(),'hours')
    if diff > 23
      return

    @calculateOffsetEndAtMoment (calculatedOffsetEndAtMoment) =>
      # Reminder only shows if there are at least 10 seconds remaining
      if moment.duration(moment(calculatedOffsetEndAtMoment).diff(moment()))._milliseconds > 10000
        @offsetEndAtMoment = moment(calculatedOffsetEndAtMoment)
        @open()

    @$container = $('body')

  open: =>
    @$el.
      addClass("auction-reminder-modal").
      html auctionTemplate
        auctionId: @auctionId
        auctionImage: @auctionImage
        auctionName: @auctionName
    @$dialog = @$('.modal-dialog')
    @setupClock()
    @$container.append @$el

    $('.auction-reminder-clock').on 'auctionClosed', ->
      $('.modal-dialog').removeClass('is-spring-in')
      $('.modal-dialog').removeClass('is-static-open')
      $('.modal-dialog').addClass('is-close-out')

    # Transition based on if they've seen the reminder already
    if Cookies.get('firstAuctionReminderSeen')
      @$('.modal-dialog').addClass('is-static-open')
    else
      cookieValue = "#{@auctionName}|#{@auctionId}|#{@auctionImage}|#{@auctionEndat}"
      cookieExpiration = moment.duration(moment(@offsetEndAtMoment).diff(moment()))._milliseconds
      Cookies.set('firstAuctionReminderSeen', cookieValue, { expires: cookieExpiration })
      activate = => @$dialog.addClass("is-spring-in")
      _.delay(activate,5000)

  close: (cb) ->
    @$('.modal-dialog').removeClass('is-spring-in')
    $('.modal-dialog').removeClass('is-static-open')
    @$('.modal-dialog').addClass('is-close-out')

    Cookies.set('closeAuctionReminder', true)
    track.click 'Closed Auction Reminder'
    analytics.snowplowStruct 'auction_reminder', 'dismiss', @auctionId, 'feature'

  calculateOffsetEndAtMoment: (callback) ->
    new Backbone.Model().
      fetch
        url: "#{API_URL}/api/v1/system/time"
        success: (model) =>
          offset = moment().diff(model.get('iso8601'))
          endat = moment(@auctionEndat).add(offset)
{'fraction_non_alphanumeric': 0.1576681020205366, 'fraction_numerical': 0.0059622391520370984, 'mean_word_length': 3.6705336426914155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '6600125', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1854, 'n_words': 347}