Skip to content

Commit

Permalink
org settings: De-duplicate the JS code for notifications stream handl…
Browse files Browse the repository at this point in the history
…ers.

(This is another preliminary commit in the direction of having
"save/discard" widget show up rather than saving immediately.)

The code for selecting and processing the stream for both types of
notifications is almost the same, so de-duplicated.
  • Loading branch information
pragatiagrawal31 authored and timabbott committed Jan 15, 2020
1 parent 9a6b3c1 commit a1426d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 46 deletions.
56 changes: 12 additions & 44 deletions static/js/settings_org.js
Original file line number Diff line number Diff line change
Expand Up @@ -1068,62 +1068,30 @@ exports.build_page = function () {
});
});

function notification_stream_update(data) {
_.each(get_subsection_property_elements($("#org-discard-notifications")),
discard_property_element_changes);
const nearest_save_button = $("#org-submit-notifications");
exports.save_organization_settings(data, nearest_save_button);
}

function update_notifications_stream(new_notifications_stream_id) {
exports.render_notifications_stream_ui(new_notifications_stream_id,
$('#realm_notifications_stream_name'));
notification_stream_update({
notifications_stream_id: JSON.stringify(parseInt(new_notifications_stream_id, 10)),
});
}

$("#id_realm_notifications_stream .dropdown-list-body").on("click keypress", ".stream_name", function (e) {
if (e.type === "keypress") {
if (e.which === 13) {
$("#id_realm_notifications_stream .dropdown-menu").dropdown("toggle");
} else {
return;
}
}

const stream_id = parseInt($(this).attr('data-stream-id'), 10);
update_notifications_stream(stream_id);
});
function notification_stream_update(stream_id, notification_type) {
exports.render_notifications_stream_ui(stream_id,
$(`#realm_${notification_type}_stream_name`));
exports.save_organization_settings({
[`${notification_type}_stream_id`]: JSON.stringify(parseInt(stream_id, 10)),
}, $("#org-submit-notifications"));

$("#notifications_stream_disable").click(function () {
update_notifications_stream(-1);
});

function update_signup_notifications_stream(new_signup_notifications_stream_id) {
exports.render_notifications_stream_ui(new_signup_notifications_stream_id,
$('#realm_signup_notifications_stream_name'));
notification_stream_update({
signup_notifications_stream_id: JSON.stringify(
parseInt(new_signup_notifications_stream_id, 10)),
});
}

$("#id_realm_signup_notifications_stream .dropdown-list-body").on("click keypress", ".stream_name", function (e) {
$(".notifications-stream-setting .dropdown-list-body").on("click keypress", ".stream_name", function (e) {
const notifications_stream_setting_elem = $(this).closest(".notifications-stream-setting");
if (e.type === "keypress") {
if (e.which === 13) {
$("#id_realm_signup_notifications_stream .dropdown-menu").dropdown("toggle");
notifications_stream_setting_elem.find(".dropdown-menu").dropdown("toggle");
} else {
return;
}
}

const stream_id = parseInt($(this).attr('data-stream-id'), 10);
update_signup_notifications_stream(stream_id);
notification_stream_update(stream_id, notifications_stream_setting_elem.data("notifications-type"));
});

$("#signup_notifications_stream_disable").click(function () {
update_signup_notifications_stream(-1);
$(".notification-disable").click(function (e) {
notification_stream_update(-1, e.target.id.replace("_stream_disable", ""));
});

function upload_realm_icon(file_input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<div class="input-group">
<label for="realm_{{ notifications_type }}_stream" id="realm_{{ notifications_type }}_stream_label" class="inline-block">
{{ label }}
<span class="dropup actual-dropdown-menu" id="id_realm_{{ notifications_type }}_stream"
name="realm_{{ notifications_type }}_stream" aria-labelledby="realm_{{ notifications_type }}_stream_label">
<span class="notifications-stream-setting dropup actual-dropdown-menu" id="id_realm_{{ notifications_type }}_stream"
name="realm_{{ notifications_type }}_stream" aria-labelledby="realm_{{ notifications_type }}_stream_label"
data-notifications-type="{{ notifications_type }}">
<button class="button small rounded dropdown-toggle" data-toggle="dropdown">
<span id="realm_{{ notifications_type }}_stream_name"></span>
<i class="fa fa-pencil"></i>
Expand Down

0 comments on commit a1426d7

Please sign in to comment.