<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import Moment from 'moment';
import { extendMoment } from 'moment-range';

import Icon from '../Icon';

import css from './Calendar.css';

const moment = extendMoment(Moment);

function getCalendar(year, month) {
  const startDate = moment([year, month]);
  const firstDay = moment(startDate).startOf('month');
  const endDay = moment(startDate).endOf('month');
  const monthRange = moment.range(firstDay, endDay);

  const weeks = [];
  const calendar = [];
  let firstWeekDay;
  let lastWeekDay;
  let week;

  const weekdays = Array.from(monthRange.by('days'));
  weekdays.forEach((mo) => {
    const ref = mo.week();
    if (weeks.indexOf(ref) < 0) {
      weeks.push(mo.week());
    }
  });

  for (let i = 0, len = weeks.length; i < len; i += 1) {
    week = weeks[i];
    if (i > 0 && week < weeks[i - 1]) {
      // We have switched to the next year

      firstWeekDay = moment([year, month]).add(1, 'year')
        .week(week).day(moment.localeData()._week.dow); // eslint-disable-line
      lastWeekDay = moment([year, month]).add(1, 'year')
        .week(week).day(moment.localeData()._week.dow + 6); // eslint-disable-line
    } else {
      firstWeekDay = moment([year, month])
        .week(week).day(moment.localeData()._week.dow); // eslint-disable-line
      lastWeekDay = moment([year, month])
        .week(week).day(moment.localeData()._week.dow + 6); // eslint-disable-line
    }
    const weekRange = moment.range(firstWeekDay, lastWeekDay);
    calendar.push(weekRange);
  }

  return calendar;
}


const propTypes = {
  date: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
  onSetDate: PropTypes.func,
  cursorDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
  dateFormat: PropTypes.string,
  selected: PropTypes.object,
  locale: PropTypes.string,
  onBlur: PropTypes.func,
  onKeyDown: PropTypes.func,
  buttonFocus: PropTypes.func,
  excludeDates: PropTypes.oneOfType([
    PropTypes.array,
    PropTypes.object,
    PropTypes.string,
  ]),
};

const defaultProps = {
  date: moment(),
  dateFormat: 'MM DD YYYY',
  onSetDate: () => null,
  locale: 'en',
};

class Calendar extends React.Component {
  constructor(props) {
    super(props);

    moment.locale(this.props.locale);

    let initDate;   // date to initialize calendar
    let inputDate;  // initial date for state
    if (this.props.date === '' || this.props.date === 'undefined') {
      initDate = moment();
      inputDate = moment();
    } else if (moment(this.props.date, this.props.dateFormat).isValid()) {
      initDate = moment(this.props.date, this.props.dateFormat);
      inputDate = moment(this.props.date, this.props.dateFormat);
    } else {
      initDate = moment();
      inputDate = moment();
    }

    const base = moment(initDate);
    const month = base.month();
    const year = base.year();

    this.state = {
      date: inputDate,
      month,
      year,
      calendar: getCalendar(year, month),
    };

    this.handleDayClick = this.handleDayClick.bind(this);
    this.setDate = this.setDate.bind(this);
    this.nextMonth = this.nextMonth.bind(this);
    this.previousMonth = this.previousMonth.bind(this);
    this.nextYear = this.nextYear.bind(this);
    this.previousYear = this.previousYear.bind(this);
    this.checkExcluded = this.checkExcluded.bind(this);
    this.isDateSelected = this.isDateSelected.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    if (moment(nextProps.date, this.props.dateFormat, true).isValid()) {
      let newState;
      let willUpdate = false;
      if (moment(nextProps.date) !== this.state.date) {
        const moDate = moment(nextProps.date);
        const month = moDate.month();
        const year = moDate.year();
        newState = {
          date: moDate,
          month,
          year,
          calendar: getCalendar(year, month),
        };
        willUpdate = true;
      }

      if (nextProps.cursorDate !== this.props.cursorDate) {
        const month = nextProps.cursorDate.month();
        const year = nextProps.cursorDate.year();
        newState = {
          month,
          year,
          calendar: getCalendar(year, month),
        };
        willUpdate = true;
      }

      if (willUpdate) { this.setState({ ...newState }); }
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    // fix null date month navigation issue...
    if(nextProps.date === ''){
      if (nextState.month != this.state.month || nextState.year != this.state.year){
        return true;
      }
    }
    if (!moment(nextProps.date, this.props.dateFormat, true).isValid()) {
        return false
    }
    
    return true;
  }

  setDate(day) {
    this.setState({
      date: day,
    });
  }

  getMonth() {
    return this.state.month;
  }
<commit_msg>Continue fixing date navigation with invalid values.<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import Moment from 'moment';
import { extendMoment } from 'moment-range';

import Icon from '../Icon';

import css from './Calendar.css';

const moment = extendMoment(Moment);

function getCalendar(year, month) {
  const startDate = moment([year, month]);
  const firstDay = moment(startDate).startOf('month');
  const endDay = moment(startDate).endOf('month');
  const monthRange = moment.range(firstDay, endDay);

  const weeks = [];
  const calendar = [];
  let firstWeekDay;
  let lastWeekDay;
  let week;

  const weekdays = Array.from(monthRange.by('days'));
  weekdays.forEach((mo) => {
    const ref = mo.week();
    if (weeks.indexOf(ref) < 0) {
      weeks.push(mo.week());
    }
  });

  for (let i = 0, len = weeks.length; i < len; i += 1) {
    week = weeks[i];
    if (i > 0 && week < weeks[i - 1]) {
      // We have switched to the next year

      firstWeekDay = moment([year, month]).add(1, 'year')
        .week(week).day(moment.localeData()._week.dow); // eslint-disable-line
      lastWeekDay = moment([year, month]).add(1, 'year')
        .week(week).day(moment.localeData()._week.dow + 6); // eslint-disable-line
    } else {
      firstWeekDay = moment([year, month])
        .week(week).day(moment.localeData()._week.dow); // eslint-disable-line
      lastWeekDay = moment([year, month])
        .week(week).day(moment.localeData()._week.dow + 6); // eslint-disable-line
    }
    const weekRange = moment.range(firstWeekDay, lastWeekDay);
    calendar.push(weekRange);
  }

  return calendar;
}


const propTypes = {
  date: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
  onSetDate: PropTypes.func,
  cursorDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
  dateFormat: PropTypes.string,
  selected: PropTypes.object,
  locale: PropTypes.string,
  onBlur: PropTypes.func,
  onKeyDown: PropTypes.func,
  buttonFocus: PropTypes.func,
  excludeDates: PropTypes.oneOfType([
    PropTypes.array,
    PropTypes.object,
    PropTypes.string,
  ]),
};

const defaultProps = {
  date: moment(),
  dateFormat: 'MM DD YYYY',
  onSetDate: () => null,
  locale: 'en',
};

class Calendar extends React.Component {
  constructor(props) {
    super(props);

    moment.locale(this.props.locale);

    let initDate;   // date to initialize calendar
    let inputDate;  // initial date for state
    if (this.props.date === '' || this.props.date === 'undefined') {
      initDate = moment();
      inputDate = moment();
    } else if (moment(this.props.date, this.props.dateFormat).isValid()) {
      initDate = moment(this.props.date, this.props.dateFormat);
      inputDate = moment(this.props.date, this.props.dateFormat);
    } else {
      initDate = moment();
      inputDate = moment();
    }

    const base = moment(initDate);
    const month = base.month();
    const year = base.year();

    this.state = {
      date: inputDate,
      month,
      year,
      calendar: getCalendar(year, month),
    };

    this.handleDayClick = this.handleDayClick.bind(this);
    this.setDate = this.setDate.bind(this);
    this.nextMonth = this.nextMonth.bind(this);
    this.previousMonth = this.previousMonth.bind(this);
    this.nextYear = this.nextYear.bind(this);
    this.previousYear = this.previousYear.bind(this);
    this.checkExcluded = this.checkExcluded.bind(this);
    this.isDateSelected = this.isDateSelected.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    let newState;
    let willUpdate = false;
    if (moment(nextProps.date, this.props.dateFormat, true).isValid()) {
      if (moment(nextProps.date) !== this.state.date) {
        const moDate = moment(nextProps.date);
        const month = moDate.month();
        const year = moDate.year();
        newState = {
          date: moDate,
          month,
          year,
          calendar: getCalendar(year, month),
        };
        willUpdate = true;
      }

      if (nextProps.cursorDate !== this.props.cursorDate) {
        const month = nextProps.cursorDate.month();
        const year = nextProps.cursorDate.year();
        newState = {
          month,
          year,
          calendar: getCalendar(year, month),
        };
        willUpdate = true;
      }
    } else { // fix navigation issue on null or invalid date
        const fallbackDate = moment();
        const month = fallbackDate.month();
        const year = fallbackDate.year();
        newState = {
          date: fallbackDate,
          month,
          year,
          calendar: getCalendar(year, month),
        };
    }

    if (willUpdate) { this.setState({ ...newState }); }
  }

  setDate(day) {
    this.setState({
      date: day,
    });
  }

  getMonth() {
    return this.state.month;
  }

{'fraction_non_alphanumeric': 0.14346209460855194, 'fraction_numerical': 0.0018591200165255113, 'mean_word_length': 2.77504873294347, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1330079', 'n_tokens_mistral': 3029, 'n_tokens_neox': 3009, 'n_words': 648}