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
This commit is contained in:
somesh202 2022-03-31 12:39:12 +05:30 committed by Tim Abbott
parent 0b1f8c05e3
commit 6ef8cdb380
2 changed files with 25 additions and 2 deletions

View File

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

View File

@ -13,6 +13,14 @@
<label for="user_id">{{t "User ID" }}</label>
<input type="text" autocomplete="off" name="user_id" value="{{ user_id }}" readonly/>
</div>
<div class="input-group">
<label class="input-label" for="bot-role-select">{{t 'Role' }}
{{> ../help_link_widget link="/help/roles-and-permissions" }}
</label>
<select name="bot-role-select" id="bot-role-select" data-setting-widget-type="number" {{#if disable_role_dropdown}}disabled{{/if}}>
{{> dropdown_options_widget option_values=user_role_values}}
</select>
</div>
<div class="input-group edit_bot_owner_container">
<label for="bot_owner_select">{{t "Owner" }}</label>
{{> dropdown_list_widget