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.
This commit is contained in:
Sahil Batra 2024-05-28 19:22:21 +05:30 committed by Tim Abbott
parent 431145d4f2
commit 671547345d
5 changed files with 16 additions and 38 deletions

View File

@ -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<RealmSetting>;
}
@ -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":

View File

@ -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);
});
}

View File

@ -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}}
</div>

View File

@ -19,7 +19,7 @@
<div>
<p>{{t "Configure the authentication methods for your organization."}}</p>
<div id="id_realm_authentication_methods">
<div id="id_realm_authentication_methods" class="prop-element" data-setting-widget-type="auth-methods">
{{! Empty div is intentional, it will get populated by a dedicated template }}
</div>
</div>

View File

@ -1,7 +1,7 @@
{{#unless (eq render_only false)}}
<div class="input-group {{#if is_disabled}}control-label-disabled{{/if}}">
<label class="checkbox">
<input type="checkbox" class="{{setting_name}} inline-block setting-widget prop-element" name="{{setting_name}}" data-setting-widget-type="boolean"
<input type="checkbox" class="{{setting_name}} inline-block setting-widget {{#unless skip_prop_element}}prop-element{{/unless}}" name="{{setting_name}}" data-setting-widget-type="boolean"
id="{{#if prefix}}{{prefix}}{{/if}}{{setting_name}}" {{#if is_checked}}checked="checked"{{/if}} {{#if is_disabled}}disabled{{/if}} />
<span></span>
</label>