<commit_before>import React, { PropTypes } from 'react'
import debounce from 'lodash/debounce'
import Radium, { StyleRoot } from 'radium'

import parseTime from '../helpers/parse-time'
import ClockWrapper from './ClockWrapper'
import Time from './Time'

import * as defaultConfig from '../helpers/config';

export class Timepicker extends React.Component {
	constructor(props){
		super(props)

		this.state = {
			...parseTime(props.time),
			unit: 'hour'
		}

		// override any default styles
		const config = Object.assign({}, defaultConfig, props.config)
		this.config = config

		this.changeHour =  this.handleTimeChange.bind(this, 'hour')
		this.changeMinute =  this.handleTimeChange.bind(this, 'minute')
		this.changeUnit =  this.changeUnit.bind(this)
		this.changeMeridiem = this.handleMeridiemChange.bind(this)

		this.timeChangeHandler = debounce(() => {
			this.props.onChange(this.getTime())
		}, 80)
	}

	componentWillReceiveProps(nextProps){
		if (nextProps.time){
			this.setState(parseTime(nextProps.time))
		}
	}

	getTime(){
		const state = this.state;
		const meridiemAdd = state.meridiem === 'pm' ? 12 : 0
		const paddedMinute = ('0' + state.minute).slice(-2)
		return {
			formatted: `${state.hour}:${paddedMinute} ${state.meridiem}`,
			formattedSimple: `${state.hour}:${paddedMinute}`,
			formatted24: `${state.hour + meridiemAdd}:${paddedMinute}`,
			hour: state.hour,
			hour24: state.hour + meridiemAdd,
			minute: state.minute,
			meridiem: state.meridiem
		}
	}

	handleTimeChange(unit, val, canChangeUnit){
		val = parseInt(val, 10);
		if (isNaN(val)){
			return
		}
		if (unit === 'hour' && val === 0){
			val = 12
		} else if (unit === 'minute' && val === 60){
			val = 0
		}

		this.setState({
			[unit]: val
		})

		const props = this.props
		// update time on parent
		props.onChange && this.timeChangeHandler()

		if (canChangeUnit && unit === 'hour' && props.switchToMinuteOnHourSelect){
			this.changeUnit('minute')
		} else if (canChangeUnit && unit === 'minute' && props.closeOnMinuteSelect){
			props.onDoneClick && props.onDoneClick()
		}
	}
	handleMeridiemChange(val){
		if (val !== this.state.meridiem){
			this.setState({
				meridiem: val
			})
			
			// update on parent
			this.props.onChange && this.timeChangeHandler()
		}
	}

	changeUnit(newUnit){
		const currentUnit = this.state.unit;
		if (currentUnit === newUnit){
			return;
		}
		this.setState({ unit: newUnit })
	}

	render(){<commit_msg>Ensures onChange has latest values from getTime()<commit_after>import React, { PropTypes } from 'react'
import debounce from 'lodash/debounce'
import Radium, { StyleRoot } from 'radium'

import parseTime from '../helpers/parse-time'
import ClockWrapper from './ClockWrapper'
import Time from './Time'

import * as defaultConfig from '../helpers/config';

export class Timepicker extends React.Component {
	constructor(props){
		super(props)

		this.state = {
			...parseTime(props.time),
			unit: 'hour'
		}

		// override any default styles
		const config = Object.assign({}, defaultConfig, props.config)
		this.config = config

		this.changeHour =  this.handleTimeChange.bind(this, 'hour')
		this.changeMinute =  this.handleTimeChange.bind(this, 'minute')
		this.changeUnit =  this.changeUnit.bind(this)
		this.changeMeridiem = this.handleMeridiemChange.bind(this)

		this.timeChangeHandler = null;
		if (typeof props.onChange === 'function') {
			this.timeChangeHandler = debounce(() => {
				this.props.onChange(this.getTime())
			}, 80)
		}
	}

	componentWillReceiveProps(nextProps){
		if (nextProps.time){
			this.setState(parseTime(nextProps.time))
		}
	}

	getTime(){
		const state = this.state;
		const meridiemAdd = state.meridiem === 'pm' ? 12 : 0
		const paddedMinute = ('0' + state.minute).slice(-2)
		return {
			formatted: `${state.hour}:${paddedMinute} ${state.meridiem}`,
			formattedSimple: `${state.hour}:${paddedMinute}`,
			formatted24: `${state.hour + meridiemAdd}:${paddedMinute}`,
			hour: state.hour,
			hour24: state.hour + meridiemAdd,
			minute: state.minute,
			meridiem: state.meridiem
		}
	}

	handleTimeChange(unit, val, canChangeUnit){
		val = parseInt(val, 10);
		if (isNaN(val)){
			return
		}
		if (unit === 'hour' && val === 0){
			val = 12
		} else if (unit === 'minute' && val === 60){
			val = 0
		}

		this.setState({
			[unit]: val
		}, this.timeChangeHandler) // update time on parent

		const props = this.props

		if (canChangeUnit && unit === 'hour' && props.switchToMinuteOnHourSelect){
			this.changeUnit('minute')
		} else if (canChangeUnit && unit === 'minute' && props.closeOnMinuteSelect){
			props.onDoneClick && props.onDoneClick()
		}
	}
	handleMeridiemChange(val){
		if (val !== this.state.meridiem){
			this.setState({
				meridiem: val
			}, this.timeChangeHandler) // update on parent
		}
	}

	changeUnit(newUnit){
		const currentUnit = this.state.unit;
		if (currentUnit === newUnit){
			return;
		}
		this.setState({ unit: newUnit })
	}

	render(){
{'fraction_non_alphanumeric': 0.15303856248738137, 'fraction_numerical': 0.007672117908338381, 'mean_word_length': 3.8237585199610518, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7461204', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1679, 'n_words': 363}