<commit_before>'use strict';
window.Categories = (function() {
  var categoryInput;
  var categorySubmitButton;
  var categoryDropDown;
   
  return {
    init: function() {
      categoryInput = $('.category-form #customCategory');
      categorySubmitButton = $('.category-form #submitCategory');
      categoryDropDown = $('#POST-category');
      categoryInput.on('keyup', validateNewCategory);
      categorySubmitButton.on('click', addNewCategory);
    }
  };
  
  function validateNewCategory() {
    var category = categoryInput.val();
    if (0 < category.length && category.length <= 20) {
      categorySubmitButton.attr('disabled', false);
      return true;
    } else {
      categorySubmitButton.attr('disabled', true);
      return false;
    }  
  }

  function addNewCategory() {
    if (validateNewCategory() && !checkForDuplicateCategory()) {
      var category = categoryInput.val();
      sendNewCategoryWithFetch(category);
      App.addUserCategory(category);
      categoryDropDown.append($('<option>', {value: category, text: category}));
    } else if (!validateNewCategory()) {
      alert('The length of your new category should be <= 20');
    } else if (checkForDuplicateCategory()) {
      alert('That category already exists');
    }
    clearNewCategory();
    $('html, body').animate({ scrollTop: 0 }, 'medium');
  }

  function checkForDuplicateCategory() {
    var category = categoryInput.val();
    var hasDuplicate = false;
    $('select[name=\'category\'] > option').each(function () {
      if (category.toLowerCase() === this.text.toLowerCase()) {
        hasDuplicate = true;
        //break out of loop
        return false;
      }
    });
    return hasDuplicate; 
  }

  function clearNewCategory() {
    categorySubmitButton.attr('disabled', true);
    categoryInput.val('');
  }

  function sendNewCategoryWithFetch(category) {
    debugger;
    let username = localStorage.getItem('username');
    let sessionID = localStorage.getItem('sessionID');
    let url = `${awsURL}/api/category/${username}/?category=${category}&sessionID=${sessionID}`;
    fetch(url, {
      credentials: 'same-origin',
      method: 'post'
    });
  }
}());<commit_msg>A new category gets added to the page only when the post request returns successful<commit_after>'use strict';
window.Categories = (function() {
  var categoryInput;
  var categorySubmitButton;
  var categoryDropDown;
   
  return {
    init: function() {
      categoryInput = $('.category-form #customCategory');
      categorySubmitButton = $('.category-form #submitCategory');
      categoryDropDown = $('#POST-category');
      categoryInput.on('keyup', validateNewCategory);
      categorySubmitButton.on('click', addNewCategory);
    }
  };
  
  function validateNewCategory() {
    var category = categoryInput.val();
    if (0 < category.length && category.length <= 20) {
      categorySubmitButton.attr('disabled', false);
      return true;
    } else {
      categorySubmitButton.attr('disabled', true);
      return false;
    }  
  }

  function addNewCategory() {
    if (validateNewCategory() && !checkForDuplicateCategory()) {
      var category = categoryInput.val();
      sendNewCategoryWithFetch(category);
    } else if (!validateNewCategory()) {
      alert('The length of your new category should be <= 20');
    } else if (checkForDuplicateCategory()) {
      alert('That category already exists');
    }
    clearNewCategory();
    $('html, body').animate({ scrollTop: 0 }, 'medium');
  }

  function checkForDuplicateCategory() {
    var category = categoryInput.val();
    var hasDuplicate = false;
    $('select[name=\'category\'] > option').each(function () {
      if (category.toLowerCase() === this.text.toLowerCase()) {
        hasDuplicate = true;
        //break out of loop
        return false;
      }
    });
    return hasDuplicate; 
  }

  function clearNewCategory() {
    categorySubmitButton.attr('disabled', true);
    categoryInput.val('');
  }

  function sendNewCategoryWithFetch(category) {
    let username = localStorage.getItem('username');
    let sessionID = localStorage.getItem('sessionID');
    let url = `${awsURL}/api/category/${username}/?category=${category}&sessionID=${sessionID}`;
    fetch(url, {
      credentials: 'same-origin',
      method: 'post'
    }).then(function(result){
      return result;
    }).then(function(res) {
      App.addUserCategory(category);
      categoryDropDown.append($('<option>', {value: category, text: category}));
    }).catch(function(error) {
      alert('Sorry, there was an error with inserting a new category', error);
    });
  }
}());
{'fraction_non_alphanumeric': 0.14522732169790994, 'fraction_numerical': 0.002585649644473174, 'mean_word_length': 3.41674595623216, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '4289362', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1275, 'n_words': 287}