From b50e500daeae9c1be54afef2da09644cdf9cdd7d Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 3 Oct 2023 15:36:23 -0700 Subject: [PATCH] scheduled_messages: Move update_send_later_options. Signed-off-by: Anders Kaseorg --- web/src/scheduled_messages.js | 26 ---------------------- web/src/scheduled_messages_popover.js | 31 +++++++++++++++++++++++++-- web/tests/scheduled_messages.test.js | 7 +++--- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/web/src/scheduled_messages.js b/web/src/scheduled_messages.js index a35c26d9ab..6b33b8e9d8 100644 --- a/web/src/scheduled_messages.js +++ b/web/src/scheduled_messages.js @@ -1,7 +1,6 @@ import $ from "jquery"; import render_compose_banner from "../templates/compose_banner/compose_banner.hbs"; -import render_send_later_modal_options from "../templates/send_later_modal_options.hbs"; import * as channel from "./channel"; import * as compose from "./compose"; @@ -16,7 +15,6 @@ import * as sub_store from "./sub_store"; import * as timerender from "./timerender"; export const MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS = 5 * 60; -export const SCHEDULING_MODAL_UPDATE_INTERVAL_IN_MILLISECONDS = 60 * 1000; // scheduled_messages_data is a dictionary where key=scheduled_message_id and value=scheduled_messages export const scheduled_messages_data = {}; @@ -297,27 +295,3 @@ export function initialize(scheduled_messages_params) { e.stopPropagation(); }); } - -// This function is exported for unit testing purposes. -export function should_update_send_later_options(date) { - const current_minute = date.getMinutes(); - const current_hour = date.getHours(); - - if (current_hour === 0 && current_minute === 0) { - // We need to rerender the available options at midnight, - // since Monday could become in range. - return true; - } - - // Rerender at MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS before the - // hour, so we don't offer a 4:00PM send time at 3:59 PM. - return current_minute === 60 - MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS / 60; -} - -export function update_send_later_options() { - const now = new Date(); - if (should_update_send_later_options(now)) { - const filtered_send_opts = get_filtered_send_opts(now); - $("#send_later_options").replaceWith(render_send_later_modal_options(filtered_send_opts)); - } -} diff --git a/web/src/scheduled_messages_popover.js b/web/src/scheduled_messages_popover.js index 227fd5e4c2..f313a1e66a 100644 --- a/web/src/scheduled_messages_popover.js +++ b/web/src/scheduled_messages_popover.js @@ -2,6 +2,7 @@ import $ from "jquery"; import {delegate} from "tippy.js"; import render_send_later_modal from "../templates/send_later_modal.hbs"; +import render_send_later_modal_options from "../templates/send_later_modal_options.hbs"; import render_send_later_popover from "../templates/send_later_popover.hbs"; import * as compose from "./compose"; @@ -13,6 +14,8 @@ import * as scheduled_messages from "./scheduled_messages"; import * as timerender from "./timerender"; import {parse_html} from "./ui_util"; +export const SCHEDULING_MODAL_UPDATE_INTERVAL_IN_MILLISECONDS = 60 * 1000; + let selected_send_later_timestamp; export function get_selected_send_later_timestamp() { @@ -63,8 +66,8 @@ export function open_send_later_menu() { autoremove: true, on_show() { interval = setInterval( - scheduled_messages.update_send_later_options, - scheduled_messages.SCHEDULING_MODAL_UPDATE_INTERVAL_IN_MILLISECONDS, + update_send_later_options, + SCHEDULING_MODAL_UPDATE_INTERVAL_IN_MILLISECONDS, ); const $send_later_modal = $("#send_later_modal"); @@ -172,3 +175,27 @@ export function initialize() { }, }); } + +// This function is exported for unit testing purposes. +export function should_update_send_later_options(date) { + const current_minute = date.getMinutes(); + const current_hour = date.getHours(); + + if (current_hour === 0 && current_minute === 0) { + // We need to rerender the available options at midnight, + // since Monday could become in range. + return true; + } + + // Rerender at MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS before the + // hour, so we don't offer a 4:00PM send time at 3:59 PM. + return current_minute === 60 - scheduled_messages.MINIMUM_SCHEDULED_MESSAGE_DELAY_SECONDS / 60; +} + +export function update_send_later_options() { + const now = new Date(); + if (should_update_send_later_options(now)) { + const filtered_send_opts = scheduled_messages.get_filtered_send_opts(now); + $("#send_later_options").replaceWith(render_send_later_modal_options(filtered_send_opts)); + } +} diff --git a/web/tests/scheduled_messages.test.js b/web/tests/scheduled_messages.test.js index c1d37109ec..0410edda8e 100644 --- a/web/tests/scheduled_messages.test.js +++ b/web/tests/scheduled_messages.test.js @@ -6,6 +6,7 @@ const {zrequire} = require("./lib/namespace"); const {run_test} = require("./lib/test"); const scheduled_messages = zrequire("scheduled_messages"); +const scheduled_messages_popover = zrequire("scheduled_messages_popover"); const per_day_stamps = { "2023-04-30": { @@ -164,7 +165,7 @@ run_test("should_update_send_later_options", () => { // We should rerender at midnight const start_of_the_day = new Date(); start_of_the_day.setHours(0, 0); - assert.ok(scheduled_messages.should_update_send_later_options(start_of_the_day)); + assert.ok(scheduled_messages_popover.should_update_send_later_options(start_of_the_day)); function get_minutes_to_hour(minutes) { const date = new Date(); @@ -179,10 +180,10 @@ run_test("should_update_send_later_options", () => { const current_time = get_minutes_to_hour(minute); if (minute === 55) { // Should rerender - assert.ok(scheduled_messages.should_update_send_later_options(current_time)); + assert.ok(scheduled_messages_popover.should_update_send_later_options(current_time)); } else { // Should not rerender - assert.ok(!scheduled_messages.should_update_send_later_options(current_time)); + assert.ok(!scheduled_messages_popover.should_update_send_later_options(current_time)); } } });