From 1091615b17a8e101bfcd40f0d382110bcb302ee2 Mon Sep 17 00:00:00 2001 From: Rohitt Vashishtha Date: Wed, 20 May 2020 01:22:27 +0530 Subject: [PATCH] edit-bot: Show dropdown list widget for selecting owner. This commit removes user_dropdown in favor of the common dropdown_list_widget for our bot edit forms. --- static/js/settings_bots.js | 13 +++++++----- static/js/settings_users.js | 21 +++++++++--------- static/js/user_dropdown.js | 33 ----------------------------- static/templates/admin_bot_form.hbs | 5 +++++ static/templates/edit_bot.hbs | 7 ++++-- static/templates/user_dropdown.hbs | 5 ----- tools/test-js-with-node | 1 - 7 files changed, 29 insertions(+), 56 deletions(-) delete mode 100644 static/js/user_dropdown.js delete mode 100644 static/templates/user_dropdown.hbs diff --git a/static/js/settings_bots.js b/static/js/settings_bots.js index d224dbccdc..ea71dcd90b 100644 --- a/static/js/settings_bots.js +++ b/static/js/settings_bots.js @@ -1,4 +1,3 @@ -const user_dropdown = require("./user_dropdown"); const render_bot_avatar_row = require('../templates/bot_avatar_row.hbs'); const render_edit_bot = require('../templates/edit_bot.hbs'); const render_settings_edit_embedded_bot_service = require("../templates/settings/edit_embedded_bot_service.hbs"); @@ -404,9 +403,13 @@ exports.set_up = function () { const image = li.find(".image"); const errors = form.find('.bot_edit_errors'); - const owner_dropdown = user_dropdown.create(bot.owner_id); - const owner_select_div = $('#edit_bot_modal .select-form'); - owner_select_div.append(owner_dropdown.elem); + const opts = { + widget_name: 'bot_owner', + data: users_list.map(u => ({name: u.full_name, value: u.user_id.toString()})), + default_text: i18n.t("No owner"), + value: bot.owner_id, + }; + const owner_widget = dropdown_list_widget(opts); const service = bot_data.get_services(bot_id)[0]; if (bot.bot_type.toString() === OUTGOING_WEBHOOK_BOT_TYPE) { @@ -433,7 +436,7 @@ exports.set_up = function () { const type = form.attr('data-type'); const full_name = form.find('.edit_bot_name').val(); - const bot_owner_id = owner_dropdown.get_user_id(); + const bot_owner_id = owner_widget.value(); const file_input = $(".edit_bot_form").find('.edit_bot_avatar_file_input'); const spinner = form.find('.edit_bot_spinner'); const edit_button = form.find('.edit_bot_button'); diff --git a/static/js/settings_users.js b/static/js/settings_users.js index a8a6b135ff..beb65a24fb 100644 --- a/static/js/settings_users.js +++ b/static/js/settings_users.js @@ -1,5 +1,4 @@ const settings_data = require("./settings_data"); -const user_dropdown = require("./user_dropdown"); const render_admin_user_list = require("../templates/admin_user_list.hbs"); const render_admin_human_form = require('../templates/admin_human_form.hbs'); const render_admin_bot_form = require('../templates/admin_bot_form.hbs'); @@ -453,15 +452,19 @@ function open_bot_form(person) { // NOTE: building `owner_dropdown` is quite expensive! const owner_id = bot_data.get(person.user_id).owner_id; - const owner_dropdown = user_dropdown.create(owner_id); - modal_container.find( - ".edit_bot_owner_container" - ).append(owner_dropdown.elem); + const users_list = people.get_active_humans(); + const opts = { + widget_name: 'edit_bot_owner', + data: users_list.map(u => ({name: u.full_name, value: u.user_id.toString()})), + default_text: i18n.t("No owner"), + value: owner_id, + }; + const owner_widget = dropdown_list_widget(opts); return { modal: div, - owner_dropdown: owner_dropdown, + owner_widget, }; } @@ -614,9 +617,7 @@ function handle_bot_form(tbody, status_field) { return; } - const ret = open_bot_form(bot); - const modal = ret.modal; - const owner_dropdown = ret.owner_dropdown; + const {modal, owner_widget} = open_bot_form(bot); modal.find('.submit_bot_change').on("click", function (e) { e.preventDefault(); @@ -629,7 +630,7 @@ function handle_bot_form(tbody, status_field) { full_name: full_name.val(), }; - const human_user_id = owner_dropdown.get_user_id(); + const human_user_id = owner_widget.value(); if (human_user_id) { data.bot_owner_id = human_user_id; } diff --git a/static/js/user_dropdown.js b/static/js/user_dropdown.js deleted file mode 100644 index e82ab4de9a..0000000000 --- a/static/js/user_dropdown.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - This module lets you create a dropdown list of all - active humans. - - Right now the only use case is to select the owner - of a bot, but we can generalize this code going forward - for other use cases. Right now it should be quick to - audit the code to find places where we specifically - hard-code stuff for the bot owner case. See - 'bot_owner_select' as an example. -*/ - -const render_user_dropdown = require("../templates/user_dropdown.hbs"); - -exports.create = (current_user_id) => { - const users = people.get_active_humans(); - const info = { - users: users, - name: 'bot_owner_select', // used for label - }; - - const html = render_user_dropdown(info); - const elem = $(html); - - if (current_user_id) { - elem.val(current_user_id); - } - - return { - elem: elem, - get_user_id: () => elem.val(), - }; -}; diff --git a/static/templates/admin_bot_form.hbs b/static/templates/admin_bot_form.hbs index c3fdd212a4..5cb7059c4c 100644 --- a/static/templates/admin_bot_form.hbs +++ b/static/templates/admin_bot_form.hbs @@ -17,6 +17,11 @@
+ {{> settings/dropdown_list_widget + widget_name="edit_bot_owner" + list_placeholder=(t 'Filter users') + reset_button_text=(t '[Remove owner]') + label="" }}
diff --git a/static/templates/edit_bot.hbs b/static/templates/edit_bot.hbs index 0a6acc9976..825c1e20dd 100644 --- a/static/templates/edit_bot.hbs +++ b/static/templates/edit_bot.hbs @@ -18,8 +18,11 @@
-
-
+ {{> settings/dropdown_list_widget + widget_name="bot_owner" + list_placeholder=(t 'Filter users') + reset_button_text=(t '[Remove owner]') + label="" }}
diff --git a/static/templates/user_dropdown.hbs b/static/templates/user_dropdown.hbs deleted file mode 100644 index f1c7f6c98d..0000000000 --- a/static/templates/user_dropdown.hbs +++ /dev/null @@ -1,5 +0,0 @@ - diff --git a/tools/test-js-with-node b/tools/test-js-with-node index fe304469d6..7ff9800a00 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -154,7 +154,6 @@ EXEMPT_FILES = { 'static/js/unread_ops.js', 'static/js/unread_ui.js', 'static/js/upload_widget.js', - 'static/js/user_dropdown.js', 'static/js/user_status_ui.js', 'static/js/zcommand.js', 'static/js/zform.js',