FEATURE: Show relative time when date is omitted (#18547)

This commit is contained in:
Natalie Tay 2022-10-12 11:23:34 +08:00 committed by GitHub
parent 84f2529138
commit 1631394826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 10 deletions

View File

@ -67,12 +67,12 @@ export default class LocalDateBuilder {
} }
const previews = this._generatePreviews(localDate, displayedTimezone); const previews = this._generatePreviews(localDate, displayedTimezone);
const hasTime = hour !== undefined;
return { return {
pastEvent: pastEvent:
!this.recurring && !this.recurring &&
moment.tz(this.localTimezone).isAfter(localDate.datetime), moment.tz(this.localTimezone).isAfter(localDate.datetime),
formatted: this._applyFormatting(localDate, displayedTimezone), formatted: this._applyFormatting(localDate, displayedTimezone, hasTime),
previews, previews,
textPreview: this._generateTextPreviews(previews), textPreview: this._generateTextPreviews(previews),
}; };
@ -210,7 +210,7 @@ export default class LocalDateBuilder {
return duration < 0 ? dates.reverse() : dates; return duration < 0 ? dates.reverse() : dates;
} }
_applyFormatting(localDate, displayedTimezone) { _applyFormatting(localDate, displayedTimezone, hasTime) {
if (this.countdown) { if (this.countdown) {
const diffTime = moment.tz(this.localTimezone).diff(localDate.datetime); const diffTime = moment.tz(this.localTimezone).diff(localDate.datetime);
@ -241,7 +241,7 @@ export default class LocalDateBuilder {
if (inCalendarRange && sameTimezone) { if (inCalendarRange && sameTimezone) {
const date = localDate.datetimeWithZone(this.localTimezone); const date = localDate.datetimeWithZone(this.localTimezone);
if (date.hours() === 0 && date.minutes() === 0) { if (hasTime && date.hours() === 0 && date.minutes() === 0) {
return date.format("dddd"); return date.format("dddd");
} }

View File

@ -1,6 +1,6 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import { applyLocalDates } from "../initializers/discourse-local-dates"; import { applyLocalDates } from "../initializers/discourse-local-dates";
import { freezeTime } from "../lib/local-date-builder-test"; import { freezeTime } from "./local-date-builder-test";
module("Unit | Discourse Local Dates | discourse-local-dates", function () { module("Unit | Discourse Local Dates | discourse-local-dates", function () {
function createElementFromHTML(htmlString) { function createElementFromHTML(htmlString) {
@ -72,7 +72,7 @@ module("Unit | Discourse Local Dates | discourse-local-dates", function () {
); );
assert.equal( assert.equal(
to.querySelector(".relative-time").textContent, to.querySelector(".relative-time").textContent,
"Thursday" "Yesterday"
); );
} }
); );

View File

@ -1,5 +1,5 @@
import I18n from "I18n"; import I18n from "I18n";
import LocalDateBuilder from "./local-date-builder"; import LocalDateBuilder from "../lib/local-date-builder";
import sinon from "sinon"; import sinon from "sinon";
import QUnit, { module, test } from "qunit"; import QUnit, { module, test } from "qunit";
@ -10,6 +10,7 @@ const NEW_YORK = "America/New_York";
const PARIS = "Europe/Paris"; const PARIS = "Europe/Paris";
const LAGOS = "Africa/Lagos"; const LAGOS = "Africa/Lagos";
const LONDON = "Europe/London"; const LONDON = "Europe/London";
const SINGAPORE = "Asia/Singapore";
export function freezeTime({ date, timezone }, cb) { export function freezeTime({ date, timezone }, cb) {
date = date || "2020-01-22 10:34"; date = date || "2020-01-22 10:34";
@ -61,7 +62,7 @@ QUnit.assert.buildsCorrectDate = function (options, expected, message) {
} }
}; };
module("lib:local-date-builder", function () { module("Unit | Library | local-date-builder", function () {
test("date", function (assert) { test("date", function (assert) {
freezeTime({ date: "2020-03-11" }, () => { freezeTime({ date: "2020-03-11" }, () => {
assert.buildsCorrectDate( assert.buildsCorrectDate(
@ -70,6 +71,23 @@ module("lib:local-date-builder", function () {
"it displays the date without time" "it displays the date without time"
); );
}); });
freezeTime({ date: "2022-10-11", timezone: "Asia/Singapore" }, () => {
const localDateBuilder = new LocalDateBuilder(
{
date: "2022-10-12",
timezone: SINGAPORE,
localTimezone: SINGAPORE,
},
SINGAPORE
);
assert.strictEqual(
localDateBuilder.build().formatted,
"Tomorrow",
"Displays relative day"
);
});
}); });
test("date and time", function (assert) { test("date and time", function (assert) {
@ -91,8 +109,8 @@ module("lib:local-date-builder", function () {
{ {
time: "12:22:00", time: "12:22:00",
date: "2022-10-07", date: "2022-10-07",
timezone: "Asia/Singapore", timezone: SINGAPORE,
localTimezone: "Asia/Singapore", localTimezone: SINGAPORE,
sameLocalDayAsFrom: true, sameLocalDayAsFrom: true,
}, },
{ formatted: "12:22 PM (Singapore)" }, { formatted: "12:22 PM (Singapore)" },