<commit_before>import React, { Component, PropTypes } from 'react';
import { SingleDatePicker } from 'react-dates';
import moment from 'moment';

/**
 * DatePickerSingle is a tailor made component designed for selecting
 * a single day off of a calendar.
 *
 * The DatePicker integrates well with the Python datetime module with the
 * startDate and endDate being returned in a string format suitable for
 * creating datetime objects.
 *
 * This component is based off of Airbnb's react-dates react component
 * which can be found here: https://github.com/airbnb/react-dates
 */
export default class DatePickerSingle extends Component {
    constructor(props) {
        super(props);
        const propObj = {
            date: this.props.date,
            initialVisibleMonth: this.props.initial_visible_month,
            minDateAllowed: this.props.min_date_allowed,
            maxDateAllowed: this.props.max_date_allowed
        };
        const momentProps = this.convertPropsToMoment(propObj);
        this.state = {
            date: momentProps.date,
            focused: props.autoFocus,
            initialVisibleMonth: momentProps.initialVisibleMonth,
            minDateAllowed: momentProps.min,
            maxDateAllowed: momentProps.max,
            prevInitialVisibleMonth: props.initial_visible_month,
            prevMinDateAllowed: props.min_date_allowed,
            prevMaxDateAllowed: props.max_date_allowed
        };
    }

    convertPropsToMoment(props) {
        let date = null;
        let initialVisibleMonth = moment(props.date);
        if (typeof props.date !== 'undefined') {
            date = moment(props.date);
        }

        if (typeof props.initialVisibleMonth !== 'undefined') {
            initialVisibleMonth = moment(props.initialVisibleMonth);
        }

        let min; let max;
        if (typeof props.minDateAllowed !== 'undefined') {
            min = moment(props.minDateAllowed);
        }

        if (typeof props.maxDateAllowed !== 'undefined') {
            max = moment(props.maxDateAllowed);
            max.add(1, 'days');
        }

        return { date, initialVisibleMonth, min, max };
    }

    componentWillReceiveProps(newProps) {
        const propObj = {
            date: newProps.date,
            initialVisibleMonth: newProps.initial_visible_month,
            minDateAllowed: newProps.min_date_allowed,
            maxDateAllowed: newProps.max_date_allowed
        };
        const momentProps = this.convertPropsToMoment(propObj);
        if (this.state.date !== momentProps.date) {
            this.setState({ date: momentProps.date });
        }

        if (this.state.prevInitialVisibleMonth !== newProps.initial_visible_month) {
            this.setState({
                    prevInitialVisibleMonth: newProps.initial_visible_month,
                    initialVisibleMonth: momentProps.initialVisibleMonth
                });
        }

        if (this.state.prevMinDateAllowed !== newProps.min_date_allowed) {
            this.setState({
                prevMinDateAllowed: newProps.min_date_allowed,
                minDateAllowed: momentProps.minDateAllowed
            });
        }

        if (this.state.prevMaxDateAllowed !== newProps.max_date_allowed) {
            this.setState({
                prevMaxDateAllowed: newProps.max_date_allowed,
                maxDateAllowed: momentProps.maxDateAllowed
            });
        }
    }

    render() {
        const { setProps, fireEvent } = this.props;
        let verticalFlag = true;
        if (this.props.calendar_orientation === 'vertical') {
            verticalFlag = false;
        }

        return (
            <SingleDatePicker
                date={this.state.date}
                onDateChange={(date) => {
                        this.setState({ date });
                        if (date !== null) {
                            const dateStr = date.format('YYYY-MM-DD');
                            if (setProps) {
                                setProps({
                                    date: dateStr
                                });
                            }
                            if (fireEvent) {
                                fireEvent('change');
                            }
                        }
                    }
                }
                focused={this.state.focused}
                onFocusChange={({ focused }) => this.setState({ focused })}
                initialVisibleMonth={() => {
                    if (this.state.date !== null) {
                        return this.state.date;
                    } else {
                        return this.state.initialVisibleMonth;
                    }
                }
                }
                isOutsideRange={(date) => {
                    if (typeof this.state.minDateAllowed !== 'undefined' &&
                            typeof this.state.maxDateAllowed !== 'undefined') {
                        return date < this.state.minDateAllowed || date >= this.state.maxDateAllowed;
                    } else if (typeof this.state.minDateAllowed === 'undefined' &&
                                         typeof this.state.maxDateAllowed !== 'undefined') {
                        return date >= this.state.maxDateAllowed;
                    } else if (typeof this.state.minDateAllowed !== 'undefined' &&
                                         typeof this.state.maxDateAllowed === 'undefined') {
                        return date < this.state.minDateAllowed;
                    } else {
                        return false;
                    }
                }
                }
                numberOfMonths={this.props.number_of_months_shown}
                withPortal={this.props.with_portal && verticalFlag}
                withFullScreenPortal={this.props.with_full_screen_portal && verticalFlag}
                firstDayOfWeek={this.props.first_day_of_week}
                enableOutSideDays={this.props.show_outside_days}
                monthFormat={this.props.month_format}
                displayFormat={this.props.display_format}
                placeholder={this.props.placeholder}
                showClearDate={this.props.clearable}
                disabled={this.props.disabled}
                keepOpenOnDateSelect={this.props.stay_open_on_select}
                reopenPickerOnClearDates={this.props.reopen_calendar_on_clear}
                isRTL={this.props.is_RTL}
                orientation={this.props.calendar_orientation}
                daySize={this.props.day_size}
            />
        );
    }
}

DatePickerSingle.propTypes = {
    id: PropTypes.string,

    /**
     * Specifies the starting date for the component, best practice is to pass
     * value via datetime object
     */
    date: PropTypes.string,

    /**
     * Specifies the lowest selectable date for the component.
     * Accepts datetime.datetime objects or strings
     * in the format 'YYYY-MM-DD'
     */
    min_date_allowed: PropTypes.string,

    /**
     * Specifies the highest selectable date for the component.
     * Accepts datetime.datetime objects or strings
     * in the format 'YYYY-MM-DD'
     */
    max_date_allowed: PropTypes.string,
<commit_msg>clean up props<commit_after>import React, { Component, PropTypes } from 'react';
import { SingleDatePicker } from 'react-dates';
import moment from 'moment';

/**
 * DatePickerSingle is a tailor made component designed for selecting
 * a single day off of a calendar.
 *
 * The DatePicker integrates well with the Python datetime module with the
 * startDate and endDate being returned in a string format suitable for
 * creating datetime objects.
 *
 * This component is based off of Airbnb's react-dates react component
 * which can be found here: https://github.com/airbnb/react-dates
 */
export default class DatePickerSingle extends Component {
    constructor(props) {
        super(props);
        const propObj = {
            date: this.props.date,
            initialVisibleMonth: this.props.initial_visible_month,
            minDateAllowed: this.props.min_date_allowed,
            maxDateAllowed: this.props.max_date_allowed
        };
        const momentProps = this.convertPropsToMoment(propObj);
        this.state = {
            date: momentProps.date,
            focused: props.autoFocus,
            initialVisibleMonth: momentProps.initialVisibleMonth,
            minDateAllowed: momentProps.min,
            maxDateAllowed: momentProps.max,
            prevInitialVisibleMonth: props.initial_visible_month,
            prevMinDateAllowed: props.min_date_allowed,
            prevMaxDateAllowed: props.max_date_allowed
        };
        this.onDateChange = this.onDateChange.bind(this);
        this.isOutsideRange = this.isOutsideRange.bind(this);
    }

    convertPropsToMoment(props) {
        let date = null;
        let initialVisibleMonth = moment(props.date);
        if (typeof props.date !== 'undefined') {
            date = moment(props.date);
        }

        if (typeof props.initialVisibleMonth !== 'undefined') {
            initialVisibleMonth = moment(props.initialVisibleMonth);
        }

        let minDateAllowed;
        let maxDateAllowed;
        if (typeof props.minDateAllowed !== 'undefined') {
            minDateAllowed = moment(props.minDateAllowed);
        }

        if (typeof props.maxDateAllowed !== 'undefined') {
            maxDateAllowed = moment(props.maxDateAllowed);
            maxDateAllowed.add(1, 'days');
        }

        return {date, initialVisibleMonth, minDateAllowed, maxDateAllowed};
    }

    componentWillReceiveProps(newProps) {
        const propObj = {
            date: newProps.date,
            initialVisibleMonth: newProps.initial_visible_month,
            minDateAllowed: newProps.min_date_allowed,
            maxDateAllowed: newProps.max_date_allowed
        };
        const momentProps = this.convertPropsToMoment(propObj);
        if (this.state.date !== momentProps.date) {
            this.setState({ date: momentProps.date });
        }

        if (this.state.prevInitialVisibleMonth !== newProps.initial_visible_month) {
            this.setState({
                    prevInitialVisibleMonth: newProps.initial_visible_month,
                    initialVisibleMonth: momentProps.initialVisibleMonth
                });
        }

        if (this.state.prevMinDateAllowed !== newProps.min_date_allowed) {
            this.setState({
                prevMinDateAllowed: newProps.min_date_allowed,
                minDateAllowed: momentProps.minDateAllowed
            });
        }

        if (this.state.prevMaxDateAllowed !== newProps.max_date_allowed) {
            this.setState({
                prevMaxDateAllowed: newProps.max_date_allowed,
                maxDateAllowed: momentProps.maxDateAllowed
            });
        }
    }

    isOutsideRange(date) {
        if (typeof this.state.minDateAllowed !== 'undefined' &&
                typeof this.state.maxDateAllowed !== 'undefined') {
            return date < this.state.minDateAllowed || date >= this.state.maxDateAllowed;
        } else if (typeof this.state.minDateAllowed === 'undefined' &&
                             typeof this.state.maxDateAllowed !== 'undefined') {
            return date >= this.state.maxDateAllowed;
        } else if (typeof this.state.minDateAllowed !== 'undefined' &&
                             typeof this.state.maxDateAllowed === 'undefined') {
            return date < this.state.minDateAllowed;
        } else {
            return false;
        }
    }


    onDateChange(date) {
        this.setState({ date });
        if (date !== null) {
            const dateStr = date.format('YYYY-MM-DD');
            if (setProps) {
                setProps({
                    date: dateStr
                });
            }
            if (fireEvent) {
                fireEvent('change');
            }
        }
    }

    render() {
        const {
            date,
            focused,
            initialVisibleMonth
        } = this.state;

        const {
            calendar_orientation,
            clearable,
            day_size,
            disabled,
            display_format,
            fireEvent,
            first_day_of_week,
            is_RTL,
            minimum_nights,
            month_format,
            number_of_months_shown,
            placeholder,
            reopen_calendar_on_clear,
            setProps,
            show_outside_days,
            stay_open_on_select,
            with_full_screen_portal,
            with_portal,
        } = this.props;

        const verticalFlag = (calendar_orientation !== 'vertical');

        return (
            <SingleDatePicker
                date={date}
                onDateChange={this.onDateChange}
                focused={focused}
                onFocusChange={({focused}) => this.setState({focused})}
                initialVisibleMonth={date || initialVisibleMonth}
                isOutsideRange={this.isOutsideRange}
                numberOfMonths={number_of_months_shown}
                withPortal={with_portal && verticalFlag}
                withFullScreenPortal={with_full_screen_portal && verticalFlag}
                firstDayOfWeek={first_day_of_week}
                enableOutSideDays={show_outside_days}
                monthFormat={month_format}
                displayFormat={display_format}
                placeholder={placeholder}
                showClearDate={clearable}
                disabled={disabled}
                keepOpenOnDateSelect={stay_open_on_select}
                reopenPickerOnClearDates={reopen_calendar_on_clear}
                isRTL={is_RTL}
                orientation={calendar_orientation}
                daySize={day_size}
            />
        );
    }
}

DatePickerSingle.propTypes = {
    id: PropTypes.string,

    /**
     * Specifies the starting date for the component, best practice is to pass
     * value via datetime object
     */
    date: PropTypes.string,

    /**
     * Specifies the lowest selectable date for the component.
     * Accepts datetime.datetime objects or strings
     * in the format 'YYYY-MM-DD'
     */
    min_date_allowed: PropTypes.string,

    /**
     * Specifies the highest selectable date for the component.
     * Accepts datetime.datetime objects or strings
     * in the format 'YYYY-MM-DD'
     */
    max_date_allowed: PropTypes.string,

{'fraction_non_alphanumeric': 0.0879082936261308, 'fraction_numerical': 0.00013811200883916857, 'mean_word_length': 1.7642679900744418, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2965800', 'n_tokens_mistral': 3678, 'n_tokens_neox': 3616, 'n_words': 728}