From 58b612a4f0d8fcafb3bd8d4048f2b37d5f8a6104 Mon Sep 17 00:00:00 2001 From: Rohitt Vashishtha Date: Wed, 20 May 2020 04:02:34 +0530 Subject: [PATCH] settings-users: Rerender bot rows on data change. Previously, we fiddled with the existing HTML to update the state. Now, we can use list_render.render_item() to render the complete item properly. --- frontend_tests/node_tests/dispatch.js | 9 ++++----- static/js/server_events_dispatch.js | 5 +---- static/js/settings_users.js | 19 ++++++++----------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index 1082fc27de..ee6eef4a6e 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -1044,7 +1044,7 @@ with_overrides(function (override) { global.with_stub(function (bot_stub) { global.with_stub(function (admin_stub) { override('bot_data.add', bot_stub.f); - override('settings_users.update_user_data', admin_stub.f); + override('settings_users.update_bot_data', admin_stub.f); dispatch(event); const args = bot_stub.get_args('bot'); assert_same(args.bot, event.bot); @@ -1057,7 +1057,7 @@ with_overrides(function (override) { global.with_stub(function (bot_stub) { global.with_stub(function (admin_stub) { override('bot_data.deactivate', bot_stub.f); - override('settings_users.update_user_data', admin_stub.f); + override('settings_users.update_bot_data', admin_stub.f); dispatch(event); const args = bot_stub.get_args('user_id'); assert_same(args.user_id, event.bot.user_id); @@ -1070,7 +1070,7 @@ with_overrides(function (override) { global.with_stub(function (bot_stub) { global.with_stub(function (admin_stub) { override('bot_data.del', bot_stub.f); - override('settings_users.update_user_data', admin_stub.f); + override('settings_users.update_bot_data', admin_stub.f); dispatch(event); const args = bot_stub.get_args('bot_id'); assert_same(args.bot_id, event.bot.user_id); @@ -1083,7 +1083,7 @@ with_overrides(function (override) { global.with_stub(function (bot_stub) { global.with_stub(function (admin_stub) { override('bot_data.update', bot_stub.f); - override('settings_users.update_user_data', admin_stub.f); + override('settings_users.update_bot_data', admin_stub.f); dispatch(event); @@ -1093,7 +1093,6 @@ with_overrides(function (override) { args = admin_stub.get_args('update_user_id', 'update_bot_data'); assert_same(args.update_user_id, event.bot.user_id); - assert_same(args.update_bot_data, event.bot); }); }); }); diff --git a/static/js/server_events_dispatch.js b/static/js/server_events_dispatch.js index 9effd34365..7890fe375c 100644 --- a/static/js/server_events_dispatch.js +++ b/static/js/server_events_dispatch.js @@ -187,18 +187,15 @@ exports.dispatch_normal_event = function dispatch_normal_event(event) { case 'realm_bot': if (event.op === 'add') { bot_data.add(event.bot); - settings_users.update_user_data(event.bot.user_id, event.bot); } else if (event.op === 'remove') { bot_data.deactivate(event.bot.user_id); event.bot.is_active = false; - settings_users.update_user_data(event.bot.user_id, event.bot); } else if (event.op === 'delete') { bot_data.del(event.bot.user_id); - settings_users.update_user_data(event.bot.user_id, event.bot); } else if (event.op === 'update') { bot_data.update(event.bot.user_id, event.bot); - settings_users.update_user_data(event.bot.user_id, event.bot); } + settings_users.update_bot_data(event.bot.user_id); break; case 'realm_emoji': diff --git a/static/js/settings_users.js b/static/js/settings_users.js index beb65a24fb..0cfeff43ad 100644 --- a/static/js/settings_users.js +++ b/static/js/settings_users.js @@ -236,16 +236,19 @@ function human_info(person) { return info; } +let bot_list_widget; + section.bots.create_table = () => { loading.make_indicator($('#admin_page_bots_loading_indicator'), {text: 'Loading...'}); const $bots_table = $("#admin_bots_table"); $bots_table.hide(); const bot_user_ids = bot_data.all_user_ids(); - list_render.create($bots_table, bot_user_ids, { + bot_list_widget = list_render.create($bots_table, bot_user_ids, { name: "admin_bot_list", get_item: bot_info, modifier: render_admin_user_list, + html_selector: (item) => `tr[data-user-id='${item}']`, filter: { element: $bots_table.closest(".settings-section").find(".search"), predicate: function (item, value) { @@ -320,6 +323,10 @@ section.deactivated.create_table = (deactivated_users) => { $("#admin_deactivated_users_table").show(); }; +exports.update_bot_data = function (bot_user_id) { + bot_list_widget.render_item(bot_user_id); +}; + exports.update_user_data = function (user_id, new_data) { const user_row = get_user_info_row(user_id); @@ -332,16 +339,6 @@ exports.update_user_data = function (user_id, new_data) { user_row.find(".user_name").text(new_data.full_name); } - if (new_data.owner_id !== undefined) { - // TODO: Linkify the owner name to match the - // formatting of the list. Ideally we can - // make this whole function simpler - // by re-rendering the entire row via - // the list widget. - const owner_name = bot_owner_full_name(new_data.owner_id); - user_row.find(".owner").text(owner_name); - } - if (new_data.is_active !== undefined) { if (new_data.is_active === false) { // Deactivate the user/bot in the table