settings-input: Move time input check helper to web/src/util.

Moves the helper function for converting a time input into a
number so that it can be reused in both modals and settings
components.
This commit is contained in:
Lauryn Menard 2024-06-26 21:18:58 +02:00 committed by Tim Abbott
parent 8e3317d15a
commit 7d5a715ddf
3 changed files with 40 additions and 20 deletions

View File

@ -348,22 +348,6 @@ export function set_time_limit_setting(property_name: MessageTimeLimitSetting):
);
}
function check_valid_number_input(input_value: string, keep_number_as_float = false): number {
// This check is important to make sure that inputs like "24a" are
// considered invalid and this function returns NaN for such inputs.
// Number.parseInt and Number.parseFloat will convert strings like
// "24a" to 24.
if (Number.isNaN(Number(input_value))) {
return Number.NaN;
}
if (keep_number_as_float) {
return Number.parseFloat(Number.parseFloat(input_value).toFixed(1));
}
return Number.parseInt(input_value, 10);
}
function get_message_retention_setting_value(
$input_elem: JQuery<HTMLSelectElement>,
for_api_data = true,
@ -390,7 +374,7 @@ function get_message_retention_setting_value(
if (custom_input_val.length === 0) {
return settings_config.retain_message_forever;
}
return check_valid_number_input(custom_input_val);
return util.check_time_input(custom_input_val);
}
export const select_field_data_schema = z.record(z.object({text: z.string(), order: z.string()}));
@ -757,7 +741,7 @@ export function get_auth_method_list_data(): Record<string, boolean> {
}
export function parse_time_limit($elem: JQuery<HTMLInputElement>): number {
const time_limit_in_minutes = check_valid_number_input($elem.val()!, true);
const time_limit_in_minutes = util.check_time_input($elem.val()!, true);
return Math.floor(time_limit_in_minutes * 60);
}
@ -796,7 +780,7 @@ function get_time_limit_setting_value(
if ($input_elem.attr("id") === "id_realm_waiting_period_threshold") {
// For realm waiting period threshold setting, the custom input element contains
// number of days.
return check_valid_number_input($custom_input_elem.val()!);
return util.check_time_input($custom_input_elem.val()!);
}
return parse_time_limit($custom_input_elem);
@ -1336,7 +1320,7 @@ function should_disable_save_button_for_time_limit_settings(
const $custom_input_elem = $(setting_elem).find<HTMLInputElement>(
"input.time-limit-custom-input",
);
const custom_input_elem_val = check_valid_number_input($custom_input_elem.val()!);
const custom_input_elem_val = util.check_time_input($custom_input_elem.val()!);
const for_realm_default_settings =
$dropdown_elem.closest(".settings-section.show").attr("id") ===

View File

@ -451,6 +451,22 @@ export function get_custom_time_in_minutes(time_unit: string, time_input: number
}
}
export function check_time_input(input_value: string, keep_number_as_float = false): number {
// This check is important to make sure that inputs like "24a" are
// considered invalid and this function returns NaN for such inputs.
// Number.parseInt and Number.parseFloat will convert strings like
// "24a" to 24.
if (Number.isNaN(Number(input_value))) {
return Number.NaN;
}
if (keep_number_as_float) {
return Number.parseFloat(Number.parseFloat(input_value).toFixed(1));
}
return Number.parseInt(input_value, 10);
}
// Helper for shorthand for Typescript to get an item from a list with
// exactly one item.
export function the<T>(items: T[] | JQuery<T>): T {

View File

@ -404,6 +404,26 @@ run_test("get_custom_time_in_minutes", () => {
);
});
run_test("check_and_validate_custom_time_input", () => {
const input_is_nan = "24abc";
let checked_input = util.check_time_input(input_is_nan);
assert.equal(checked_input, Number.NaN);
const input_is_negative = "-24";
checked_input = util.check_time_input(input_is_negative);
assert.equal(checked_input, -24);
const input_is_float = "24.5";
checked_input = util.check_time_input(input_is_float);
assert.equal(checked_input, 24);
checked_input = util.check_time_input(input_is_float, true);
assert.equal(checked_input, 24.5);
const input_is_integer = "10";
checked_input = util.check_time_input(input_is_integer);
assert.equal(checked_input, 10);
});
run_test("the", () => {
const list_with_one_item = ["foo"];
assert.equal(util.the(list_with_one_item), "foo");