diff --git a/frontend_tests/puppeteer_tests/settings.ts b/frontend_tests/puppeteer_tests/settings.ts index 35b451400a..22c9c6f1fa 100644 --- a/frontend_tests/puppeteer_tests/settings.ts +++ b/frontend_tests/puppeteer_tests/settings.ts @@ -69,7 +69,7 @@ async function test_get_api_key(page: Page): Promise { const get_api_key_button_selector = "#get_api_key_button"; await page.waitForSelector(get_api_key_button_selector, {visible: true}); - await page.waitForFunction(() => $(":focus").attr("id") === "api_key_modal"); + await common.wait_for_micromodal_to_open(page); await common.fill_form(page, "#api_key_form", { password: test_credentials.default_user.password, }); @@ -89,8 +89,8 @@ async function test_get_api_key(page: Page): Promise { await page.click(download_zuliprc_selector); const zuliprc_decoded_url = await get_decoded_url_in_selector(page, download_zuliprc_selector); assert.match(zuliprc_decoded_url, zuliprc_regex, "Incorrect zuliprc file"); - await page.click("#api_key_modal .close"); - await common.wait_for_modal_to_close(page); + await page.click("#api_key_modal .modal__close"); + await common.wait_for_micromodal_to_close(page); } async function test_webhook_bot_creation(page: Page): Promise { diff --git a/static/js/settings_account.js b/static/js/settings_account.js index 8aa701880e..207c5c47d2 100644 --- a/static/js/settings_account.js +++ b/static/js/settings_account.js @@ -1,5 +1,4 @@ import $ from "jquery"; -import _ from "lodash"; import render_confirm_deactivate_own_user from "../templates/confirm_dialog/confirm_deactivate_own_user.hbs"; import render_dialog_change_password from "../templates/dialog_change_password.hbs"; @@ -289,7 +288,7 @@ export function set_up() { add_custom_profile_fields_to_settings(); $("#account-settings-status").hide(); - const setup_api_key_modal = _.once(() => { + const setup_api_key_modal = () => { function request_api_key(data) { channel.post({ url: "/json/fetch_api_key", @@ -302,7 +301,9 @@ export function set_up() { // remove it. $("#api_key_status").remove(); $("#password_confirmation").hide(); + $("#get_api_key_button").hide(); $("#show_api_key").show(); + $("#api_key_buttons").show(); }, error(xhr) { ui_report.error( @@ -311,35 +312,45 @@ export function set_up() { $("#api_key_status").expectOne(), ); $("#show_api_key").hide(); - $("#api_key_modal").show(); }, }); } - $(".account-settings-form").append(render_settings_api_key_modal()); $("#api_key_value").text(""); $("#show_api_key").hide(); + $("#api_key_buttons").hide(); common.setup_password_visibility_toggle( "#get_api_key_password", "#get_api_key_password + .password_visibility_toggle", {tippy_tooltips: true}, ); + function do_get_api_key() { + $("#api_key_status").hide(); + const data = {}; + data.password = $("#get_api_key_password").val(); + request_api_key(data); + } + if (page_params.realm_password_auth_enabled === false) { // Skip the password prompt step, since the user doesn't have one. request_api_key({}); } else { $("#get_api_key_button").on("click", (e) => { - const data = {}; e.preventDefault(); e.stopPropagation(); - - data.password = $("#get_api_key_password").val(); - request_api_key(data); + do_get_api_key(); + }); + $("#get_api_key_password").on("keydown", (e) => { + if (e.key === "Enter") { + e.preventDefault(); + e.stopPropagation(); + do_get_api_key(); + } }); } - $("#show_api_key").on("click", "button.regenerate_api_key", (e) => { + $("#regenerate_api_key").on("click", (e) => { channel.post({ url: "/json/users/me/api_key/regenerate", success(data) { @@ -363,17 +374,27 @@ export function set_up() { $(this).attr("href", settings_bots.encode_zuliprc_as_uri(data)); }); - $("#api_key_modal [data-dismiss=modal]").on("click", () => { + $("#api_key_modal [data-micromodal-close]").on("click", () => { common.reset_password_toggle_icons( "#get_api_key_password", "#get_api_key_password + .password_visibility_toggle", ); }); - }); + }; $("#api_key_button").on("click", (e) => { + $("body").append(render_settings_api_key_modal()); setup_api_key_modal(); - overlays.open_modal("#api_key_modal"); + $("#api_key_status").hide(); + overlays.open_modal("api_key_modal", { + autoremove: true, + micromodal: true, + micromodal_opts: { + onShow: () => { + $("#get_api_key_password").trigger("focus"); + }, + }, + }); e.preventDefault(); e.stopPropagation(); }); diff --git a/static/styles/settings.css b/static/styles/settings.css index 0bbba544f4..7caf7fb5e0 100644 --- a/static/styles/settings.css +++ b/static/styles/settings.css @@ -990,6 +990,15 @@ input[type="checkbox"] { } } +#api_key_status { + margin: 0; +} + +#download_zuliprc { + color: hsl(0, 0%, 100%); + text-decoration: none; +} + #api_key_form, #change_password_modal { .password-div { @@ -1526,8 +1535,7 @@ input[type="checkbox"] { width: 245px; } - #change_email_modal .change_email_info, - #api_key_modal #api_key_status { + #change_email_modal .change_email_info { margin-top: 3px; } } diff --git a/static/templates/settings/api_key_modal.hbs b/static/templates/settings/api_key_modal.hbs index 99990aaafa..8d43e3b857 100644 --- a/static/templates/settings/api_key_modal.hbs +++ b/static/templates/settings/api_key_modal.hbs @@ -1,42 +1,43 @@ -