settings: Add functions to discard complete subsection.

This commit adds new functions which will be used to discard
changes for all the settings in a subsection when clicking
on discard button. This change will help in avoiding code
duplication when they will be used to discard changes in
a subsection if some other user changed a setting in the
same subsection.
This commit is contained in:
Sahil Batra
2024-06-28 16:53:35 +05:30
committed by Tim Abbott
parent da95d01c37
commit cb69a819d4
4 changed files with 47 additions and 22 deletions

View File

@@ -639,6 +639,38 @@ export function discard_realm_default_property_element_changes(elem) {
update_dependent_subsettings(property_name); update_dependent_subsettings(property_name);
} }
function discard_realm_settings_subsection_changes($subsection) {
for (const elem of settings_components.get_subsection_property_elements($subsection)) {
discard_realm_property_element_changes(elem);
}
const $save_btn_controls = $subsection.find(".save-button-controls");
settings_components.change_save_button_state($save_btn_controls, "discarded");
}
export function discard_stream_settings_subsection_changes($subsection, sub) {
for (const elem of settings_components.get_subsection_property_elements($subsection)) {
discard_stream_property_element_changes(elem, sub);
}
const $save_btn_controls = $subsection.find(".save-button-controls");
settings_components.change_save_button_state($save_btn_controls, "discarded");
}
export function discard_group_settings_subsection_changes($subsection, group) {
for (const elem of settings_components.get_subsection_property_elements($subsection)) {
discard_group_property_element_changes(elem, group);
}
const $save_btn_controls = $subsection.find(".save-button-controls");
settings_components.change_save_button_state($save_btn_controls, "discarded");
}
function discard_realm_default_settings_subsection_changes($subsection) {
for (const elem of settings_components.get_subsection_property_elements($subsection)) {
discard_realm_default_property_element_changes(elem);
}
const $save_btn_controls = $subsection.find(".save-button-controls");
settings_components.change_save_button_state($save_btn_controls, "discarded");
}
export function deactivate_organization(e) { export function deactivate_organization(e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@@ -872,15 +904,11 @@ export function register_save_discard_widget_handlers(
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
const $subsection = $(e.target).closest(".settings-subsection-parent"); const $subsection = $(e.target).closest(".settings-subsection-parent");
for (const elem of settings_components.get_subsection_property_elements($subsection)) {
if (for_realm_default_settings) { if (for_realm_default_settings) {
discard_realm_default_property_element_changes(elem); discard_realm_default_settings_subsection_changes($subsection);
} else { } else {
discard_realm_property_element_changes(elem); discard_realm_settings_subsection_changes($subsection);
} }
}
const $save_btn_controls = $(e.target).closest(".save-button-controls");
settings_components.change_save_button_state($save_btn_controls, "discarded");
}); });
$container.on("click", ".subsection-header .subsection-changes-save button", (e) => { $container.on("click", ".subsection-header .subsection-changes-save button", (e) => {

View File

@@ -756,12 +756,8 @@ export function initialize() {
const sub = sub_store.get(stream_id); const sub = sub_store.get(stream_id);
const $subsection = $(e.target).closest(".settings-subsection-parent"); const $subsection = $(e.target).closest(".settings-subsection-parent");
for (const elem of settings_components.get_subsection_property_elements($subsection)) { settings_org.discard_stream_settings_subsection_changes($subsection, sub);
settings_org.discard_stream_property_element_changes(elem, sub);
}
stream_ui_updates.update_default_stream_and_stream_privacy_state($subsection); stream_ui_updates.update_default_stream_and_stream_privacy_state($subsection);
const $save_btn_controls = $(e.target).closest(".save-button-controls");
settings_components.change_save_button_state($save_btn_controls, "discarded");
}, },
); );
} }

View File

@@ -916,11 +916,7 @@ export function initialize() {
const group = user_groups.get_user_group_from_id(group_id); const group = user_groups.get_user_group_from_id(group_id);
const $subsection = $(e.target).closest(".settings-subsection-parent"); const $subsection = $(e.target).closest(".settings-subsection-parent");
for (const elem of settings_components.get_subsection_property_elements($subsection)) { settings_org.discard_group_settings_subsection_changes($subsection, group);
settings_org.discard_group_property_element_changes(elem, group);
}
const $save_btn_controls = $(e.target).closest(".save-button-controls");
settings_components.change_save_button_state($save_btn_controls, "discarded");
}, },
); );
} }

View File

@@ -444,15 +444,20 @@ function test_discard_changes_button(discard_changes) {
); );
const $discard_button_parent = $(".settings-subsection-parent"); const $discard_button_parent = $(".settings-subsection-parent");
$discard_button_parent.find = () => [ $discard_button_parent.set_find_results(".prop-element", [
$allow_edit_history, $allow_edit_history,
$msg_edit_limit_setting, $msg_edit_limit_setting,
$msg_delete_limit_setting, $msg_delete_limit_setting,
$edit_topic_policy, $edit_topic_policy,
]; ]);
const {$discard_button, props} = createSaveButtons("msg-editing"); const {$discard_button, $save_button_controls, props} = createSaveButtons("msg-editing");
$discard_button.closest = (selector) => $(selector); $discard_button.closest = (selector) => {
assert.equal(selector, ".settings-subsection-parent");
return $discard_button_parent;
};
$discard_button_parent.set_find_results(".save-button-controls", $save_button_controls);
discard_changes(ev); discard_changes(ev);