<commit_before>Backbone = require 'backbone'
sd = require('sharify').data
Q = require 'q'
{ map } = require 'underscore'
qlimit = require 'qlimit'
Block = require '../../../../models/block.coffee'
Blocks = require '../../../../collections/blocks.coffee'
Channel = require '../../../../models/channel.coffee'
SearchBlocks = require '../../../../collections/search_blocks.coffee'
ImportSelectChannelView = require './import_select_channel_view.coffee'

template =-> require('./templates/import_status.jade') arguments...
limit = qlimit(1)

class Status extends Backbone.Model

module.exports = class ImportStatusView extends Backbone.View
  events:
    'click .import__status__inner__connect' : 'initConnectBlocks'

  initialize: ->    
    @selectedChannel = new Channel()
    @selectedChannel.url = "#{sd.API_URL}/channels/"
    @status = new Status 
      selecting: false
      connecting: false
    @search = new SearchBlocks()
    @search.url = "#{sd.API_URL}/user/#{sd.CURRENT_USER.slug}/search"
    @blocks = new Blocks()

    @listenTo @collection, 'model:selected', @render
    @listenTo @status, 'change:connecting', @render

  initConnectBlocks: ->
    @status.set connecting: true
    blocks = @collection.filter('selected')
    @maybeCreateChannel()
      .then @createBlocks
      .then @showSuccess
      .catch @showError

  maybeCreateChannel: ->
    dfd = Q.defer()
    if @selectedChannel.get('is_new')
      @selectedChannel.save null, success: => dfd.resolve()
    else
      dfd.resolve()
    dfd.promise

  createBlocks: =>
    Q.all map @collection.filter('selected'), limit @createBlockFromLink
    .catch @showError

  createBlockFromLink: (link) =>
    dfd = Q.defer()
    blocks = @blocks
    selectedChannel = @selectedChannel
    block = new Block source: link.get('href')
    blocks.create block.toJSON(),
      url: "#{sd.API_URL}/channels/#{selectedChannel.get('slug')}/blocks"
      success: (block) ->
        link.set selected: false
        link.collection.trigger 'model:selected'
        dfd.resolve()
    dfd.promise
      

  showSuccess: ->
    console.log('success')

  showError: (e) ->
    console.log('error', e, e.stack)

  render: ->
    @$el.html template
      collection: @collection
      status: @status
      selectedChannel: @selectedChannel

    @_postRender()
  
  _postRender: ->
    new ImportSelectChannelView
      el: @$('.import__status__inner__select')
      model: @selectedChannel
      status: @status
      search: @search
    .render()

<commit_msg>Update status on any change<commit_after>Backbone = require 'backbone'
sd = require('sharify').data
Q = require 'q'
{ map } = require 'underscore'
qlimit = require 'qlimit'
Block = require '../../../../models/block.coffee'
Blocks = require '../../../../collections/blocks.coffee'
Channel = require '../../../../models/channel.coffee'
SearchBlocks = require '../../../../collections/search_blocks.coffee'
ImportSelectChannelView = require './import_select_channel_view.coffee'

template =-> require('./templates/import_status.jade') arguments...
limit = qlimit(1)

class Status extends Backbone.Model

module.exports = class ImportStatusView extends Backbone.View
  events:
    'click .import__status__inner__connect' : 'initConnectBlocks'

  initialize: ->    
    @selectedChannel = new Channel()
    @selectedChannel.url = "#{sd.API_URL}/channels/"
    @status = new Status 
      selecting: false
      connecting: false
    @search = new SearchBlocks()
    @search.url = "#{sd.API_URL}/user/#{sd.CURRENT_USER.slug}/search"
    @blocks = new Blocks()

    @listenTo @collection, 'model:selected', @render
    @listenTo @status, 'change', @render

  initConnectBlocks: ->
    @status.set connecting: true
    blocks = @collection.filter('selected')
    @maybeCreateChannel()
      .then @createBlocks
      .then @showSuccess
      .catch @showError

  maybeCreateChannel: ->
    dfd = Q.defer()
    if @selectedChannel.get('is_new')
      @selectedChannel.save null, success: => dfd.resolve()
    else
      dfd.resolve()
    dfd.promise

  createBlocks: =>
    Q.all map @collection.filter('selected'), limit @createBlockFromLink
    .catch @showError

  createBlockFromLink: (link) =>
    dfd = Q.defer()
    blocks = @blocks
    selectedChannel = @selectedChannel
    block = new Block source: link.get('href')
    existingConnections = link.get('connections') or []
    blocks.create block.toJSON(),
      url: "#{sd.API_URL}/channels/#{selectedChannel.get('slug')}/blocks"
      success: (block) ->
        connections = existingConnections.concat [
          {
            title: selectedChannel.get('title')
            status: selectedChannel.get('status')
            slug: selectedChannel.get('slug')
            user_slug: selectedChannel.get('user').slug
          }
        ]
        link.set 
          selected: false
          connections: connections
        link.collection.trigger 'model:selected'
        dfd.resolve()
    dfd.promise
      
  showSuccess: =>
    @status.set
      selecting: false
      connecting: false
    @selectedChannel.reset()

  showError: (e) ->
    console.log('error', e, e.stack)

  render: ->
    @$el.html template
      collection: @collection
      status: @status
      selectedChannel: @selectedChannel

    @_postRender()
  
  _postRender: ->
    new ImportSelectChannelView
      el: @$('.import__status__inner__select')
      model: @selectedChannel
      status: @status
      search: @search
    .render()


{'fraction_non_alphanumeric': 0.14067055393586006, 'fraction_numerical': 0.00036443148688046647, 'mean_word_length': 2.9291338582677167, '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': '1792128', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1605, 'n_words': 352}