summaryrefslogtreecommitdiff
blob: be0dd24bba60d25c6ed34f914499295b47bd533e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
 * External dependencies
 */
import { _x, sprintf } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { date } from '@wordpress/date';
import { isEmpty } from 'lodash';

class DayPreview extends Component {
	formatTime( time ) {
		const { timeFormat } = this.props;
		const [ hours, minutes ] = time.split( ':' );
		const _date = new Date();
		if ( ! hours || ! minutes ) {
			return false;
		}
		_date.setHours( hours );
		_date.setMinutes( minutes );
		return date( timeFormat, _date );
	}

	renderInterval = ( interval, key ) => {
		return (
			<dd key={ key }>
				{ sprintf(
					_x( 'From %s to %s', 'from business opening hour to closing hour', 'jetpack' ),
					this.formatTime( interval.opening ),
					this.formatTime( interval.closing )
				) }
			</dd>
		);
	};

	render() {
		const { day, localization } = this.props;
		const hours = day.hours.filter(
			// remove any malformed or empty intervals
			interval => this.formatTime( interval.opening ) && this.formatTime( interval.closing )
		);
		return (
			<Fragment>
				<dt className={ day.name }>{ localization.days[ day.name ] }</dt>
				{ isEmpty( hours ) ? (
					<dd>{ _x( 'Closed', 'business is closed on a full day', 'jetpack' ) }</dd>
				) : (
					hours.map( this.renderInterval )
				) }
			</Fragment>
		);
	}
}

export default DayPreview;