<commit_before>_                             = require 'underscore'
sd                            = require('sharify').data
Backbone                      = require 'backbone'
StepView                      = require './step.coffee'
Artist                        = require '../../../../models/artist.coffee'
Followable                    = require '../mixins/followable.coffee'
GeneArtists                   = require '../mixins/gene_artists.coffee'
{ FollowButton, Following }   = require '../../../../components/follow_button/index.coffee'

template                  = -> require('../../templates/artists.jade') arguments...
suggestedArtistsTemplate  = -> require('../../templates/suggested_artists.jade') arguments...

module.exports = class ArtistsView extends StepView
  _.extend @prototype, Followable
  _.extend @prototype, GeneArtists

  analyticsUnfollowMessage : 'Unfollowed artist from personalize artist search'
  analyticsFollowMessage   : 'Followed artist from personalize artist search'

  events:
    'click .personalize-skip' : 'advance'
    'click .pfa-remove'       : 'unfollow'

  initialize: (options) ->
    super

    @following    = new Following [], kind: 'artist'
    @suggestions  = new Backbone.Collection
    @followed     = new Backbone.Collection [], model: Artist

    @initializeFollowable()
    @initializeGeneArtists()

    @listenTo @followed, 'add', @fetchRelatedArtists
    @listenTo @followed, 'remove', @disposeSuggestionSet
    @listenTo @suggestions, 'add', @renderSuggestions
    @listenTo @suggestions, 'remove', @renderSuggestions

  setupFollowButton: (key, model, el) ->
    @followButtonViews ?= {}
    @followButtonViews[key].remove() if @followButtonViews[key]?
    @followButtonViews[key] = new FollowButton
      analyticsUnfollowMessage : 'Unfollowed artist from personalize artist suggestions'
      analyticsFollowMessage   : 'Followed artist from personalize artist suggestions'
      notes                    : 'Followed from /personalize'
      following                : @following
      model                    : model
      modelName                : 'artist'
      el                       : el

  createSuggestionSet: (artist) ->
    new Backbone.Model
      id          : artist.id
      name        : "Artists related to #{artist.get 'name'}"
      suggestions : artist.relatedArtists

  fetchRelatedArtists: (artist) ->
    artist.fetchRelatedArtists 'Artists',
      success: (model, response) =>
        if response.length > 0 # If there are any suggestions
          @suggestions.unshift @createSuggestionSet artist

  # Removes the corresponding suggestionSet and disposes of
  # its FollowButton views
  disposeSuggestionSet: (model) ->
    suggestionSet = @suggestions.remove model.id
    _.each suggestionSet.get('suggestions').pluck('id'), (id) =>
      key = "#{suggestionSet.id}_#{id}"
      @followButtonViews[key].remove()
      @followButtonViews[key] = null

  renderSuggestions: ->
    (@$suggestions ?= @$('#personalize-suggestions')).
      html suggestedArtistsTemplate suggestions: @suggestions.models
    @setupFollowButtons()
<commit_msg>Fixes error when trying to dispose of a set that does not exist<commit_after>_                             = require 'underscore'
sd                            = require('sharify').data
Backbone                      = require 'backbone'
StepView                      = require './step.coffee'
Artist                        = require '../../../../models/artist.coffee'
Followable                    = require '../mixins/followable.coffee'
GeneArtists                   = require '../mixins/gene_artists.coffee'
{ FollowButton, Following }   = require '../../../../components/follow_button/index.coffee'

template                  = -> require('../../templates/artists.jade') arguments...
suggestedArtistsTemplate  = -> require('../../templates/suggested_artists.jade') arguments...

module.exports = class ArtistsView extends StepView
  _.extend @prototype, Followable
  _.extend @prototype, GeneArtists

  analyticsUnfollowMessage : 'Unfollowed artist from personalize artist search'
  analyticsFollowMessage   : 'Followed artist from personalize artist search'

  events:
    'click .personalize-skip' : 'advance'
    'click .pfa-remove'       : 'unfollow'

  initialize: (options) ->
    super

    @following    = new Following [], kind: 'artist'
    @suggestions  = new Backbone.Collection
    @followed     = new Backbone.Collection [], model: Artist

    @initializeFollowable()
    @initializeGeneArtists()

    @listenTo @followed, 'add', @fetchRelatedArtists
    @listenTo @followed, 'remove', @disposeSuggestionSet
    @listenTo @suggestions, 'add', @renderSuggestions
    @listenTo @suggestions, 'remove', @renderSuggestions

  setupFollowButton: (key, model, el) ->
    @followButtonViews ?= {}
    @followButtonViews[key].remove() if @followButtonViews[key]?
    @followButtonViews[key] = new FollowButton
      analyticsUnfollowMessage : 'Unfollowed artist from personalize artist suggestions'
      analyticsFollowMessage   : 'Followed artist from personalize artist suggestions'
      notes                    : 'Followed from /personalize'
      following                : @following
      model                    : model
      modelName                : 'artist'
      el                       : el

  createSuggestionSet: (artist) ->
    new Backbone.Model
      id          : artist.id
      name        : "Artists related to #{artist.get 'name'}"
      suggestions : artist.relatedArtists

  fetchRelatedArtists: (artist) ->
    artist.fetchRelatedArtists 'Artists',
      success: (model, response) =>
        if response.length > 0 # If there are any suggestions
          @suggestions.unshift @createSuggestionSet artist

  # Removes the corresponding suggestionSet and disposes of
  # its FollowButton views
  disposeSuggestionSet: (model) ->
    if (suggestionSet = @suggestions.remove model.id)
      _.each suggestionSet.get('suggestions').pluck('id'), (id) =>
        key = "#{suggestionSet.id}_#{id}"
        @followButtonViews[key].remove()
        @followButtonViews[key] = null

  renderSuggestions: ->
    (@$suggestions ?= @$('#personalize-suggestions')).
      html suggestedArtistsTemplate suggestions: @suggestions.models
    @setupFollowButtons()

{'fraction_non_alphanumeric': 0.1115512597052765, 'fraction_numerical': 0.00031690698779908097, 'mean_word_length': 2.534154535274356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '1205185', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1650, 'n_words': 405}