<commit_before>Backbone = require 'backbone'
Promise = require 'bluebird-q'
{ API_URL } = require('sharify').data
Channel = require '../../../../models/channel.coffee'
mediator = require '../../../../lib/mediator.coffee'

module.exports = class ConnectCreateView extends Backbone.View
  className: 'Connect__create'

  events:
    click: 'create'

  initialize: ({ @connectable, @state }) ->
    @listenTo @state, 'change:query', @render

  create: (e) ->
    e.preventDefault()
    e.stopPropagation()

    @$el.text 'Creating'

    title = @state.get 'query'

    channel = new Channel
      title: title
      status: 'private'

    channel.url = "#{API_URL}/channels"

    Promise(channel.save())
      .then =>
        @$el.text 'Connecting'
        @connect channel

      .then =>
        @render()
        channel.set "selected:#{@connectable.get 'base_class'}:#{@connectable.id}", true
        @collection.unshift channel

      .catch =>
        @$el.text 'Error'

  connect: (channel) ->
    Promise $.ajax
      type: 'POST'
      url: "#{API_URL}/channels/#{channel.id}/connections"
      data:
        connectable_id: @connectable.id
        connectable_type: @connectable.get('base_class')

    .then =>
      mediator.shared.recent_connections.shove channel
      mediator.trigger "connection:added:#{@connectable.id}", channel
      mediator.trigger 'connection:added', channel

  render: ->
    @$el.text "+ New private channel “#{@state.get('query')}”"
    this<commit_msg>Set attribute before doing anything with it<commit_after>Backbone = require 'backbone'
Promise = require 'bluebird-q'
{ API_URL } = require('sharify').data
Channel = require '../../../../models/channel.coffee'
mediator = require '../../../../lib/mediator.coffee'

module.exports = class ConnectCreateView extends Backbone.View
  className: 'Connect__create'

  events:
    click: 'create'

  initialize: ({ @connectable, @state }) ->
    @listenTo @state, 'change:query', @render

  create: (e) ->
    e.preventDefault()
    e.stopPropagation()

    @$el.text 'Creating'

    title = @state.get 'query'

    channel = new Channel
      title: title
      status: 'private'

    channel.url = "#{API_URL}/channels"
    channel.set "selected:#{@connectable.get 'base_class'}:#{@connectable.id}", true

    Promise(channel.save())
      .then =>
        @$el.text 'Connecting'
        @connect channel

      .then =>
        @render()
        @collection.unshift channel

      .catch =>
        @$el.text 'Error'

  connect: (channel) ->
    Promise $.ajax
      type: 'POST'
      url: "#{API_URL}/channels/#{channel.id}/connections"
      data:
        connectable_id: @connectable.id
        connectable_type: @connectable.get('base_class')

    .then =>
      mediator.shared.recent_connections.shove channel
      mediator.trigger "connection:added:#{@connectable.id}", channel
      mediator.trigger 'connection:added', channel

  render: ->
    @$el.text "+ New private channel “#{@state.get('query')}”"
    this
{'fraction_non_alphanumeric': 0.15813953488372093, 'fraction_numerical': 0.0, 'mean_word_length': 2.825921219822109, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6535205', 'n_tokens_mistral': 943, 'n_tokens_neox': 914, 'n_words': 195}