settings: Extract code for disabling save button in a function.

This is a prep commit so that we can reuse the function to enable
or disable the save button when changing the message edit and delete
setting dropdown.
This commit is contained in:
Sahil Batra 2022-08-22 20:21:23 +05:30 committed by Tim Abbott
parent 1da7086c35
commit f3c15affd7
1 changed files with 30 additions and 26 deletions

View File

@ -1148,6 +1148,35 @@ export function build_page() {
register_save_discard_widget_handlers($(".admin-realm-form"), "/json/realm", false);
function update_save_button_state(e) {
const edit_limit_value = Number.parseInt(
$("#id_realm_message_content_edit_limit_minutes").val(),
10,
);
const delete_limit_value = Number.parseInt(
$("#id_realm_message_content_delete_limit_minutes").val(),
10,
);
const msg_edit_limit_dropdown_value = $("#id_realm_msg_edit_limit_setting").val();
const edit_limit_custom_value_invalid =
msg_edit_limit_dropdown_value === "custom_limit" &&
(edit_limit_value <= 0 || Number.isNaN(edit_limit_value));
const msg_delete_limit_dropdown_value = $("#id_realm_msg_delete_limit_setting").val();
const delete_limit_custom_value_invalid =
msg_delete_limit_dropdown_value === "custom_limit" &&
(delete_limit_value <= 0 || Number.isNaN(delete_limit_value));
const disable_save_btn =
edit_limit_custom_value_invalid || delete_limit_custom_value_invalid;
$(e.target)
.closest(".org-subsection-parent")
.find(".subsection-changes-save button")
.prop("disabled", disable_save_btn);
}
$(".org-subsection-parent").on("keydown", "input", (e) => {
e.stopPropagation();
if (e.key === "Enter") {
@ -1249,32 +1278,7 @@ export function build_page() {
});
$("#org-msg-editing").on("input", ".admin-realm-time-limit-input", (e) => {
const edit_limit_value = Number.parseInt(
$("#id_realm_message_content_edit_limit_minutes").val(),
10,
);
const delete_limit_value = Number.parseInt(
$("#id_realm_message_content_delete_limit_minutes").val(),
10,
);
const msg_edit_limit_dropdown_value = $("#id_realm_msg_edit_limit_setting").val();
const edit_limit_custom_value_invalid =
msg_edit_limit_dropdown_value === "custom_limit" &&
(edit_limit_value <= 0 || Number.isNaN(edit_limit_value));
const msg_delete_limit_dropdown_value = $("#id_realm_msg_delete_limit_setting").val();
const delete_limit_custom_value_invalid =
msg_delete_limit_dropdown_value === "custom_limit" &&
(delete_limit_value <= 0 || Number.isNaN(delete_limit_value));
const disable_save_btn =
edit_limit_custom_value_invalid || delete_limit_custom_value_invalid;
$(e.target)
.closest(".org-subsection-parent")
.find(".subsection-changes-save button")
.prop("disabled", disable_save_btn);
update_save_button_state(e);
});
$("#show_realm_domains_modal").on("click", (e) => {