invite-user-modal: Generalize converting custom input into minutes.

Moves custom time input helper to `web/src/util.ts` so that it
can be reused for other modals where users can select a custom
time duration.

Co-authored by: Ujjawal Modi <umodi2003@gmail.com>
This commit is contained in:
Lauryn Menard 2024-06-19 21:57:28 +02:00 committed by Tim Abbott
parent cc1a3eeec1
commit b8888d2c33
3 changed files with 53 additions and 17 deletions

View File

@ -67,7 +67,10 @@ function get_common_invitation_data(): {
if (raw_expires_in === "null") { if (raw_expires_in === "null") {
expires_in = null; expires_in = null;
} else if (raw_expires_in === "custom") { } else if (raw_expires_in === "custom") {
expires_in = get_expiration_time_in_minutes(); expires_in = util.get_custom_time_in_minutes(
custom_expiration_time_unit,
custom_expiration_time_input,
);
} else { } else {
expires_in = Number.parseFloat(raw_expires_in); expires_in = Number.parseFloat(raw_expires_in);
} }
@ -151,7 +154,10 @@ function submit_invitation_form(): void {
if ($expires_in.val() === "custom") { if ($expires_in.val() === "custom") {
// Hide the custom inputs if the custom input is set // Hide the custom inputs if the custom input is set
// to one of the dropdown's standard options. // to one of the dropdown's standard options.
const time_in_minutes = get_expiration_time_in_minutes(); const time_in_minutes = util.get_custom_time_in_minutes(
custom_expiration_time_unit,
custom_expiration_time_input,
);
for (const option of Object.values(settings_config.expires_in_values)) { for (const option of Object.values(settings_config.expires_in_values)) {
if (option.value === time_in_minutes) { if (option.value === time_in_minutes) {
$("#custom-invite-expiration-time").hide(); $("#custom-invite-expiration-time").hide();
@ -261,24 +267,18 @@ function valid_to(time_valid: number): string {
return $t({defaultMessage: "Expires on {date} at {time}"}, {date, time}); return $t({defaultMessage: "Expires on {date} at {time}"}, {date, time});
} }
function get_expiration_time_in_minutes(): number {
switch (custom_expiration_time_unit) {
case "hours":
return custom_expiration_time_input * 60;
case "days":
return custom_expiration_time_input * 24 * 60;
case "weeks":
return custom_expiration_time_input * 7 * 24 * 60;
default:
return custom_expiration_time_input;
}
}
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");
if ($expires_in.val() === "custom") { if ($expires_in.val() === "custom") {
$("#expires_on").hide(); $("#expires_on").hide();
$("#custom_expires_on").text(valid_to(get_expiration_time_in_minutes())); $("#custom_expires_on").text(
valid_to(
util.get_custom_time_in_minutes(
custom_expiration_time_unit,
custom_expiration_time_input,
),
),
);
} else { } else {
$("#expires_on").show(); $("#expires_on").show();
$("#expires_on").text(valid_to(Number.parseFloat($expires_in.val()!))); $("#expires_on").text(valid_to(Number.parseFloat($expires_in.val()!)));
@ -449,7 +449,14 @@ 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()!;
$("#custom_expires_on").text(valid_to(get_expiration_time_in_minutes())); $("#custom_expires_on").text(
valid_to(
util.get_custom_time_in_minutes(
custom_expiration_time_unit,
custom_expiration_time_input,
),
),
);
}); });
$("#invite_check_all_button").on("click", () => { $("#invite_check_all_button").on("click", () => {

View File

@ -438,6 +438,19 @@ export function get_remaining_time(start_time: number, duration: number): number
return Math.max(0, start_time + duration - Date.now()); return Math.max(0, start_time + duration - Date.now());
} }
export function get_custom_time_in_minutes(time_unit: string, time_input: number): number {
switch (time_unit) {
case "hours":
return time_input * 60;
case "days":
return time_input * 24 * 60;
case "weeks":
return time_input * 7 * 24 * 60;
default:
return time_input;
}
}
// Helper for shorthand for Typescript to get an item from a list with // Helper for shorthand for Typescript to get an item from a list with
// exactly one item. // exactly one item.
export function the<T>(items: T[] | JQuery<T>): T { export function the<T>(items: T[] | JQuery<T>): T {

View File

@ -388,6 +388,22 @@ run_test("get_remaining_time", () => {
MockDate.reset(); MockDate.reset();
}); });
run_test("get_custom_time_in_minutes", () => {
const time_input = 15;
assert.equal(util.get_custom_time_in_minutes("weeks", time_input), time_input * 7 * 24 * 60);
assert.equal(util.get_custom_time_in_minutes("days", time_input), time_input * 24 * 60);
assert.equal(util.get_custom_time_in_minutes("hours", time_input), time_input * 60);
assert.equal(util.get_custom_time_in_minutes("minutes", time_input), time_input);
// Unknown time unit returns same time input
assert.equal(util.get_custom_time_in_minutes("invalid", time_input), time_input);
/// NaN time input returns NaN
const invalid_time_input = Number.NaN;
assert.equal(
util.get_custom_time_in_minutes("minutes", invalid_time_input),
invalid_time_input,
);
});
run_test("the", () => { run_test("the", () => {
const list_with_one_item = ["foo"]; const list_with_one_item = ["foo"];
assert.equal(util.the(list_with_one_item), "foo"); assert.equal(util.the(list_with_one_item), "foo");