settings_bots: Extract logic for ability to create bots.

This is a minor refactor/deduplication and renaming of
'admin_only_bot_creation' to 'can_create_new_bots'
This commit is contained in:
Shubham Dhama
2018-06-13 20:29:15 +05:30
committed by Tim Abbott
parent b0bfb6b153
commit 603139500a
4 changed files with 26 additions and 10 deletions

View File

@@ -195,3 +195,15 @@ run_test('test tab clicks', () => {
assert(forms.inactive.visible()); assert(forms.inactive.visible());
}()); }());
}); });
run_test('can_create_new_bots', () => {
page_params.is_admin = true;
assert(settings_bots.can_create_new_bots());
page_params.is_admin = false;
page_params.realm_bot_creation_policy = 1;
assert(settings_bots.can_create_new_bots());
page_params.realm_bot_creation_policy = 3;
assert(!settings_bots.can_create_new_bots());
});

View File

@@ -136,9 +136,7 @@ exports.setup_page = function () {
zuliprc: 'zuliprc', zuliprc: 'zuliprc',
botserverrc: 'botserverrc', botserverrc: 'botserverrc',
timezones: moment.tz.names(), timezones: moment.tz.names(),
admin_only_bot_creation: page_params.is_admin || can_create_new_bots: settings_bots.can_create_new_bots(),
page_params.realm_bot_creation_policy !==
settings_bots.bot_creation_policy_values.admins_only.code,
settings_label: settings.settings_label, settings_label: settings.settings_label,
}); });

View File

@@ -72,8 +72,7 @@ function render_bots() {
user_owns_an_active_bot = user_owns_an_active_bot || elem.is_active; user_owns_an_active_bot = user_owns_an_active_bot || elem.is_active;
}); });
if (page_params.is_admin || page_params.realm_bot_creation_policy !== if (settings_bots.can_create_new_bots()) {
exports.bot_creation_policy_values.admins_only.code) {
if (!user_owns_an_active_bot) { if (!user_owns_an_active_bot) {
focus_tab.add_a_new_bot_tab(); focus_tab.add_a_new_bot_tab();
return; return;
@@ -146,6 +145,15 @@ exports.bot_creation_policy_values = {
}, },
}; };
exports.can_create_new_bots = function () {
if (page_params.is_admin) {
return true;
}
return page_params.realm_bot_creation_policy !==
exports.bot_creation_policy_values.admins_only.code;
};
exports.update_bot_settings_tip = function () { exports.update_bot_settings_tip = function () {
var permission_type = exports.bot_creation_policy_values; var permission_type = exports.bot_creation_policy_values;
var current_permission = page_params.realm_bot_creation_policy; var current_permission = page_params.realm_bot_creation_policy;
@@ -164,9 +172,7 @@ exports.update_bot_permissions_ui = function () {
exports.update_bot_settings_tip(); exports.update_bot_settings_tip();
$('#bot_table_error').hide(); $('#bot_table_error').hide();
$("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy); $("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy);
if (page_params.realm_bot_creation_policy === if (!exports.can_create_new_bots()) {
exports.bot_creation_policy_values.admins_only.code &&
!page_params.is_admin) {
$('#create_bot_form').hide(); $('#create_bot_form').hide();
$('.add-a-new-bot-tab').hide(); $('.add-a-new-bot-tab').hide();
focus_tab.active_bots_tab(); focus_tab.active_bots_tab();

View File

@@ -15,7 +15,7 @@
<ul class="nav nav-tabs nav-justified" id="bots_lists_navbar"> <ul class="nav nav-tabs nav-justified" id="bots_lists_navbar">
<li class="active active-bots-tab"><a>{{t "Active bots" }}</a></li> <li class="active active-bots-tab"><a>{{t "Active bots" }}</a></li>
<li class="inactive-bots-tab"><a>{{t "Inactive bots" }}</a></li> <li class="inactive-bots-tab"><a>{{t "Inactive bots" }}</a></li>
<li class="add-a-new-bot-tab {{#unless admin_only_bot_creation}}hide{{/unless}}"><a>{{t "Add a new bot" }}</a></li> <li class="add-a-new-bot-tab {{#unless can_create_new_bots}}hide{{/unless}}"><a>{{t "Add a new bot" }}</a></li>
</ul> </ul>
<ol class="bots_list required-text" id="active_bots_list" data-empty="{{t 'You have no active bots.' }}"> <ol class="bots_list required-text" id="active_bots_list" data-empty="{{t 'You have no active bots.' }}">
@@ -28,7 +28,7 @@
<div id="add-a-new-bot-form"> <div id="add-a-new-bot-form">
<form id="create_bot_form" <form id="create_bot_form"
class="form-horizontal no-padding {{#unless admin_only_bot_creation}}hide{{/unless}}"> class="form-horizontal no-padding {{#unless can_create_new_bots}}hide{{/unless}}">
<div class="new-bot-form"> <div class="new-bot-form">
<div class="input-group"> <div class="input-group">
<label for="bot_type"> <label for="bot_type">