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.
This commit is contained in:
Rohitt Vashishtha 2020-05-20 04:02:34 +05:30 committed by Steve Howell
parent a114b6a1b1
commit 58b612a4f0
3 changed files with 13 additions and 20 deletions

View File

@ -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);
});
});
});

View File

@ -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':

View File

@ -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