From 02e82ef10c3b64ea2fddd55f9476c196cb63c2f9 Mon Sep 17 00:00:00 2001 From: Yashashvi Dave Date: Fri, 14 Jun 2019 22:59:48 +0530 Subject: [PATCH] static/js/stream_events: Deduplicate notification settings updates. --- frontend_tests/node_tests/stream_events.js | 10 +++--- static/js/stream_events.js | 42 ++++------------------ 2 files changed, 11 insertions(+), 41 deletions(-) diff --git a/frontend_tests/node_tests/stream_events.js b/frontend_tests/node_tests/stream_events.js index 4c961aa67c..23ed4b1140 100644 --- a/frontend_tests/node_tests/stream_events.js +++ b/frontend_tests/node_tests/stream_events.js @@ -73,25 +73,25 @@ run_test('update_property', () => { // Test desktop notifications stream_events.update_property(1, '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); // Tests audible notifications stream_events.update_property(1, '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); // Tests push notifications stream_events.update_property(1, '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); // Tests email notifications stream_events.update_property(1, '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); // Test name change @@ -124,7 +124,7 @@ run_test('update_property', () => { with_overrides(function (override) { override('stream_list.refresh_pinned_or_unpinned_stream', noop); 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); }); diff --git a/static/js/stream_events.js b/static/js/stream_events.js index d48e157c35..63e0a35467 100644 --- a/static/js/stream_events.js +++ b/static/js/stream_events.js @@ -6,34 +6,10 @@ var exports = {}; // defaults, however, they are only called after a manual override, so // doing so is unnecessary with the current code. Ideally, we'd do a // refactor to address that, however. -function update_stream_desktop_notifications(sub, value) { - var desktop_notifications_checkbox = $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #sub_desktop_notifications_setting .sub_setting_control"); - desktop_notifications_checkbox.prop('checked', value); - sub.desktop_notifications = 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; +function update_stream_setting(sub, value, setting) { + var setting_checkbox = $("#" + setting + "_" + sub.stream_id); + setting_checkbox.prop("checked", value); + sub[setting] = value; } 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); break; case 'desktop_notifications': - update_stream_desktop_notifications(sub, value); - break; case 'audible_notifications': - update_stream_audible_notifications(sub, value); - break; case 'push_notifications': - update_stream_push_notifications(sub, value); - break; case 'email_notifications': - update_stream_email_notifications(sub, value); + update_stream_setting(sub, value, property); break; case 'name': subs.update_stream_name(sub, value); @@ -75,7 +45,7 @@ exports.update_property = function (stream_id, property, value, other_values) { sub.email_address = value; break; case 'pin_to_top': - update_stream_pin(sub, value); + update_stream_setting(sub, value, property); stream_list.refresh_pinned_or_unpinned_stream(sub); break; case 'invite_only':