<commit_before>kd                        = require 'kd'
AvatarPopup               = require './avatarpopup'
CustomLinkView            = require '../customlinkview'
HelpSupportModal          = require '../commonviews/helpsupportmodal'
trackEvent                = require 'app/util/trackEvent'

module.exports = class AccountPopup extends AvatarPopup

  constructor: (options = {}, data) ->

    options.cssClass = kd.utils.curry 'account', options.cssClass

    super options, data

    { groupsController } = kd.singletons

    @avatarPopupContent.addSubView @paymentActionLabel = new CustomLinkView
      title      : 'Upgrade Plan'
      href       : '/Pricing'
      cssClass   : 'bottom-separator'
      click      : @bound 'goToPricing'

    @avatarPopupContent.addSubView new CustomLinkView
      title      : 'Koding University'
      href       : 'http://learn.koding.com'
      target     : '_blank'

    @avatarPopupContent.addSubView new CustomLinkView
      title      : 'Contact support'
      cssClass   : 'bottom-separator support'
      click      : @bound 'goToSupport'

    @avatarPopupContent.addSubView new CustomLinkView
      title      : 'Account Settings'
      href       : '/Account'
      attributes : testpath : 'AccountSettingsLink'
      cssClass   : 'bottom-separator'
      click      : @bound 'goToAccountSettings'

    @avatarPopupContent.addSubView dashboardLink = new CustomLinkView
      title    : 'Group Dashboard'
      href     : '/Dashboard'
      cssClass : 'bottom hidden'
      click    : @bound 'goToDashboard'

    @avatarPopupContent.addSubView adminLink = new CustomLinkView
      title    : 'Team Settings'
      href     : '/Admin'
      cssClass : 'bottom hidden'
      click    : @bound 'goToTeamSettings'

    @avatarPopupContent.addSubView new CustomLinkView
      title      : 'Logout'
      href       : '/Logout'
      attributes : testpath : 'logout-link'

    # FIXME:
    groupsController.ready ->
      group = groupsController.getCurrentGroup()
      group.canEditGroup (err, success) ->
        return  unless success
        dashboardLink.show()
        adminLink.show()


  viewAppended: ->

    @updatePaymentTitle()

    { paymentController } = kd.singletons

    paymentFinishedEvents = [
      'UserPlanUpdated'
      'PaypalRequestFinished'
    ]

    paymentController.on paymentFinishedEvents, @bound 'updatePaymentTitle'


  updatePaymentTitle: ->
    { paymentController } = kd.singletons

    paymentController.subscriptions (err, subscription) =>
      title = if err or not subscription
      then 'Upgrade Plan'
      else
        if subscription.planTitle is 'free'
        then 'Upgrade Plan'
        else 'Change Plan'

      @paymentActionLabel.updatePartial title


  hide:->

    super

    @emit 'AvatarPopupShouldBeHidden'


  goToPricing: (event) ->

    kd.utils.stopDOMEvent event
    kd.singletons.router.handleRoute '/Pricing'
    @hide()

    trackEvent 'Account upgrade plan, click',
      category : 'userInteraction'
      action   : 'clicks'<commit_msg>accountpopup: write plan name with lowercase `p`<commit_after>kd                        = require 'kd'
AvatarPopup               = require './avatarpopup'
CustomLinkView            = require '../customlinkview'
HelpSupportModal          = require '../commonviews/helpsupportmodal'
trackEvent                = require 'app/util/trackEvent'

module.exports = class AccountPopup extends AvatarPopup

  constructor: (options = {}, data) ->

    options.cssClass = kd.utils.curry 'account', options.cssClass

    super options, data

    { groupsController } = kd.singletons

    @avatarPopupContent.addSubView @paymentActionLabel = new CustomLinkView
      title      : 'Upgrade plan'
      href       : '/Pricing'
      cssClass   : 'bottom-separator'
      click      : @bound 'goToPricing'

    @avatarPopupContent.addSubView new CustomLinkView
      title      : 'Koding University'
      href       : 'http://learn.koding.com'
      target     : '_blank'

    @avatarPopupContent.addSubView new CustomLinkView
      title      : 'Contact support'
      cssClass   : 'bottom-separator support'
      click      : @bound 'goToSupport'

    @avatarPopupContent.addSubView new CustomLinkView
      title      : 'Account Settings'
      href       : '/Account'
      attributes : testpath : 'AccountSettingsLink'
      cssClass   : 'bottom-separator'
      click      : @bound 'goToAccountSettings'

    @avatarPopupContent.addSubView dashboardLink = new CustomLinkView
      title    : 'Group Dashboard'
      href     : '/Dashboard'
      cssClass : 'bottom hidden'
      click    : @bound 'goToDashboard'

    @avatarPopupContent.addSubView adminLink = new CustomLinkView
      title    : 'Team Settings'
      href     : '/Admin'
      cssClass : 'bottom hidden'
      click    : @bound 'goToTeamSettings'

    @avatarPopupContent.addSubView new CustomLinkView
      title      : 'Logout'
      href       : '/Logout'
      attributes : testpath : 'logout-link'

    # FIXME:
    groupsController.ready ->
      group = groupsController.getCurrentGroup()
      group.canEditGroup (err, success) ->
        return  unless success
        dashboardLink.show()
        adminLink.show()


  viewAppended: ->

    @updatePaymentTitle()

    { paymentController } = kd.singletons

    paymentFinishedEvents = [
      'UserPlanUpdated'
      'PaypalRequestFinished'
    ]

    paymentController.on paymentFinishedEvents, @bound 'updatePaymentTitle'


  updatePaymentTitle: ->
    { paymentController } = kd.singletons

    paymentController.subscriptions (err, subscription) =>
      title = if err or not subscription
      then 'Upgrade plan'
      else
        if subscription.planTitle is 'free'
        then 'Upgrade plan'
        else 'Change plan'

      @paymentActionLabel.updatePartial title


  hide:->

    super

    @emit 'AvatarPopupShouldBeHidden'


  goToPricing: (event) ->

    kd.utils.stopDOMEvent event
    kd.singletons.router.handleRoute '/Pricing'
    @hide()

    trackEvent 'Account upgrade plan, click',
      category : 'userInteraction'
      action   : 'clicks'
{'fraction_non_alphanumeric': 0.08850855745721271, 'fraction_numerical': 0.0, 'mean_word_length': 2.422197434467373, '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': '6977673', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1713, 'n_words': 395}