all files / src/components/ blocked-summoners.js

8.97% Statements 7/78
0% Branches 0/26
0% Functions 0/15
8.97% Lines 7/78
1 statement Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                                                                                                                                                                                                                                                                                                                        
const ProviderProxy = require('riotclient-async-provider-proxy');
const components = ProviderProxy.components;
const ValidationError = require('../utils/validation-error');
const transitionTime = 200;
const displayTime = 4000;
 
components.defineElement('lol-blocked-summoners', {
  styles: require('./blocked-summoners.styl'),
  data: require('../data'),
 
  created: function() {
    this.transitionStart = 0;
    this.updateTooltip = this.updateTooltip.bind(this);
    this.closeTooltip = this.closeTooltip.bind(this);
  },
 
  blockSummoner: function() {
    var summonerName = this.blockSummonerInput.value.trim();
    var validationError = this.validateSummonerName(summonerName);
 
    if (validationError) {
      this.tooltipTitle = this.t('block_system_message_error_title');
      this.tooltipText = this.t(validationError.translationKey, validationError.params);
      this.updateTooltip();
    } else {
      this.showBlockSummonerDialog(summonerName);
    }
  },
 
  showBlockSummonerDialog: function(summonerName) {
    let dialogContent = ProviderProxy.templateHelper.contentBlockDialog(
      this.t('blocked_summoners_block_confirm_title'),
      this.t('blocked_summoners_block_confirm_text', { name: summonerName }), 'dialog-medium',
        'confirm-friend-actions');
 
    var confirm = ProviderProxy.modalManager.add({
      type: 'DialogConfirm',
      owner: this,
      data: {
        contents: dialogContent,
        acceptText: this.t('blocked_summoners_block_button'),
        declineText: this.t('blocked_summoners_block_cancel'),
        closeButton: false
      }
    });
 
    // set the dialog width and left align the body text
    var confirmDiv = confirm.domNode.querySelector('.confirm-friend-actions');
    confirmDiv.style.width = '360px';
    confirmDiv.querySelector('p').style.textAlign = 'left';
 
    confirm.acceptPromise.then(() => {
      // Block the player
      ProviderProxy.ajax.get('/lol-summoner/v1/summoners?name=' + summonerName).then((summoner) => {
        ProviderProxy.ajax.post('/lol-chat/v1/blocked-players', { id: summoner.summonerId }).then(() => {
          this.blockSummonerInput.value = '';
          this.tooltipTitle = this.t('block_system_message_success_title');
          this.tooltipText = this.t('block_system_message_success_text');
          this.updateTooltip();
        }).catch(() => {
          this.tooltipTitle = this.t('block_system_message_error_title');
          this.tooltipText = this.t('block_system_message_server_error');
          this.updateTooltip();
        });
      }).catch(() => {
        this.tooltipTitle = this.t('block_system_message_error_title');
        this.tooltipText = this.t('block_system_message_summoner_dne', { name: summonerName });
        this.updateTooltip();
      });
    }).catch(() => {
      // Do nothing, they chose not to block
    });
  },
 
  updateTooltip: function() {
    var now = Date.now();
    if (now - this.transitionStart < transitionTime) {
      if (!this.updatingTooltip) {
        this.updatingTooltip = true;
        setTimeout(this.updateTooltip, transitionTime - (now - this.transitionStart));
      }
      return;
    }
 
    this.updatingTooltip = false;
 
    // Run the update, if message and open we need to close to reopen, close and queue update again
    if (this.tooltipText) {
      if (this.open) {
        this.closeTooltip();
        this.updatingTooltip = true;
        setTimeout(this.updateTooltip, transitionTime);
      } else {
        this.openTooltip();
      }
    } else {
      if (this.open) {
        this.closeTooltip();
      }
    }
  },
 
  openTooltip: function() {
    this.open = true;
    this.transitionStart = Date.now();
    var options = {
      showDelay: 0,
      targetAnchor: { x: 'center', y: 'bottom' },
      tooltipAnchor: { x: 'center', y: 'top' },
      offset: { y: 5 },
      showEvent: 'nothing',
      hideEvent: 'nothing'
    };
    var tooltip = this.createTooltip(this.tooltipTitle, this.tooltipText, options);
 
    ProviderProxy.tooltipManager.assign(this.blockSummonerInput, tooltip, null, options);
    ProviderProxy.tooltipManager.show(this.blockSummonerInput);
    this.closeTimer = setTimeout(this.closeTooltip, displayTime);
  },
 
  closeTooltip: function() {
    clearTimeout(this.closeTimer);
    this.open = false;
    this.transitionStart = Date.now();
    ProviderProxy.tooltipManager.unassign(this.blockSummonerInput);
  },
 
  createTooltip: function(title, text, options) {
    var tooltip = document.createElement('lol-uikit-tooltip');
    var tooltipBody = ProviderProxy.templateHelper.contentBlockTooltip(title, text);
    tooltipBody.style.width = options && options.width ? options.width + 'px' : '200px';
    tooltipBody.style.whiteSpace = 'normal';
    tooltip.appendChild(tooltipBody);
    return tooltip;
  },
 
  validateSummonerName: function(newName) {
    //validate the new name
    if (newName === '') {
      return new ValidationError('blank_name', 'block_system_message_blank_summoner_name');
    }
 
    if (newName.length < 3) {
      return new ValidationError('short_name', 'block_system_message_name_too_short');
    }
 
    if (newName.toUpperCase() === this.data.me.name.toUpperCase()) {
      return new ValidationError('cannot_invite_self', 'block_system_message_cannot_block_self');
    }
 
    var alreadyBlocked = this.data.blockedPlayers.some((player) => {
      return (player.name && player.name.toUpperCase() === newName.toUpperCase());
    });
    if (alreadyBlocked) {
      return new ValidationError('name_exists', 'block_system_message_already_blocked', { name: newName });
    }
 
    return null;
  }
});