From 6ef8cdb38018b0c8df06ab9cbb577191820e06fe Mon Sep 17 00:00:00 2001 From: somesh202 <77766761+somesh202@users.noreply.github.com> Date: Thu, 31 Mar 2022 12:39:12 +0530 Subject: [PATCH] org_settings: Add main feature to change bot role. This commit attempts to refactor the `handle_bot_form` by adding new field for `Role` in the `Manage bot` modal. It uses the `/json/users/` url for passing the role of a bot and allow changing it as in case of a normal user. Fixes: #21105 --- static/js/settings_users.js | 19 +++++++++++++++++-- static/templates/settings/admin_bot_form.hbs | 8 ++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/static/js/settings_users.js b/static/js/settings_users.js index 773639b143..b6ddd6aaed 100644 --- a/static/js/settings_users.js +++ b/static/js/settings_users.js @@ -645,16 +645,20 @@ function handle_bot_form($tbody) { user_id, email: bot.email, full_name: bot.full_name, + user_role_values: settings_config.user_role_values, + disable_role_dropdown: bot.is_owner && !page_params.is_owner, }); let owner_widget; function submit_bot_details() { + const role = Number.parseInt($("#bot-role-select").val().trim(), 10); const $full_name = $("#dialog_widget_modal").find("input[name='full_name']"); const url = "/json/bots/" + encodeURIComponent(user_id); const data = { full_name: $full_name.val(), + role: JSON.stringify(role), }; if (owner_widget === undefined) { @@ -668,7 +672,7 @@ function handle_bot_form($tbody) { dialog_widget.submit_api_request(channel.patch, url, data); } - function get_bot_owner_widget() { + function get_bot_owner_widget_and_set_role_values() { const owner_id = bot_data.get(user_id).owner_id; const user_ids = people.get_active_human_ids(); @@ -687,6 +691,17 @@ function handle_bot_form($tbody) { // organizations with 10Ks of users. owner_widget = new DropdownListWidget(opts); owner_widget.setup(); + + $("#bot-role-select").val(bot.role); + if (!page_params.is_owner) { + $("#bot-role-select") + .find( + `option[value="${CSS.escape( + settings_config.user_role_values.owner.code, + )}"]`, + ) + .hide(); + } } dialog_widget.launch({ @@ -694,7 +709,7 @@ function handle_bot_form($tbody) { html_body, id: "edit_bot_modal", on_click: submit_bot_details, - post_render: get_bot_owner_widget, + post_render: get_bot_owner_widget_and_set_role_values, }); }); } diff --git a/static/templates/settings/admin_bot_form.hbs b/static/templates/settings/admin_bot_form.hbs index 2e62556cc7..d015c8285c 100644 --- a/static/templates/settings/admin_bot_form.hbs +++ b/static/templates/settings/admin_bot_form.hbs @@ -13,6 +13,14 @@ +