From ea252f07695509f7f2bcf35575b6ca3f02309f44 Mon Sep 17 00:00:00 2001 From: opmkumar Date: Mon, 28 Oct 2024 11:24:20 +0530 Subject: [PATCH] invites: Refactor `valid_to` function. Refactors the `valid_to` function to return the correct formatted string for all cases (custom and preset) of invitation expiration input values. Co-authored-by: Lauryn Menard --- web/src/invite.ts | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/web/src/invite.ts b/web/src/invite.ts index c37857a6ca..abc357557b 100644 --- a/web/src/invite.ts +++ b/web/src/invite.ts @@ -255,42 +255,44 @@ function generate_multiuse_invite(): void { }); } -function valid_to(time_valid: number): string { - if (!time_valid) { +function valid_to(): string { + const $expires_in = $("select:not([multiple])#expires_in"); + const time_input_value = $expires_in.val()!; + + if (time_input_value === "null") { return $t({defaultMessage: "Never expires"}); } + let time_in_minutes: number; + if (time_input_value === "custom") { + if (!util.validate_custom_time_input(custom_expiration_time_input)) { + return $t({defaultMessage: "Invalid custom time"}); + } + time_in_minutes = util.get_custom_time_in_minutes( + custom_expiration_time_unit, + custom_expiration_time_input, + ); + } else { + time_in_minutes = Number.parseFloat(time_input_value); + } + // The below is a duplicate of timerender.get_full_datetime, with a different base string. - const valid_to = add(new Date(), {minutes: time_valid}); + const valid_to = add(new Date(), {minutes: time_in_minutes}); const date = timerender.get_localized_date_or_time_for_format(valid_to, "dayofyear_year"); const time = timerender.get_localized_date_or_time_for_format(valid_to, "time"); return $t({defaultMessage: "Expires on {date} at {time}"}, {date, time}); } -function set_custom_expires_on_text(): void { - if (util.validate_custom_time_input(custom_expiration_time_input)) { - $("#custom_expires_on").text( - valid_to( - util.get_custom_time_in_minutes( - custom_expiration_time_unit, - custom_expiration_time_input, - ), - ), - ); - } else { - $("#custom_expires_on").text($t({defaultMessage: "Invalid custom time"})); - } -} - function set_expires_on_text(): void { const $expires_in = $("select:not([multiple])#expires_in"); + const valid_to_text = valid_to(); if ($expires_in.val() === "custom") { $("#expires_on").hide(); - set_custom_expires_on_text(); + $("#custom_expires_on").text(valid_to_text); } else { $("#expires_on").show(); - $("#expires_on").text(valid_to(Number.parseFloat($expires_in.val()!))); + $("#expires_on").text(valid_to_text); } } @@ -454,7 +456,7 @@ function open_invite_user_modal(e: JQuery.ClickEvent): void custom_expiration_time_unit = $( "select:not([multiple])#custom-expiration-time-unit", ).val()!; - set_custom_expires_on_text(); + set_expires_on_text(); toggle_invite_submit_button(); });