2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2019-10-20 16:59:02 +02:00
|
|
|
import React from "react";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { withTranslation, WithTranslation } from "react-i18next";
|
2020-06-12 08:19:33 +02:00
|
|
|
import { formatDistance, format, Locale } from "date-fns";
|
2019-10-20 16:59:02 +02:00
|
|
|
import { enUS, de, es } from "date-fns/locale";
|
2020-06-12 08:19:33 +02:00
|
|
|
import { DateInput, DateElement, FullDateFormat, toDate } from "./dates";
|
2019-02-07 09:28:43 +01:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
type LocaleMap = {
|
2019-10-21 10:57:56 +02:00
|
|
|
[key: string]: Locale;
|
2019-10-20 16:59:02 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-30 15:46:03 +01:00
|
|
|
export const supportedLocales: LocaleMap = {
|
2019-10-09 15:51:49 +02:00
|
|
|
enUS,
|
2019-10-30 15:26:43 +01:00
|
|
|
en: enUS,
|
2019-10-09 15:51:49 +02:00
|
|
|
de,
|
2019-10-20 16:59:02 +02:00
|
|
|
es
|
2019-10-09 15:51:49 +02:00
|
|
|
};
|
2019-03-12 10:44:58 +01:00
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
type Props = WithTranslation & {
|
2019-10-20 16:59:02 +02:00
|
|
|
date?: DateInput;
|
2019-10-19 16:38:07 +02:00
|
|
|
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-20 16:59:02 +02:00
|
|
|
baseDate?: DateInput;
|
|
|
|
|
};
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
type Options = {
|
|
|
|
|
addSuffix: boolean;
|
|
|
|
|
locale: Locale;
|
|
|
|
|
timeZone?: string;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-30 15:46:03 +01:00
|
|
|
export const chooseLocale = (language: string, languages?: string[]) => {
|
|
|
|
|
for (const lng of languages || []) {
|
|
|
|
|
const locale = supportedLocales[lng];
|
2019-10-30 15:29:49 +01:00
|
|
|
if (locale) {
|
|
|
|
|
return locale;
|
|
|
|
|
}
|
2019-10-30 15:46:03 +01:00
|
|
|
}
|
2019-10-30 15:29:49 +01:00
|
|
|
|
2019-10-30 15:46:03 +01:00
|
|
|
const locale = supportedLocales[language];
|
|
|
|
|
if (locale) {
|
|
|
|
|
return locale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return enUS;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class DateFromNow extends React.Component<Props> {
|
|
|
|
|
getLocale = (): Locale => {
|
|
|
|
|
const { i18n } = this.props;
|
|
|
|
|
return chooseLocale(i18n.language, i18n.languages);
|
2019-10-09 15:51:49 +02:00
|
|
|
};
|
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-20 16:59:02 +02:00
|
|
|
const options: Options = {
|
2019-10-12 14:03:49 +02:00
|
|
|
addSuffix: true,
|
2019-10-20 16:59:02 +02:00
|
|
|
locale: 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) {
|
2020-06-12 08:19:33 +02:00
|
|
|
return toDate(baseDate);
|
2019-10-12 14:03:49 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-06-12 08:19:33 +02:00
|
|
|
const isoDate = toDate(date);
|
2019-10-09 15:51:49 +02:00
|
|
|
const options = this.createOptions();
|
2019-10-12 14:03:49 +02:00
|
|
|
const distance = formatDistance(isoDate, this.getBaseDate(), options);
|
2020-06-12 08:19:33 +02:00
|
|
|
const formatted = format(isoDate, FullDateFormat, 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-23 15:47:08 +02:00
|
|
|
export default withTranslation()(DateFromNow);
|