2018-10-10 07:22:06 +02:00
|
|
|
// @flow
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2018-10-10 07:22:06 +02:00
|
|
|
import moment from 'moment';
|
2017-09-03 18:35:01 +02:00
|
|
|
|
2018-10-10 07:22:06 +02:00
|
|
|
type Props = {
|
|
|
|
date?: number,
|
|
|
|
timeAgo?: boolean,
|
|
|
|
formatOptions: {},
|
|
|
|
show?: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
class DateTime extends React.PureComponent<Props> {
|
2017-12-21 22:08:54 +01:00
|
|
|
static SHOW_DATE = 'date';
|
|
|
|
static SHOW_TIME = 'time';
|
|
|
|
static SHOW_BOTH = 'both';
|
2017-09-18 02:52:57 +02:00
|
|
|
|
2017-10-08 22:43:56 +02:00
|
|
|
static defaultProps = {
|
|
|
|
formatOptions: {
|
2017-12-21 22:08:54 +01:00
|
|
|
month: 'long',
|
|
|
|
day: 'numeric',
|
|
|
|
year: 'numeric',
|
2017-10-08 22:43:56 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-09-03 18:35:01 +02:00
|
|
|
componentWillMount() {
|
|
|
|
this.refreshDate(this.props);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(props) {
|
|
|
|
this.refreshDate(props);
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshDate(props) {
|
|
|
|
const { block, date, fetchBlock } = props;
|
|
|
|
if (block && date === undefined) {
|
|
|
|
fetchBlock(block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-10-10 07:22:06 +02:00
|
|
|
const { date, formatOptions, timeAgo } = this.props;
|
2017-09-18 02:52:57 +02:00
|
|
|
const show = this.props.show || DateTime.SHOW_BOTH;
|
2017-10-08 22:43:56 +02:00
|
|
|
const locale = app.i18n.getLocale();
|
2017-09-03 18:35:01 +02:00
|
|
|
|
2018-10-10 19:43:15 +02:00
|
|
|
// If !date, it's currently being fetched
|
|
|
|
|
2018-10-10 07:22:06 +02:00
|
|
|
if (timeAgo) {
|
|
|
|
return date ? <span>{moment(date).from(moment())}</span> : <span />;
|
|
|
|
}
|
|
|
|
|
2017-09-18 02:52:57 +02:00
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{date &&
|
2018-10-10 07:22:06 +02:00
|
|
|
(show === DateTime.SHOW_BOTH || show === DateTime.SHOW_DATE) &&
|
2017-12-21 22:08:54 +01:00
|
|
|
date.toLocaleDateString([locale, 'en-US'], formatOptions)}
|
2018-10-10 07:22:06 +02:00
|
|
|
{show === DateTime.SHOW_BOTH && ' '}
|
2017-09-18 02:52:57 +02:00
|
|
|
{date &&
|
2018-10-10 07:22:06 +02:00
|
|
|
(show === DateTime.SHOW_BOTH || show === DateTime.SHOW_TIME) &&
|
2017-09-18 02:52:57 +02:00
|
|
|
date.toLocaleTimeString()}
|
2017-12-21 22:08:54 +01:00
|
|
|
{!date && '...'}
|
2017-09-18 02:52:57 +02:00
|
|
|
</span>
|
|
|
|
);
|
2017-09-03 18:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DateTime;
|