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

This commit is contained in:
Andrei Prigorshnev 2022-02-04 14:18:21 +01:00 committed by GitHub
parent f4c6a61855
commit c52e8ef8b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 34 deletions

View File

@ -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,
});
}

View File

@ -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);

View File

@ -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,
},
];
}