static/js/stream_events: Deduplicate notification settings updates.

This commit is contained in:
Yashashvi Dave 2019-06-14 22:59:48 +05:30 committed by Tim Abbott
parent 5c88475378
commit 02e82ef10c
2 changed files with 11 additions and 41 deletions

View File

@ -73,25 +73,25 @@ run_test('update_property', () => {
// Test desktop notifications // Test desktop notifications
stream_events.update_property(1, 'desktop_notifications', true); stream_events.update_property(1, 'desktop_notifications', true);
assert.equal(frontend.desktop_notifications, true); assert.equal(frontend.desktop_notifications, true);
var checkbox = $(".subscription_settings[data-stream-id='1'] #sub_desktop_notifications_setting .sub_setting_control"); var checkbox = $("#desktop_notifications_1");
assert.equal(checkbox.prop('checked'), true); assert.equal(checkbox.prop('checked'), true);
// Tests audible notifications // Tests audible notifications
stream_events.update_property(1, 'audible_notifications', true); stream_events.update_property(1, 'audible_notifications', true);
assert.equal(frontend.audible_notifications, true); assert.equal(frontend.audible_notifications, true);
checkbox = $(".subscription_settings[data-stream-id='1'] #sub_audible_notifications_setting .sub_setting_control"); checkbox = $("#audible_notifications_1");
assert.equal(checkbox.prop('checked'), true); assert.equal(checkbox.prop('checked'), true);
// Tests push notifications // Tests push notifications
stream_events.update_property(1, 'push_notifications', true); stream_events.update_property(1, 'push_notifications', true);
assert.equal(frontend.push_notifications, true); assert.equal(frontend.push_notifications, true);
checkbox = $(".subscription_settings[data-stream-id='1'] #sub_push_notifications_setting .sub_setting_control"); checkbox = $("#push_notifications_1");
assert.equal(checkbox.prop('checked'), true); assert.equal(checkbox.prop('checked'), true);
// Tests email notifications // Tests email notifications
stream_events.update_property(1, 'email_notifications', true); stream_events.update_property(1, 'email_notifications', true);
assert.equal(frontend.email_notifications, true); assert.equal(frontend.email_notifications, true);
checkbox = $(".subscription_settings[data-stream-id='1'] #sub_email_notifications_setting .sub_setting_control"); checkbox = $("#email_notifications_1");
assert.equal(checkbox.prop('checked'), true); assert.equal(checkbox.prop('checked'), true);
// Test name change // Test name change
@ -124,7 +124,7 @@ run_test('update_property', () => {
with_overrides(function (override) { with_overrides(function (override) {
override('stream_list.refresh_pinned_or_unpinned_stream', noop); override('stream_list.refresh_pinned_or_unpinned_stream', noop);
stream_events.update_property(1, 'pin_to_top', true); stream_events.update_property(1, 'pin_to_top', true);
checkbox = $(".subscription_settings[data-stream-id='1'] #sub_pin_to_top_setting .sub_setting_control"); checkbox = $("#pin_to_top_1");
assert.equal(checkbox.prop('checked'), true); assert.equal(checkbox.prop('checked'), true);
}); });

View File

@ -6,34 +6,10 @@ var exports = {};
// defaults, however, they are only called after a manual override, so // defaults, however, they are only called after a manual override, so
// doing so is unnecessary with the current code. Ideally, we'd do a // doing so is unnecessary with the current code. Ideally, we'd do a
// refactor to address that, however. // refactor to address that, however.
function update_stream_desktop_notifications(sub, value) { function update_stream_setting(sub, value, setting) {
var desktop_notifications_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_desktop_notifications_setting .sub_setting_control"); var setting_checkbox = $("#" + setting + "_" + sub.stream_id);
desktop_notifications_checkbox.prop('checked', value); setting_checkbox.prop("checked", value);
sub.desktop_notifications = value; sub[setting] = value;
}
function update_stream_audible_notifications(sub, value) {
var audible_notifications_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_audible_notifications_setting .sub_setting_control");
audible_notifications_checkbox.prop('checked', value);
sub.audible_notifications = value;
}
function update_stream_push_notifications(sub, value) {
var push_notifications_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_push_notifications_setting .sub_setting_control");
push_notifications_checkbox.prop('checked', value);
sub.push_notifications = value;
}
function update_stream_email_notifications(sub, value) {
var email_notifications_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_email_notifications_setting .sub_setting_control");
email_notifications_checkbox.prop('checked', value);
sub.email_notifications = value;
}
function update_stream_pin(sub, value) {
var pin_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_pin_to_top_setting .sub_setting_control");
pin_checkbox.prop('checked', value);
sub.pin_to_top = value;
} }
exports.update_property = function (stream_id, property, value, other_values) { exports.update_property = function (stream_id, property, value, other_values) {
@ -54,16 +30,10 @@ exports.update_property = function (stream_id, property, value, other_values) {
stream_muting.update_is_muted(sub, !value); stream_muting.update_is_muted(sub, !value);
break; break;
case 'desktop_notifications': case 'desktop_notifications':
update_stream_desktop_notifications(sub, value);
break;
case 'audible_notifications': case 'audible_notifications':
update_stream_audible_notifications(sub, value);
break;
case 'push_notifications': case 'push_notifications':
update_stream_push_notifications(sub, value);
break;
case 'email_notifications': case 'email_notifications':
update_stream_email_notifications(sub, value); update_stream_setting(sub, value, property);
break; break;
case 'name': case 'name':
subs.update_stream_name(sub, value); subs.update_stream_name(sub, value);
@ -75,7 +45,7 @@ exports.update_property = function (stream_id, property, value, other_values) {
sub.email_address = value; sub.email_address = value;
break; break;
case 'pin_to_top': case 'pin_to_top':
update_stream_pin(sub, value); update_stream_setting(sub, value, property);
stream_list.refresh_pinned_or_unpinned_stream(sub); stream_list.refresh_pinned_or_unpinned_stream(sub);
break; break;
case 'invite_only': case 'invite_only':