From 671547345d8269de4b2b00f25c97cfaaefc6553d Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Tue, 28 May 2024 19:22:21 +0530 Subject: [PATCH] settings: Refactor code to handle realm authentication methods. This commit refactors code to handle realm authentication methods in a better way. We remove the get_complete_data_for_subsection function and instead let populate_data_for_request handle the request data for authentication methods. We also remove the "prop-element" class from each authentication setting checkbox and instead add it to the container div as a the list of those checkboxes represent a single field, i.e. the realm_authentication_methods field received from the server and the authentication_methods field sent to 'PATCH /realm' endpoint. --- web/src/settings_components.ts | 12 ++++--- web/src/settings_org.js | 35 +++---------------- .../settings/admin_auth_methods_list.hbs | 3 +- .../settings/auth_methods_settings_admin.hbs | 2 +- web/templates/settings/settings_checkbox.hbs | 2 +- 5 files changed, 16 insertions(+), 38 deletions(-) diff --git a/web/src/settings_components.ts b/web/src/settings_components.ts index bb41cd48c6..63328c0e0f 100644 --- a/web/src/settings_components.ts +++ b/web/src/settings_components.ts @@ -164,6 +164,10 @@ export function get_property_value( return "no_restriction"; } + if (property_name === "realm_authentication_methods") { + return JSON.stringify(realm_authentication_methods_to_boolean_dict()); + } + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return realm[property_name as keyof RealmSetting] as valueof; } @@ -728,6 +732,8 @@ export function get_input_element_value( return get_field_data_input_value($input_elem); case "language-setting": return $input_elem.find(".language_selection_button span").attr("data-language-code"); + case "auth-methods": + return JSON.stringify(get_auth_method_list_data()); default: return undefined; } @@ -834,7 +840,7 @@ export function check_property_changed( $elem, for_realm_default_settings, ) as setting_property_type; - let current_val = get_property_value( + const current_val = get_property_value( property_name, for_realm_default_settings, sub, @@ -846,9 +852,7 @@ export function check_property_changed( switch (property_name) { case "realm_authentication_methods": - current_val = JSON.stringify(realm_authentication_methods_to_boolean_dict()); - proposed_val = get_auth_method_list_data(); - proposed_val = JSON.stringify(proposed_val); + proposed_val = get_input_element_value(elem, "auth-methods"); break; case "realm_new_stream_announcements_stream_id": case "realm_signup_announcements_stream_id": diff --git a/web/src/settings_org.js b/web/src/settings_org.js index 63a3cbea29..10ce02fb50 100644 --- a/web/src/settings_org.js +++ b/web/src/settings_org.js @@ -876,43 +876,16 @@ export function register_save_discard_widget_handlers( settings_components.change_save_button_state($save_btn_controls, "discarded"); }); - function get_complete_data_for_subsection(subsection) { - let data = {}; - - switch (subsection) { - case "auth_settings": - data = {}; - data.authentication_methods = JSON.stringify( - settings_components.get_auth_method_list_data(), - ); - break; - } - return data; - } - $container.on("click", ".subsection-header .subsection-changes-save button", (e) => { e.preventDefault(); e.stopPropagation(); const $save_button = $(e.currentTarget); const $subsection_elem = $save_button.closest(".settings-subsection-parent"); - let extra_data = {}; - if (!for_realm_default_settings) { - // The organization settings system has some coupled - // fields that must be submitted together, which is - // managed by the get_complete_data_for_subsection function. - const [, subsection_id] = /^org-(.*)$/.exec($subsection_elem.attr("id")); - const subsection = subsection_id.replaceAll("-", "_"); - extra_data = get_complete_data_for_subsection(subsection); - } - - const data = { - ...settings_components.populate_data_for_request( - $subsection_elem, - for_realm_default_settings, - ), - ...extra_data, - }; + const data = settings_components.populate_data_for_request( + $subsection_elem, + for_realm_default_settings, + ); save_organization_settings(data, $save_button, patch_url); }); } diff --git a/web/templates/settings/admin_auth_methods_list.hbs b/web/templates/settings/admin_auth_methods_list.hbs index 1c91cc817e..1be3180164 100644 --- a/web/templates/settings/admin_auth_methods_list.hbs +++ b/web/templates/settings/admin_auth_methods_list.hbs @@ -5,5 +5,6 @@ is_checked=enabled label=method is_disabled=disable_configure_auth_method - tooltip_text=unavailable_reason}} + tooltip_text=unavailable_reason + skip_prop_element=true}} diff --git a/web/templates/settings/auth_methods_settings_admin.hbs b/web/templates/settings/auth_methods_settings_admin.hbs index 6953f5b553..4a357729db 100644 --- a/web/templates/settings/auth_methods_settings_admin.hbs +++ b/web/templates/settings/auth_methods_settings_admin.hbs @@ -19,7 +19,7 @@

{{t "Configure the authentication methods for your organization."}}

-
+
{{! Empty div is intentional, it will get populated by a dedicated template }}
diff --git a/web/templates/settings/settings_checkbox.hbs b/web/templates/settings/settings_checkbox.hbs index 3109b10d1b..9f2d0424b9 100644 --- a/web/templates/settings/settings_checkbox.hbs +++ b/web/templates/settings/settings_checkbox.hbs @@ -1,7 +1,7 @@ {{#unless (eq render_only false)}}