Skip to content

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

FIX: Clamp date-time range ends through a shared helper (#42978)

Régis Hanol54e30e47d3e3
diff --git a/frontend/discourse/app/lib/time-utils.js b/frontend/discourse/app/lib/time-utils.js
index 0836253b818..cb82a8c2545 100644
--- a/frontend/discourse/app/lib/time-utils.js
+++ b/frontend/discourse/app/lib/time-utils.js
@@ -93,6 +93,22 @@ export function nextBusinessWeekStart(timezone) {
return startOfDay(now(timezone).add(7, "days")).day(MOMENT_MONDAY);
}
+export function adjustedRangeEnd(from, to, { dateOnly = false } = {}) {
+ if (!from || !to) {
+ return to;
+ }
+
+ if (dateOnly) {
+ return to.isBefore(from, "day") ? from.clone() : to;
+ }
+
+ if (to.isBefore(from) || to.isSame(from)) {
+ return from.clone().add(1, "hour");
+ }
+
+ return to;
+}
+
export function parseCustomDatetime(
date,
time,
diff --git a/frontend/discourse/app/ui-kit/d-date-time-input-range.gjs b/frontend/discourse/app/ui-kit/d-date-time-input-range.gjs
index ae041cd64f9..25910d3d76e 100644
--- a/frontend/discourse/app/ui-kit/d-date-time-input-range.gjs
+++ b/frontend/discourse/app/ui-kit/d-date-time-input-range.gjs
@@ -3,6 +3,7 @@ import Component from "@ember/component";
import { fn, hash } from "@ember/helper";
import { action } from "@ember/object";
import { tagName } from "@ember-decorators/component";
+import { adjustedRangeEnd } from "discourse/lib/time-utils";
import DDateTimeInput from "discourse/ui-kit/d-date-time-input";
import { i18n } from "discourse-i18n";
@@ -10,8 +11,6 @@ import { i18n } from "discourse-i18n";
export default class DDateTimeInputRange extends Component {
from = null;
to = null;
- onChangeTo = null;
- onChangeFrom = null;
toTimeFirst = false;
showToTime = true;
showFromTime = true;
@@ -19,34 +18,17 @@ export default class DDateTimeInputRange extends Component {
@action
onChangeRanges(options, value) {
- if (this.onChange) {
- const state = {
- from: this.from,
- to: this.to,
- };
-
- const diff = {};
-
- if (options.prop === "from") {
- if (this.to && value?.isAfter(this.to)) {
- diff[options.prop] = value;
- diff["to"] = value.clone().add(1, "hour");
- } else {
- diff[options.prop] = value;
- }
- }
+ if (!this.onChange) {
+ return;
+ }
- if (options.prop === "to") {
- if (value && value.isBefore(this.from)) {
- diff[options.prop] = this.from.clone().add(1, "hour");
- } else {
- diff[options.prop] = value;
- }
- }
+ const from = options.prop === "from" ? value : this.from;
+ const to = options.prop === "from" ? this.to : value;
- const newState = { ...state, ...diff };
- this.onChange(newState);
- }
+ this.onChange({
+ from,
+ to: adjustedRangeEnd(from, to, { dateOnly: !this.showToTime }),
+ });
}
<template>
diff --git a/frontend/discourse/tests/integration/ui-kit/d-date-time-input-range-test.gjs b/frontend/discourse/tests/integration/ui-kit/d-date-time-input-range-test.gjs
index 55bb498daad..9a3ad38e808 100644
--- a/frontend/discourse/tests/integration/ui-kit/d-date-time-input-range-test.gjs
+++ b/frontend/discourse/tests/integration/ui-kit/d-date-time-input-range-test.gjs
@@ -73,6 +73,88 @@ module("Integration | ui-kit | DDateTimeInputRange", function (hooks) {
assert.dom(rows[5]).hasAttribute("data-name", "4:15 PM");
});
+ test("picking an end equal to start pushes the end an hour later", async function (assert) {
+ this.setProperties({
+ state: { from: DEFAULT_DATE_TIME, to: moment("2019-01-29 16:45") },
+ });
+
+ await render(
+ <template>
+ <DDateTimeInputRange
+ @from={{this.state.from}}
+ @onChange={{fn (mut this.state)}}
+ @to={{this.state.to}}
+ />
+ </template>
+ );
+
+ const toTimeSelectKit = selectKit(".to .d-time-input .select-kit");
+ await toTimeSelectKit.expand();
+ await toTimeSelectKit.selectRowByName("2:45 PM");
+
+ assert.strictEqual(
+ this.state.to.format("YYYY-MM-DD h:mm A"),
+ "2019-01-29 3:45 PM",
+ "a zero-length range is not allowed when times are shown"
+ );
+ });
+
+ test("moving the start onto the end pushes the end an hour later", async function (assert) {
+ this.setProperties({
+ state: { from: DEFAULT_DATE_TIME, to: moment("2019-01-29 15:45") },
+ });
+
+ await render(
+ <template>
+ <DDateTimeInputRange
+ @from={{this.state.from}}
+ @onChange={{fn (mut this.state)}}
+ @to={{this.state.to}}
+ />
+ </template>
+ );
+
+ const fromTimeSelectKit = selectKit(".from .d-time-input .select-kit");
+ await fromTimeSelectKit.expand();
+ await fromTimeSelectKit.selectRowByName("3:45 PM");
+
+ assert.strictEqual(
+ this.state.from.format("YYYY-MM-DD h:mm A"),
+ "2019-01-29 3:45 PM"
+ );
+ assert.strictEqual(
+ this.state.to.format("YYYY-MM-DD h:mm A"),
+ "2019-01-29 4:45 PM",
+ "the end keeps a positive duration when the start catches up to it"
+ );
+ });
+
+ test("a single-day range stays intact when times are hidden", async function (assert) {
+ this.setProperties({
+ state: { from: moment("2019-01-29"), to: null },
+ });
+
+ await render(
+ <template>
+ <DDateTimeInputRange
+ @from={{this.state.from}}
+ @onChange={{fn (mut this.state)}}
+ @showFromTime={{false}}
+ @showToTime={{false}}
+ @to={{this.state.to}}
+ />
+ </template>
+ );
+
+ await fillIn(".to.d-date-time-input .date-picker", "2019-01-29");
+
+ assert.strictEqual(
+ this.state.to.format("YYYY-MM-DD HH:mm"),
+ "2019-01-29 00:00",
+ "equal boundaries are a valid date-only range"
+ );
+ });
+
test("timezone support", async function (assert) {
this.setProperties({
state: {
diff --git a/frontend/discourse/tests/unit/lib/time-utils-test.js b/frontend/discourse/tests/unit/lib/time-utils-test.js
index 4c3878196c4..c41417bdd99 100644
--- a/frontend/discourse/tests/unit/lib/time-utils-test.js
+++ b/frontend/discourse/tests/unit/lib/time-utils-test.js
@@ -1,6 +1,7 @@
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import {
+ adjustedRangeEnd,
laterThisWeek,
laterToday,
nextMonth,
@@ -14,6 +15,50 @@ const timezone = "Australia/Brisbane";
module("Unit | lib | timeUtils", function (hooks) {
setupTest(hooks);
+ test("adjustedRangeEnd leaves a missing boundary alone", function (assert) {
+ const from = moment("2026-07-01 10:00");
+
+ assert.strictEqual(adjustedRangeEnd(null, null), null);
+ assert.strictEqual(adjustedRangeEnd(from, null), null);
+ });
+
+ test("adjustedRangeEnd pushes an end at or before the start an hour past it", function (assert) {
+ const from = moment("2026-07-01 10:00");
+
+ assert.strictEqual(
+ adjustedRangeEnd(from, from.clone()).format("HH:mm"),
+ "11:00"
+ );
+ assert.strictEqual(
+ adjustedRangeEnd(from, moment("2026-07-01 08:00")).format("HH:mm"),
+ "11:00"
+ );
+ });
+
+ test("adjustedRangeEnd keeps a valid end", function (assert) {
+ const from = moment("2026-07-01 10:00");
+ const to = moment("2026-07-01 12:00");
+
+ assert.strictEqual(adjustedRangeEnd(from, to), to);
+ });
+
+ test("adjustedRangeEnd allows equal boundaries when dateOnly", function (assert) {
+ const from = moment("2026-07-01");
+ const to = moment("2026-07-01");
+
+ assert.strictEqual(adjustedRangeEnd(from, to, { dateOnly: true }), to);
+ });
+
+ test("adjustedRangeEnd snaps an earlier end to the start day when dateOnly", function (assert) {
+ const from = moment("2026-07-01");
+ const to = moment("2026-06-20");
+
+ assert.strictEqual(
+ adjustedRangeEnd(from, to, { dateOnly: true }).format("YYYY-MM-DD"),
+ "2026-07-01"
+ );
+ });
+
test("nextMonth gets next month correctly", function (assert) {
withFrozenTime("2019-12-11T08:00:00", timezone, () => {
assert.strictEqual(