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") ===