Skip to content

A platform for community discussion. Free, open, simple.

DEV: dry up formatting of time options in time-shortcut-picker (#15810)

Andrei Prigorshnevc52e8ef8b663
diff --git a/app/assets/javascripts/discourse/app/components/bookmark.js b/app/assets/javascripts/discourse/app/components/bookmark.js
index 0e1bbc10494..13035912ea6 100644
--- a/app/assets/javascripts/discourse/app/components/bookmark.js
+++ b/app/assets/javascripts/discourse/app/components/bookmark.js
@@ -297,9 +297,7 @@ export default Component.extend({
id: TIME_SHORTCUT_TYPES.POST_LOCAL_DATE,
label: "time_shortcut.post_local_date",
time: this._postLocalDate(),
- timeFormatted: this._postLocalDate().format(
- I18n.t("dates.long_no_year")
- ),
+ timeFormatKey: "dates.long_no_year",
hidden: false,
});
}
diff --git a/app/assets/javascripts/discourse/app/components/time-shortcut-picker.js b/app/assets/javascripts/discourse/app/components/time-shortcut-picker.js
index 10219e71fc7..5839191f48f 100644
--- a/app/assets/javascripts/discourse/app/components/time-shortcut-picker.js
+++ b/app/assets/javascripts/discourse/app/components/time-shortcut-picker.js
@@ -179,12 +179,6 @@ export default Component.extend({
let options = defaultShortcutOptions(userTimezone);
this._hideDynamicOptions(options);
-
- customOptions.forEach((opt) => {
- if (!opt.timeFormatted && opt.time) {
- opt.timeFormatted = opt.time.format(I18n.t(opt.timeFormatKey));
- }
- });
options = options.concat(customOptions);
options.sort((a, b) => {
@@ -205,9 +199,7 @@ export default Component.extend({
TIME_SHORTCUT_TYPES.LAST_CUSTOM
);
lastCustom.time = this.parsedLastCustomDatetime;
- lastCustom.timeFormatted = this.parsedLastCustomDatetime.format(
- I18n.t("dates.long_no_year")
- );
+ lastCustom.timeFormatKey = "dates.long_no_year";
lastCustom.hidden = false;
}
@@ -221,12 +213,8 @@ export default Component.extend({
});
}
- options.forEach((option) => {
- if (customLabels[option.id]) {
- option.label = customLabels[option.id];
- }
- });
-
+ this._applyCustomLabels(options, customLabels);
+ this._formatTime(options);
return options;
},
@@ -276,6 +264,22 @@ export default Component.extend({
}
},
+ _applyCustomLabels(options, customLabels) {
+ options.forEach((option) => {
+ if (customLabels[option.id]) {
+ option.label = customLabels[option.id];
+ }
+ });
+ },
+
+ _formatTime(options) {
+ options.forEach((option) => {
+ if (option.time && option.timeFormatKey) {
+ option.timeFormatted = option.time.format(I18n.t(option.timeFormatKey));
+ }
+ });
+ },
+
_hideDynamicOptions(options) {
if (now(this.userTimezone).hour() >= LATER_TODAY_CUTOFF_HOUR) {
this._hideOption(options, TIME_SHORTCUT_TYPES.LATER_TODAY);
diff --git a/app/assets/javascripts/discourse/app/lib/time-shortcut.js b/app/assets/javascripts/discourse/app/lib/time-shortcut.js
index 7ee30b041c0..37d6585cdf9 100644
--- a/app/assets/javascripts/discourse/app/lib/time-shortcut.js
+++ b/app/assets/javascripts/discourse/app/lib/time-shortcut.js
@@ -9,7 +9,6 @@ import {
thisWeekend,
tomorrow,
} from "discourse/lib/time-utils";
-import I18n from "I18n";
export const TIME_SHORTCUT_TYPES = {
LATER_TODAY: "later_today",
@@ -32,32 +31,28 @@ export function defaultShortcutOptions(timezone) {
id: TIME_SHORTCUT_TYPES.LATER_TODAY,
label: "time_shortcut.later_today",
time: laterToday(timezone),
- timeFormatted: laterToday(timezone).format(I18n.t("dates.time")),
+ timeFormatKey: "dates.time",
},
{
icon: "far-sun",
id: TIME_SHORTCUT_TYPES.TOMORROW,
label: "time_shortcut.tomorrow",
time: tomorrow(timezone),
- timeFormatted: tomorrow(timezone).format(I18n.t("dates.time_short_day")),
+ timeFormatKey: "dates.time_short_day",
},
{
icon: "angle-double-right",
id: TIME_SHORTCUT_TYPES.LATER_THIS_WEEK,
label: "time_shortcut.later_this_week",
time: laterThisWeek(timezone),
- timeFormatted: laterThisWeek(timezone).format(
- I18n.t("dates.time_short_day")
- ),
+ timeFormatKey: "dates.time_short_day",
},
{
icon: "bed",
id: TIME_SHORTCUT_TYPES.THIS_WEEKEND,
label: "time_shortcut.this_weekend",
time: thisWeekend(timezone),
- timeFormatted: thisWeekend(timezone).format(
- I18n.t("dates.time_short_day")
- ),
+ timeFormatKey: "dates.time_short_day",
},
{
icon: "briefcase",
@@ -68,16 +63,14 @@ export function defaultShortcutOptions(timezone) {
? "time_shortcut.start_of_next_business_week_alt"
: "time_shortcut.start_of_next_business_week",
time: nextBusinessWeekStart(timezone),
- timeFormatted: nextBusinessWeekStart(timezone).format(
- I18n.t("dates.long_no_year")
- ),
+ timeFormatKey: "dates.long_no_year",
},
{
icon: "far-calendar-plus",
id: TIME_SHORTCUT_TYPES.NEXT_MONTH,
label: "time_shortcut.next_month",
time: nextMonth(timezone),
- timeFormatted: nextMonth(timezone).format(I18n.t("dates.long_no_year")),
+ timeFormatKey: "dates.long_no_year",
},
];
}
@@ -89,7 +82,6 @@ export function specialShortcutOptions() {
id: TIME_SHORTCUT_TYPES.LAST_CUSTOM,
label: "time_shortcut.last_custom",
time: null,
- timeFormatted: null,
hidden: true,
},
{
@@ -97,7 +89,6 @@ export function specialShortcutOptions() {
id: TIME_SHORTCUT_TYPES.CUSTOM,
label: "time_shortcut.custom",
time: null,
- timeFormatted: null,
isCustomTimeShortcut: true,
},
{
@@ -105,7 +96,6 @@ export function specialShortcutOptions() {
id: TIME_SHORTCUT_TYPES.NONE,
label: "time_shortcut.none",
time: null,
- timeFormatted: null,
},
];
}