Localize dateRanges and move to separate file

Signed-off-by: Mcat12 <newtoncat12@yahoo.com>
This commit is contained in:
Mcat12
2019-03-17 18:44:42 -07:00
parent b88df8d10d
commit 4142d98ff3
5 changed files with 141 additions and 94 deletions
+11
View File
@@ -0,0 +1,11 @@
{
"Last 24 Hours": "Last 24 Hours",
"Today": "Today",
"Yesterday": "Yesterday",
"Last 7 Days": "Last 7 Days",
"Last 30 Days": "Last 30 Days",
"This Month": "This Month",
"Last Month": "Last Month",
"This Year": "This Year",
"All Time": "All Time"
}
+55 -42
View File
@@ -9,7 +9,6 @@
* Please see LICENSE file for your rights under this license. */
import React from "react";
import { dateRanges } from "../log/QueryLog";
import DateRangePicker from "react-bootstrap-daterangepicker";
import { Button } from "reactstrap";
import {
@@ -17,6 +16,8 @@ import {
TimeRangeContext
} from "../common/context/TimeRangeContext";
import "bootstrap-daterangepicker/daterangepicker.css";
import { WithNamespaces, withNamespaces } from "react-i18next";
import { dateRanges } from "../../util/dateRanges";
export interface TimeRangeSelectorProps {
/**
@@ -38,52 +39,64 @@ export interface TimeRangeSelectorProps {
*/
export const TimeRangeSelector = ({
range,
onSelect
}: TimeRangeSelectorProps) => (
<DateRangePicker
startDate={range ? range.from : dateRanges["Last 24 Hours"][0]}
endDate={range ? range.until : dateRanges["Last 24 Hours"][1]}
maxDate={dateRanges.Today[1]}
onApply={(event, picker) => {
if (
picker.startDate.isSame(dateRanges["Last 24 Hours"][0]) &&
picker.endDate.isSame(dateRanges["Last 24 Hours"][1])
) {
// Set to null so we fetch data from FTL instead of the database
onSelect(null);
} else {
// Set the time range so we fetch from the database
onSelect({
from: picker.startDate,
until: picker.endDate,
name: picker.chosenLabel
});
}
}}
timePicker={true}
showDropdowns={true}
ranges={dateRanges}
>
<Button color="light" size="sm">
<i className="far fa-clock fa-lg" />
&nbsp; &nbsp;
{range
? range.name === "Custom Range"
? `From: ${range.from
.toDate()
.toLocaleString()}, Until: ${range.until
.toDate()
.toLocaleString()}`
: range.name
: "Last 24 Hours"}
</Button>
</DateRangePicker>
onSelect,
t
}: TimeRangeSelectorProps & WithNamespaces) => {
const translatedDateRanges = dateRanges(t);
const last24Hours = t("Last 24 Hours");
const today = t("Today");
return (
<DateRangePicker
startDate={range ? range.from : translatedDateRanges[last24Hours][0]}
endDate={range ? range.until : translatedDateRanges[last24Hours][1]}
maxDate={translatedDateRanges[today][1]}
onApply={(event, picker) => {
if (
picker.startDate.isSame(translatedDateRanges[last24Hours][0]) &&
picker.endDate.isSame(translatedDateRanges[last24Hours][1])
) {
// Set to null so we fetch data from FTL instead of the database
onSelect(null);
} else {
// Set the time range so we fetch from the database
onSelect({
from: picker.startDate,
until: picker.endDate,
name: picker.chosenLabel
});
}
}}
timePicker={true}
showDropdowns={true}
ranges={translatedDateRanges}
>
<Button color="light" size="sm">
<i className="far fa-clock fa-lg" />
&nbsp; &nbsp;
{range
? range.name === "Custom Range"
? range.from.toDate().toLocaleString() +
" - " +
range.until.toDate().toLocaleString()
: range.name
: t("Last 24 Hours")}
</Button>
</DateRangePicker>
);
};
export const TranslatedTimeRangeSelector = withNamespaces("time-ranges")(
TimeRangeSelector
);
export const TimeRangeSelectorContainer = () => (
<TimeRangeContext.Consumer>
{context => (
<TimeRangeSelector range={context.range} onSelect={context.update} />
<TranslatedTimeRangeSelector
range={context.range}
onSelect={context.update}
/>
)}
</TimeRangeContext.Consumer>
);
+37 -51
View File
@@ -21,7 +21,7 @@ import i18n from "i18next";
import i18next from "i18next";
import { WithNamespaces, withNamespaces } from "react-i18next";
import debounce from "lodash.debounce";
import moment, { Moment } from "moment";
import moment from "moment";
import {
CancelablePromise,
ignoreCancel,
@@ -29,6 +29,7 @@ import {
padNumber
} from "../../util";
import api from "../../util/api";
import { dateRanges } from "../../util/dateRanges";
import "react-table/react-table.css";
import "bootstrap-daterangepicker/daterangepicker.css";
@@ -56,14 +57,18 @@ class QueryLog extends Component<WithNamespaces, QueryLogState> {
constructor(props: WithNamespaces) {
super(props);
// This happens in the constructor to avoid using dateRanges before it's
// created
const { t } = this.props;
const translatedDateRanges = dateRanges(t);
const today = t("Today");
// This happens in the constructor to more easily use the translated date
// ranges
this.state.filters = [
{
id: "time",
value: {
start: dateRanges.Today[0],
end: dateRanges.Today[1]
start: translatedDateRanges[today][0],
end: translatedDateRanges[today][1]
}
}
];
@@ -369,35 +374,6 @@ const selectionFilter = (
);
};
/**
* Preconfigured date ranges listed in the date range picker
*/
export const dateRanges: { [name: string]: [Moment, Moment] } = {
"Last 24 Hours": [moment().subtract(1, "day"), moment()],
Today: [moment().startOf("day"), moment()],
Yesterday: [
moment()
.subtract(1, "days")
.startOf("day"),
moment()
.subtract(1, "days")
.endOf("day")
],
"Last 7 Days": [moment().subtract(6, "days"), moment()],
"Last 30 Days": [moment().subtract(29, "days"), moment()],
"This Month": [moment().startOf("month"), moment()],
"Last Month": [
moment()
.subtract(1, "month")
.startOf("month"),
moment()
.subtract(1, "month")
.endOf("month")
],
"This Year": [moment().startOf("year"), moment()],
"All Time": [moment(0), moment()]
};
/**
* The columns of the Query Log. Some pieces are translated, so you must pass in
* the translation function before using the columns.
@@ -434,23 +410,33 @@ const columns = (t: i18next.TranslationFunction) => [
}: {
filter: Filter;
onChange: ReactTableFunction;
}) => (
<DateRangePicker
startDate={filter ? filter.value.start : dateRanges["Last 24 Hours"][0]}
endDate={filter ? filter.value.end : dateRanges["Last 24 Hours"][1]}
maxDate={dateRanges.Today[1]}
onApply={(event, picker) =>
onChange({ start: picker.startDate, end: picker.endDate })
}
timePicker={true}
showDropdowns={true}
ranges={dateRanges}
>
<Button color="light" size="sm">
<i className="far fa-clock fa-lg" />
</Button>
</DateRangePicker>
)
}) => {
const translatedDateRanges = dateRanges(t);
const last24Hours = t("Last 24 Hours");
const today = t("Today");
return (
<DateRangePicker
startDate={
filter ? filter.value.start : translatedDateRanges[last24Hours][0]
}
endDate={
filter ? filter.value.end : translatedDateRanges[last24Hours][1]
}
maxDate={translatedDateRanges[today][1]}
onApply={(event, picker) =>
onChange({ start: picker.startDate, end: picker.endDate })
}
timePicker={true}
showDropdowns={true}
ranges={translatedDateRanges}
>
<Button color="light" size="sm">
<i className="far fa-clock fa-lg" />
</Button>
</DateRangePicker>
);
}
},
{
Header: t("Type"),
+36
View File
@@ -0,0 +1,36 @@
import moment, { Moment } from "moment";
import i18next from "i18next";
/**
* Preconfigured date ranges for use in a date range picker. The keys are
* translated, so this object must be computed using the translation function.
*
* @param t The translation function
*/
export const dateRanges: (
t: i18next.TranslationFunction
) => { [name: string]: [Moment, Moment] } = t => ({
[t("Last 24 Hours")]: [moment().subtract(1, "day"), moment()],
[t("Today")]: [moment().startOf("day"), moment()],
[t("Yesterday")]: [
moment()
.subtract(1, "days")
.startOf("day"),
moment()
.subtract(1, "days")
.endOf("day")
],
[t("Last 7 Days")]: [moment().subtract(6, "days"), moment()],
[t("Last 30 Days")]: [moment().subtract(29, "days"), moment()],
[t("This Month")]: [moment().startOf("month"), moment()],
[t("Last Month")]: [
moment()
.subtract(1, "month")
.startOf("month"),
moment()
.subtract(1, "month")
.endOf("month")
],
[t("This Year")]: [moment().startOf("year"), moment()],
[t("All Time")]: [moment(0), moment()]
});
+2 -1
View File
@@ -34,7 +34,8 @@ export function setupI18n() {
"query-log",
"settings",
"api-errors",
"preferences"
"preferences",
"time-ranges"
],
nsSeparator: false,
keySeparator: false,