2019-10-19 16:38:07 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import { translate } from 'react-i18next';
|
|
|
|
|
import { formatDistance, format, parseISO } from 'date-fns';
|
|
|
|
|
import { enUS, de, es } from 'date-fns/locale';
|
|
|
|
|
import styled from 'styled-components';
|
2019-02-07 09:28:43 +01:00
|
|
|
|
2019-10-09 15:51:49 +02:00
|
|
|
const supportedLocales = {
|
|
|
|
|
enUS,
|
|
|
|
|
de,
|
2019-10-19 16:38:07 +02:00
|
|
|
es,
|
2019-10-09 15:51:49 +02:00
|
|
|
};
|
2019-03-12 10:44:58 +01:00
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
date?: string;
|
|
|
|
|
timeZone?: string;
|
2019-10-12 14:03:49 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* baseDate is the date from which the distance is calculated,
|
|
|
|
|
* default is the current time (new Date()). This property
|
|
|
|
|
* is required to keep snapshots tests green over the time on
|
|
|
|
|
* ci server.
|
|
|
|
|
*/
|
2019-10-19 16:38:07 +02:00
|
|
|
baseDate?: string;
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
// context props
|
2019-10-19 16:38:07 +02:00
|
|
|
i18n: any;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-09 15:51:49 +02:00
|
|
|
const DateElement = styled.time`
|
2019-10-08 16:42:08 +02:00
|
|
|
border-bottom: 1px dotted rgba(219, 219, 219);
|
|
|
|
|
cursor: help;
|
|
|
|
|
`;
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2019-10-08 16:42:08 +02:00
|
|
|
class DateFromNow extends React.Component<Props> {
|
2019-10-09 15:51:49 +02:00
|
|
|
getLocale = () => {
|
|
|
|
|
const { i18n } = this.props;
|
|
|
|
|
const locale = supportedLocales[i18n.language];
|
|
|
|
|
if (!locale) {
|
|
|
|
|
return enUS;
|
|
|
|
|
}
|
|
|
|
|
return locale;
|
|
|
|
|
};
|
2019-02-07 09:28:43 +01:00
|
|
|
|
2019-10-09 15:51:49 +02:00
|
|
|
createOptions = () => {
|
2019-10-12 14:03:49 +02:00
|
|
|
const { timeZone } = this.props;
|
2019-10-19 16:38:07 +02:00
|
|
|
const options: object = {
|
2019-10-12 14:03:49 +02:00
|
|
|
addSuffix: true,
|
|
|
|
|
locate: this.getLocale(),
|
2019-10-09 15:51:49 +02:00
|
|
|
};
|
2019-10-12 14:03:49 +02:00
|
|
|
if (timeZone) {
|
|
|
|
|
options.timeZone = timeZone;
|
|
|
|
|
}
|
|
|
|
|
return options;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getBaseDate = () => {
|
|
|
|
|
const { baseDate } = this.props;
|
|
|
|
|
if (baseDate) {
|
|
|
|
|
return parseISO(baseDate);
|
|
|
|
|
}
|
|
|
|
|
return new Date();
|
2019-10-09 15:51:49 +02:00
|
|
|
};
|
2019-02-07 09:28:43 +01:00
|
|
|
|
2019-10-09 15:51:49 +02:00
|
|
|
render() {
|
|
|
|
|
const { date } = this.props;
|
|
|
|
|
if (date) {
|
|
|
|
|
const isoDate = parseISO(date);
|
|
|
|
|
const options = this.createOptions();
|
2019-10-12 14:03:49 +02:00
|
|
|
const distance = formatDistance(isoDate, this.getBaseDate(), options);
|
2019-10-19 16:38:07 +02:00
|
|
|
const formatted = format(isoDate, 'yyyy-MM-dd HH:mm:ss', options);
|
2019-10-09 15:51:49 +02:00
|
|
|
return <DateElement title={formatted}>{distance}</DateElement>;
|
2019-02-07 09:28:43 +01:00
|
|
|
}
|
|
|
|
|
return null;
|
2018-09-03 16:17:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 16:42:08 +02:00
|
|
|
export default translate()(DateFromNow);
|