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 <lauryn@zulip.com>
This commit is contained in:
opmkumar 2024-10-28 11:24:20 +05:30 committed by Tim Abbott
parent 789a47fb6b
commit ea252f0769
1 changed files with 23 additions and 21 deletions

View File

@ -255,42 +255,44 @@ function generate_multiuse_invite(): void {
}); });
} }
function valid_to(time_valid: number): string { function valid_to(): string {
if (!time_valid) { const $expires_in = $<HTMLSelectOneElement>("select:not([multiple])#expires_in");
const time_input_value = $expires_in.val()!;
if (time_input_value === "null") {
return $t({defaultMessage: "Never expires"}); 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. // 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 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"); const time = timerender.get_localized_date_or_time_for_format(valid_to, "time");
return $t({defaultMessage: "Expires on {date} at {time}"}, {date, 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 { function set_expires_on_text(): void {
const $expires_in = $<HTMLSelectOneElement>("select:not([multiple])#expires_in"); const $expires_in = $<HTMLSelectOneElement>("select:not([multiple])#expires_in");
const valid_to_text = valid_to();
if ($expires_in.val() === "custom") { if ($expires_in.val() === "custom") {
$("#expires_on").hide(); $("#expires_on").hide();
set_custom_expires_on_text(); $("#custom_expires_on").text(valid_to_text);
} else { } else {
$("#expires_on").show(); $("#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<Document, undefined>): void
custom_expiration_time_unit = $<HTMLSelectOneElement>( custom_expiration_time_unit = $<HTMLSelectOneElement>(
"select:not([multiple])#custom-expiration-time-unit", "select:not([multiple])#custom-expiration-time-unit",
).val()!; ).val()!;
set_custom_expires_on_text(); set_expires_on_text();
toggle_invite_submit_button(); toggle_invite_submit_button();
}); });