function shorten() {
  $.ajax({
    'url': '/api/v1/shorten',
    'type': 'POST',
    'dataType': 'JSON',
    'data': { 'url': $('#url-field').val() },
    'success': data => {
      console.log('data', data);
      const $link = $('#link');
      const resultHTML = `<a class="result" href="${data.shortUrl}">${data.shortUrl}</a>`;
      $link.html(resultHTML);
      $link.hide().fadeIn('slow');
    }
  });
}

$('#url-field').keyup( (event) => {
  if(event.keyCode === 13) 
    $('#btn-shorten').click();
});

$('#btn-shorten').on('click', () => {
  shorten();
});
