<commit_before>import $ from 'jquery';

import headerSmallImg from 'images/header-small.png';
import headerImg from 'images/header.jpg';

import styles from 'components/Landing/Landing.scss';

function onResize() {
  const scrollbar = window.innerWidth - $(window).width();
  const width = $('body').width() + scrollbar;
  const headerImage = $('#header');
  const isSmall = headerImage.attr('src') === headerSmallImg;

  if (width >= 768 && isSmall) {
    headerImage.attr('src', headerImg);
  } else if (width < 768 && !isSmall) {
    headerImage.attr('src', headerSmallImg);
  }
}

function validateForm() {
  const phoneNumber = $('#phoneNumber').val().replace(/[^0-9a-z\+]/g, '');
  const numReg = new RegExp(/^(\+?[0-9]{0,15})$/);
  let isValid = numReg.test(phoneNumber);
  const button = $('#button');
  if (isValid) {
    $('#form-response').addClass(styles.hidden);
    $('#phoneNumber').removeClass(styles.error);
    if (phoneNumber[0] === '+' ? (phoneNumber.length >= 12) : (phoneNumber.length >= 10)) {
      button.addClass(styles.enabled);
    } else {
      isValid = false;
      button.removeClass(styles.enabled);
    }
  } else {
    setError('Phone number is not valid.');
    button.removeClass(styles.enabled);
  }

  return isValid;
}

function setError(error) {
  const response = $('#form-response');
  response.children('p').text(error);
  response.removeClass(styles.success);
  response.removeClass(styles.hidden);
  $('#phoneNumber').addClass(styles.error);
}

function setSuccess(success) {
  const response = $('#form-response');
  response.children('p').text(success);
  response.addClass(styles.success);
  response.removeClass(styles.hidden);
  $('#phoneNumber').removeClass(styles.error);
}

$(document).ready(() => {
  onResize();
  $(window).resize(() => onResize());
  $('#form').submit((event) => {
    event.preventDefault();
    const isValid = validateForm();
    if (!isValid) {
      return;
    }

    const url = 'https://api.letsroundup.com/waitingList';
    const phoneNumber = $('#phoneNumber').val();
    const button = $('#button');
    button.removeClass(styles.enabled);
    if (!__DEV__) {
      ll('tagEvent', 'Submitting Form');
    }
    $.ajax({
      url,
      method: 'POST',
      contentType: 'application/json; charset=utf-8',
      data: JSON.stringify({ phoneNumber }),
      success(data) {
        if (data && data.msg === 'Already added.') {
          setError('You\'re already on the waiting list.');
          $('.button').addClass('enabled');
        } else {
          setSuccess('Thanks! We\'ll text you when it\'s your turn.');
        }
      },
      error(data, status, error) {
        console.error(data);
        let text;
        try {
          text = JSON.parse(data.responseText);
          text = text && text.error;
        } catch (ex) {
          console.error(ex);
          text = 'Oops, an error occured, please try again later!';
        }
        if (text.indexOf('exists') !== -1) {
          text = 'You\'re already on the waiting list.';
        }
        setError(text);
        button.addClass(styles.enabled);
      },
    });
  });
  $('#phoneNumber').keyup(() => validateForm());
  $('#phoneNumber').change(() => validateForm());
});
<commit_msg>[added] possibility to pre-populate phonenumber<commit_after>import $ from 'jquery';

import headerSmallImg from 'images/header-small.png';
import headerImg from 'images/header.jpg';

import styles from 'components/Landing/Landing.scss';

$(document).ready(() => {
  const phoneNumberParam = getParameterByName('phone');
  if (phoneNumberParam) {
    $('#phoneNumber').val(phoneNumberParam);
  }
});

function getParameterByName(name) {
  const escapedName = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
  const regex = new RegExp('[\\?&]' + escapedName + '=([^&#]*)');
  const results = regex.exec(location.search);
  return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

function onResize() {
  const scrollbar = window.innerWidth - $(window).width();
  const width = $('body').width() + scrollbar;
  const headerImage = $('#header');
  const isSmall = headerImage.attr('src') === headerSmallImg;

  if (width >= 768 && isSmall) {
    headerImage.attr('src', headerImg);
  } else if (width < 768 && !isSmall) {
    headerImage.attr('src', headerSmallImg);
  }
}

function validateForm() {
  const phoneNumber = $('#phoneNumber').val().replace(/[^0-9a-z\+]/g, '');
  const numReg = new RegExp(/^(\+?[0-9]{0,15})$/);
  let isValid = numReg.test(phoneNumber);
  const button = $('#button');
  if (isValid) {
    $('#form-response').addClass(styles.hidden);
    $('#phoneNumber').removeClass(styles.error);
    if (phoneNumber[0] === '+' ? (phoneNumber.length >= 12) : (phoneNumber.length >= 10)) {
      button.addClass(styles.enabled);
    } else {
      isValid = false;
      button.removeClass(styles.enabled);
    }
  } else {
    setError('Phone number is not valid.');
    button.removeClass(styles.enabled);
  }

  return isValid;
}

function setError(error) {
  const response = $('#form-response');
  response.children('p').text(error);
  response.removeClass(styles.success);
  response.removeClass(styles.hidden);
  $('#phoneNumber').addClass(styles.error);
}

function setSuccess(success) {
  const response = $('#form-response');
  response.children('p').text(success);
  response.addClass(styles.success);
  response.removeClass(styles.hidden);
  $('#phoneNumber').removeClass(styles.error);
}

$(document).ready(() => {
  onResize();
  $(window).resize(() => onResize());
  $('#form').submit((event) => {
    event.preventDefault();
    const isValid = validateForm();
    if (!isValid) {
      return;
    }

    const url = 'https://api.letsroundup.com/waitingList';
    const phoneNumber = $('#phoneNumber').val();
    const button = $('#button');
    button.removeClass(styles.enabled);
    if (!__DEV__) {
      ll('tagEvent', 'Submitting Form');
    }
    $.ajax({
      url,
      method: 'POST',
      contentType: 'application/json; charset=utf-8',
      data: JSON.stringify({ phoneNumber }),
      success(data) {
        if (data && data.msg === 'Already added.') {
          setError('You\'re already on the waiting list.');
          $('.button').addClass('enabled');
        } else {
          setSuccess('Thanks! We\'ll text you when it\'s your turn.');
        }
      },
      error(data, status, error) {
        console.error(data);
        let text;
        try {
          text = JSON.parse(data.responseText);
          text = text && text.error;
        } catch (ex) {
          console.error(ex);
          text = 'Oops, an error occured, please try again later!';
        }
        if (text.indexOf('exists') !== -1) {
          text = 'You\'re already on the waiting list.';
        }
        setError(text);
        button.addClass(styles.enabled);
      },
    });
  });
  $('#phoneNumber').keyup(() => validateForm());
  $('#phoneNumber').change(() => validateForm());
});

{'fraction_non_alphanumeric': 0.19138004852290566, 'fraction_numerical': 0.00585129156557728, 'mean_word_length': 3.161520190023753, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 2, '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': '474642', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2135, 'n_words': 431}