From 4142d98ff3eb144e83d73c66b60e22ff5b502e54 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Sat, 16 Mar 2019 14:13:43 -0700 Subject: [PATCH] Localize dateRanges and move to separate file Signed-off-by: Mcat12 --- public/i18n/en/time-ranges.json | 11 +++ .../dashboard/TimeRangeSelector.tsx | 97 +++++++++++-------- src/components/log/QueryLog.tsx | 88 +++++++---------- src/util/dateRanges.tsx | 36 +++++++ src/util/i18n.tsx | 3 +- 5 files changed, 141 insertions(+), 94 deletions(-) create mode 100644 public/i18n/en/time-ranges.json create mode 100644 src/util/dateRanges.tsx diff --git a/public/i18n/en/time-ranges.json b/public/i18n/en/time-ranges.json new file mode 100644 index 0000000..650a560 --- /dev/null +++ b/public/i18n/en/time-ranges.json @@ -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" +} \ No newline at end of file diff --git a/src/components/dashboard/TimeRangeSelector.tsx b/src/components/dashboard/TimeRangeSelector.tsx index db16bc6..bc17920 100644 --- a/src/components/dashboard/TimeRangeSelector.tsx +++ b/src/components/dashboard/TimeRangeSelector.tsx @@ -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) => ( - { - 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} - > - - + onSelect, + t +}: TimeRangeSelectorProps & WithNamespaces) => { + const translatedDateRanges = dateRanges(t); + const last24Hours = t("Last 24 Hours"); + const today = t("Today"); + + return ( + { + 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} + > + + + ); +}; + +export const TranslatedTimeRangeSelector = withNamespaces("time-ranges")( + TimeRangeSelector ); export const TimeRangeSelectorContainer = () => ( {context => ( - + )} ); diff --git a/src/components/log/QueryLog.tsx b/src/components/log/QueryLog.tsx index ca3eb06..95fc931 100644 --- a/src/components/log/QueryLog.tsx +++ b/src/components/log/QueryLog.tsx @@ -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 { 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; - }) => ( - - onChange({ start: picker.startDate, end: picker.endDate }) - } - timePicker={true} - showDropdowns={true} - ranges={dateRanges} - > - - - ) + }) => { + const translatedDateRanges = dateRanges(t); + const last24Hours = t("Last 24 Hours"); + const today = t("Today"); + + return ( + + onChange({ start: picker.startDate, end: picker.endDate }) + } + timePicker={true} + showDropdowns={true} + ranges={translatedDateRanges} + > + + + ); + } }, { Header: t("Type"), diff --git a/src/util/dateRanges.tsx b/src/util/dateRanges.tsx new file mode 100644 index 0000000..276fc2e --- /dev/null +++ b/src/util/dateRanges.tsx @@ -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()] +}); diff --git a/src/util/i18n.tsx b/src/util/i18n.tsx index f49cbb9..16e0ffa 100644 --- a/src/util/i18n.tsx +++ b/src/util/i18n.tsx @@ -34,7 +34,8 @@ export function setupI18n() { "query-log", "settings", "api-errors", - "preferences" + "preferences", + "time-ranges" ], nsSeparator: false, keySeparator: false,