users: Extract code for showing deactivation modal in new file.

This commit creates a new file user_deactivation_ui.js which
now contains the code for showing deactivation modal for user
and bots.

Since confirm_deactivation and confirm_bot_deactivation functions
are used in multiple places, this change helps us in avoiding
import cycles.
This commit is contained in:
Sahil Batra 2023-09-25 22:49:40 +05:30 committed by Tim Abbott
parent 8852c4b7af
commit f3c17b8d4f
5 changed files with 95 additions and 84 deletions

View File

@ -225,6 +225,7 @@ EXEMPT_FILES = make_set(
"web/src/upload_widget.ts",
"web/src/url-template.d.ts",
"web/src/user_card_popover.js",
"web/src/user_deactivation_ui.js",
"web/src/user_group_create.js",
"web/src/user_group_create_members.js",
"web/src/user_group_create_members_data.ts",

View File

@ -1,7 +1,6 @@
import ClipboardJS from "clipboard";
import $ from "jquery";
import render_settings_deactivation_bot_modal from "../templates/confirm_dialog/confirm_deactivate_bot.hbs";
import render_add_new_bot_form from "../templates/settings/add_new_bot_form.hbs";
import render_bot_avatar_row from "../templates/settings/bot_avatar_row.hbs";
import render_bot_settings_tip from "../templates/settings/bot_settings_tip.hbs";
@ -21,6 +20,7 @@ import * as settings_data from "./settings_data";
import * as settings_users from "./settings_users";
import * as stream_data from "./stream_data";
import * as ui_report from "./ui_report";
import * as user_deactivation_ui from "./user_deactivation_ui";
import * as user_profile from "./user_profile";
const INCOMING_WEBHOOK_BOT_TYPE = 2;
@ -320,20 +320,6 @@ export function add_a_new_bot() {
});
}
export function confirm_bot_deactivation(bot_id, handle_confirm, loading_spinner) {
const bot = people.get_by_user_id(bot_id);
const html_body = render_settings_deactivation_bot_modal();
dialog_widget.launch({
html_heading: $t_html({defaultMessage: "Deactivate {name}?"}, {name: bot.full_name}),
help_link: "/help/deactivate-or-reactivate-a-bot",
html_body,
html_submit_button: $t_html({defaultMessage: "Deactivate"}),
on_click: handle_confirm,
loading_spinner,
});
}
export function show_generate_integration_url_modal(api_key) {
const default_url_message = $t_html({defaultMessage: "Integration URL will appear here."});
const streams = stream_data.subscribed_subs();
@ -514,7 +500,7 @@ export function set_up() {
};
dialog_widget.submit_api_request(channel.del, url, {}, opts);
}
confirm_bot_deactivation(bot_id, handle_confirm, true);
user_deactivation_ui.confirm_bot_deactivation(bot_id, handle_confirm, true);
});
$("#inactive_bots_list").on("click", "button.reactivate_bot", (e) => {

View File

@ -1,12 +1,10 @@
import $ from "jquery";
import render_settings_deactivation_user_modal from "../templates/confirm_dialog/confirm_deactivate_user.hbs";
import render_settings_reactivation_bot_modal from "../templates/confirm_dialog/confirm_reactivate_bot.hbs";
import render_settings_reactivation_user_modal from "../templates/confirm_dialog/confirm_reactivate_user.hbs";
import render_admin_user_list from "../templates/settings/admin_user_list.hbs";
import * as blueslip from "./blueslip";
import * as bot_data from "./bot_data";
import * as browser_history from "./browser_history";
import * as channel from "./channel";
import * as confirm_dialog from "./confirm_dialog";
@ -23,6 +21,7 @@ import * as settings_bots from "./settings_bots";
import * as settings_data from "./settings_data";
import * as settings_panel_menu from "./settings_panel_menu";
import * as timerender from "./timerender";
import * as user_deactivation_ui from "./user_deactivation_ui";
import * as user_profile from "./user_profile";
const section = {
@ -444,67 +443,6 @@ function start_data_load() {
populate_users();
}
export function confirm_deactivation(user_id, handle_confirm, loading_spinner) {
// Knowing the number of invites requires making this request. If the request fails,
// we won't have the accurate number of invites. So, we don't show the modal if the
// request fails.
channel.get({
url: "/json/invites",
timeout: 10 * 1000,
success(data) {
let number_of_invites_by_user = 0;
for (const invite of data.invites) {
if (invite.invited_by_user_id === user_id) {
number_of_invites_by_user = number_of_invites_by_user + 1;
}
}
const bots_owned_by_user = bot_data.get_all_bots_owned_by_user(user_id);
const user = people.get_by_user_id(user_id);
const realm_url = page_params.realm_uri;
const realm_name = page_params.realm_name;
const opts = {
username: user.full_name,
email: user.delivery_email,
bots_owned_by_user,
number_of_invites_by_user,
admin_email: people.my_current_email(),
realm_url,
realm_name,
};
const html_body = render_settings_deactivation_user_modal(opts);
function set_email_field_visibility() {
const $send_email_checkbox = $("#dialog_widget_modal").find(".send_email");
const $email_field = $("#dialog_widget_modal").find(".email_field");
$email_field.hide();
$send_email_checkbox.on("change", () => {
if ($send_email_checkbox.is(":checked")) {
$email_field.show();
} else {
$email_field.hide();
}
});
}
dialog_widget.launch({
html_heading: $t_html(
{defaultMessage: "Deactivate {name}?"},
{name: user.full_name},
),
help_link: "/help/deactivate-or-reactivate-a-user#deactivating-a-user",
html_body,
html_submit_button: $t_html({defaultMessage: "Deactivate"}),
id: "deactivate-user-modal",
on_click: handle_confirm,
post_render: set_email_field_visibility,
loading_spinner,
});
},
});
}
function handle_deactivation($tbody) {
$tbody.on("click", ".deactivate", (e) => {
// This click event must not get propagated to parent container otherwise the modal
@ -528,7 +466,7 @@ function handle_deactivation($tbody) {
dialog_widget.submit_api_request(channel.del, url, data);
}
confirm_deactivation(user_id, handle_confirm, true);
user_deactivation_ui.confirm_deactivation(user_id, handle_confirm, true);
});
}
@ -547,7 +485,7 @@ function handle_bot_deactivation($tbody) {
dialog_widget.submit_api_request(channel.del, url);
}
settings_bots.confirm_bot_deactivation(bot_id, handle_confirm, true);
user_deactivation_ui.confirm_bot_deactivation(bot_id, handle_confirm, true);
});
}

View File

@ -0,0 +1,86 @@
import $ from "jquery";
import render_settings_deactivation_bot_modal from "../templates/confirm_dialog/confirm_deactivate_bot.hbs";
import render_settings_deactivation_user_modal from "../templates/confirm_dialog/confirm_deactivate_user.hbs";
import * as bot_data from "./bot_data";
import * as channel from "./channel";
import * as dialog_widget from "./dialog_widget";
import {$t_html} from "./i18n";
import {page_params} from "./page_params";
import * as people from "./people";
export function confirm_deactivation(user_id, handle_confirm, loading_spinner) {
// Knowing the number of invites requires making this request. If the request fails,
// we won't have the accurate number of invites. So, we don't show the modal if the
// request fails.
channel.get({
url: "/json/invites",
timeout: 10 * 1000,
success(data) {
let number_of_invites_by_user = 0;
for (const invite of data.invites) {
if (invite.invited_by_user_id === user_id) {
number_of_invites_by_user = number_of_invites_by_user + 1;
}
}
const bots_owned_by_user = bot_data.get_all_bots_owned_by_user(user_id);
const user = people.get_by_user_id(user_id);
const realm_url = page_params.realm_uri;
const realm_name = page_params.realm_name;
const opts = {
username: user.full_name,
email: user.delivery_email,
bots_owned_by_user,
number_of_invites_by_user,
admin_email: people.my_current_email(),
realm_url,
realm_name,
};
const html_body = render_settings_deactivation_user_modal(opts);
function set_email_field_visibility() {
const $send_email_checkbox = $("#dialog_widget_modal").find(".send_email");
const $email_field = $("#dialog_widget_modal").find(".email_field");
$email_field.hide();
$send_email_checkbox.on("change", () => {
if ($send_email_checkbox.is(":checked")) {
$email_field.show();
} else {
$email_field.hide();
}
});
}
dialog_widget.launch({
html_heading: $t_html(
{defaultMessage: "Deactivate {name}?"},
{name: user.full_name},
),
help_link: "/help/deactivate-or-reactivate-a-user#deactivating-a-user",
html_body,
html_submit_button: $t_html({defaultMessage: "Deactivate"}),
id: "deactivate-user-modal",
on_click: handle_confirm,
post_render: set_email_field_visibility,
loading_spinner,
});
},
});
}
export function confirm_bot_deactivation(bot_id, handle_confirm, loading_spinner) {
const bot = people.get_by_user_id(bot_id);
const html_body = render_settings_deactivation_bot_modal();
dialog_widget.launch({
html_heading: $t_html({defaultMessage: "Deactivate {name}?"}, {name: bot.full_name}),
help_link: "/help/deactivate-or-reactivate-a-bot",
html_body,
html_submit_button: $t_html({defaultMessage: "Deactivate"}),
on_click: handle_confirm,
loading_spinner,
});
}

View File

@ -26,7 +26,6 @@ import * as overlays from "./overlays";
import {page_params} from "./page_params";
import * as people from "./people";
import * as popovers from "./popovers";
import * as settings_bots from "./settings_bots";
import * as settings_config from "./settings_config";
import * as settings_data from "./settings_data";
import * as settings_profile_fields from "./settings_profile_fields";
@ -37,6 +36,7 @@ import * as sub_store from "./sub_store";
import * as subscriber_api from "./subscriber_api";
import * as timerender from "./timerender";
import * as ui_report from "./ui_report";
import * as user_deactivation_ui from "./user_deactivation_ui";
import * as user_groups from "./user_groups";
import * as user_pill from "./user_pill";
import * as util from "./util";
@ -600,7 +600,7 @@ export function show_edit_bot_info_modal(user_id, $container) {
const url = "/json/bots/" + encodeURIComponent(bot_id);
dialog_widget.submit_api_request(channel.del, url);
}
settings_bots.confirm_bot_deactivation(bot_id, handle_confirm, true);
user_deactivation_ui.confirm_bot_deactivation(bot_id, handle_confirm, true);
});
$("#bot-edit-form").on("click", ".generate_url_for_integration", (e) => {
@ -698,7 +698,7 @@ export function show_edit_user_info_modal(user_id, $container) {
const url = "/json/users/" + encodeURIComponent(user_id);
dialog_widget.submit_api_request(channel.del, url);
}
settings_users.confirm_deactivation(user_id, handle_confirm, true);
user_deactivation_ui.confirm_deactivation(user_id, handle_confirm, true);
});
$("#user-profile-modal").on("click", ".dialog_submit_button", () => {