Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

alert word: Convert inline form to modal.

Ganesh Paware758ec400ba7
diff --git a/frontend_tests/node_tests/alert_words_ui.js b/frontend_tests/node_tests/alert_words_ui.js
index 8cb1f7fc6d3..16d7e552c7e 100644
--- a/frontend_tests/node_tests/alert_words_ui.js
+++ b/frontend_tests/node_tests/alert_words_ui.js
@@ -20,7 +20,6 @@ const noop = () => {};
run_test("rerender_alert_words_ui", ({mock_template}) => {
let list_widget_create_called = false;
alert_words_ui.reset();
- assert.ok(!$("#create_alert_word_name").is_focused());
const ListWidget = mock_esm("../../static/js/list_widget", {
modifier: noop,
create: (container, words, opts) => {
@@ -42,91 +41,6 @@ run_test("rerender_alert_words_ui", ({mock_template}) => {
alert_words_ui.set_up_alert_words();
assert.equal(alert_words_ui.loaded, true);
assert.equal(list_widget_create_called, true);
- assert.ok($("#create_alert_word_name").is_focused());
-});
-
-run_test("add_alert_word", ({override_rewire}) => {
- override_rewire(alert_words_ui, "rerender_alert_words_ui", () => {}); // we've already tested this above
-
- alert_words_ui.set_up_alert_words();
-
- const $create_form = $("#create_alert_word_form");
- const add_func = $create_form.get_on_handler("click", "#create_alert_word_button");
-
- const $new_alert_word = $("#create_alert_word_name");
- const $alert_word_status = $("#alert_word_status");
- const $alert_word_status_text = $(".alert_word_status_text");
- $alert_word_status.set_find_results(".alert_word_status_text", $alert_word_status_text);
-
- // add '' as alert word
- add_func();
- assert.equal($new_alert_word.val(), "");
- assert.ok($alert_word_status.hasClass("alert-danger"));
- assert.equal($alert_word_status_text.text(), "translated: Alert word can't be empty!");
- assert.ok($alert_word_status.visible());
-
- // add 'foo' as alert word (existing word)
- $new_alert_word.val("foo");
-
- add_func();
- assert.ok($alert_word_status.hasClass("alert-danger"));
- assert.equal($alert_word_status_text.text(), "translated: Alert word already exists!");
- assert.ok($alert_word_status.visible());
-
- // add 'zot' as alert word (new word)
- $new_alert_word.val("zot");
-
- let success_func;
- let fail_func;
- channel.post = (opts) => {
- assert.equal(opts.url, "/json/users/me/alert_words");
- assert.deepEqual(opts.data, {alert_words: '["zot"]'});
- success_func = opts.success;
- fail_func = opts.error;
- };
-
- add_func();
-
- // test failure
- fail_func();
- assert.ok($alert_word_status.hasClass("alert-danger"));
- assert.equal($alert_word_status_text.text(), "translated: Error adding alert word!");
- assert.ok($alert_word_status.visible());
-
- // test success
- success_func();
- assert.ok($alert_word_status.hasClass("alert-success"));
- assert.equal(
- $alert_word_status_text.text(),
- 'translated: Alert word "zot" added successfully!',
- );
- assert.ok($alert_word_status.visible());
-});
-
-run_test("add_alert_word_keypress", ({override_rewire}) => {
- override_rewire(alert_words_ui, "rerender_alert_words_ui", () => {});
- alert_words_ui.set_up_alert_words();
-
- const $create_form = $("#create_alert_word_form");
- const keypress_func = $create_form.get_on_handler("keypress", "#create_alert_word_name");
-
- const $new_alert_word = $("#create_alert_word_name");
- $new_alert_word.val("zot");
-
- const event = {
- preventDefault: () => {},
- key: "Enter",
- target: "#create_alert_word_name",
- };
-
- let called = false;
- channel.post = (opts) => {
- assert.deepEqual(opts.data, {alert_words: '["zot"]'});
- called = true;
- };
-
- keypress_func(event);
- assert.ok(called);
});
run_test("remove_alert_word", ({override_rewire}) => {
diff --git a/frontend_tests/puppeteer_tests/settings.ts b/frontend_tests/puppeteer_tests/settings.ts
index 7111c9d5aeb..204ff3eba11 100644
--- a/frontend_tests/puppeteer_tests/settings.ts
+++ b/frontend_tests/puppeteer_tests/settings.ts
@@ -241,8 +241,13 @@ async function test_your_bots_section(page: Page): Promise<void> {
const alert_word_status_selector = "#alert_word_status";
async function add_alert_word(page: Page, word: string): Promise<void> {
- await page.type("#create_alert_word_name", word);
- await page.click("#create_alert_word_button");
+ await page.click("#open-add-alert-word-modal");
+ await common.wait_for_micromodal_to_open(page);
+
+ await page.type("#add-alert-word-name", word);
+ await page.click("#add-alert-word .dialog_submit_button");
+
+ await common.wait_for_micromodal_to_close(page);
}
async function check_alert_word_added(page: Page, word: string): Promise<void> {
@@ -262,23 +267,23 @@ async function close_alert_words_status(page: Page): Promise<void> {
await page.waitForSelector(alert_word_status_selector, {hidden: true});
}
-async function test_and_close_alert_word_added_successfully_status(
- page: Page,
- word: string,
-): Promise<void> {
- const status_text = await get_alert_words_status_text(page);
- assert.strictEqual(status_text, `Alert word "${word}" added successfully!`);
- await close_alert_words_status(page);
-}
-
async function test_duplicate_alert_words_cannot_be_added(
page: Page,
duplicate_word: string,
): Promise<void> {
- await add_alert_word(page, duplicate_word);
- const status_text = await get_alert_words_status_text(page);
+ await page.click("#open-add-alert-word-modal");
+ await common.wait_for_micromodal_to_open(page);
+
+ await page.type("#add-alert-word-name", duplicate_word);
+ await page.click("#add-alert-word .dialog_submit_button");
+
+ const alert_word_status_selector = "#dialog_error";
+ await page.waitForSelector(alert_word_status_selector, {visible: true});
+ const status_text = await common.get_text_from_selector(page, alert_word_status_selector);
assert.strictEqual(status_text, "Alert word already exists!");
- await close_alert_words_status(page);
+
+ await page.click("#add-alert-word .dialog_cancel_button");
+ await common.wait_for_micromodal_to_close(page);
}
async function delete_alert_word(page: Page, word: string): Promise<void> {
@@ -298,7 +303,6 @@ async function test_alert_words_section(page: Page): Promise<void> {
await page.click('[data-section="alert-words"]');
const word = "puppeteer";
await add_alert_word(page, word);
- await test_and_close_alert_word_added_successfully_status(page, word);
await check_alert_word_added(page, word);
await test_duplicate_alert_words_cannot_be_added(page, word);
await test_alert_word_deletion(page, word);
diff --git a/static/js/alert_words_ui.js b/static/js/alert_words_ui.js
index ba9f85aff44..e88a063809f 100644
--- a/static/js/alert_words_ui.js
+++ b/static/js/alert_words_ui.js
@@ -1,11 +1,14 @@
import $ from "jquery";
+import render_add_alert_word from "../templates/settings/add_alert_word.hbs";
import render_alert_word_settings_item from "../templates/settings/alert_word_settings_item.hbs";
import * as alert_words from "./alert_words";
import * as channel from "./channel";
-import {$t} from "./i18n";
+import * as dialog_widget from "./dialog_widget";
+import {$t, $t_html} from "./i18n";
import * as ListWidget from "./list_widget";
+import * as ui_report from "./ui_report";
export let loaded = false;
@@ -26,9 +29,6 @@ export function rerender_alert_words_ui() {
$parent_container: $("#alert-word-settings"),
$simplebar_container: $("#alert-word-settings .progressive-table-wrapper"),
});
-
- // Focus new alert word name text box.
- $("#create_alert_word_name").trigger("focus");
}
function update_alert_word_status(status_text, is_error) {
@@ -42,33 +42,25 @@ function update_alert_word_status(status_text, is_error) {
$alert_word_status.show();
}
-function add_alert_word(alert_word) {
- alert_word = alert_word.trim();
- if (alert_word === "") {
- update_alert_word_status($t({defaultMessage: "Alert word can't be empty!"}), true);
- return;
- } else if (alert_words.has_alert_word(alert_word)) {
- update_alert_word_status($t({defaultMessage: "Alert word already exists!"}), true);
+function add_alert_word(e) {
+ e.preventDefault();
+ e.stopPropagation();
+
+ const alert_word = $("#add-alert-word-name").val().trim();
+
+ if (alert_words.has_alert_word(alert_word)) {
+ ui_report.client_error(
+ $t({defaultMessage: "Alert word already exists!"}),
+ $("#dialog_error"),
+ );
+ dialog_widget.hide_dialog_spinner();
return;
}
const words_to_be_added = [alert_word];
- channel.post({
- url: "/json/users/me/alert_words",
- data: {alert_words: JSON.stringify(words_to_be_added)},
- success() {
- const message = $t(
- {defaultMessage: 'Alert word "{word}" added successfully!'},
- {word: words_to_be_added[0]},
- );
- update_alert_word_status(message, false);
- $("#create_alert_word_name").val("");
- },
- error() {
- update_alert_word_status($t({defaultMessage: "Error adding alert word!"}), true);
- },
- });
+ const data = {alert_words: JSON.stringify(words_to_be_added)};
+ dialog_widget.submit_api_request(channel.post, "/json/users/me/alert_words", data);
}
function remove_alert_word(alert_word) {
@@ -88,14 +80,43 @@ function remove_alert_word(alert_word) {
});
}
+export function show_add_alert_word_modal() {
+ const html_body = render_add_alert_word();
+
+ function add_alert_word_post_render() {
+ const $add_user_group_input_element = $("#add-alert-word-name");
+ const $add_user_group_submit_button = $("#add-alert-word .dialog_submit_button");
+ $add_user_group_submit_button.prop("disabled", true);
+
+ $add_user_group_input_element.on("input", () => {
+ $add_user_group_submit_button.prop(
+ "disabled",
+ $add_user_group_input_element.val().trim() === "",
+ );
+ });
+ }
+
+ dialog_widget.launch({
+ html_heading: $t_html({defaultMessage: "Add a new alert word"}),
+ html_body,
+ html_submit_button: $t_html({defaultMessage: "Add"}),
+ help_link: "/help/pm-mention-alert-notifications#alert-words",
+ form_id: "add-alert-word-form",
+ id: "add-alert-word",
+ loading_spinner: true,
+ on_click: add_alert_word,
+ on_shown: () => $("#add-alert-word-name").trigger("focus"),
+ post_render: add_alert_word_post_render,
+ });
+}
+
export function set_up_alert_words() {
// The settings page must be rendered before this function gets called.
loaded = true;
rerender_alert_words_ui();
- $("#create_alert_word_form").on("click", "#create_alert_word_button", () => {
- const word = $("#create_alert_word_name").val();
- add_alert_word(word);
+ $("#open-add-alert-word-modal").on("click", () => {
+ show_add_alert_word_modal();
});
$("#alert-words-table").on("click", ".remove-alert-word", (event) => {
@@ -103,15 +124,6 @@ export function set_up_alert_words() {
remove_alert_word(word);
});
- $("#create_alert_word_form").on("keypress", "#create_alert_word_name", (event) => {
- // Handle Enter as "add".
- if (event.key === "Enter") {
- event.preventDefault();
- const word = $(event.target).val();
- add_alert_word(word);
- }
- });
-
$("#alert-word-settings").on("click", ".close-alert-word-status", (event) => {
event.preventDefault();
const $alert = $(event.currentTarget).parents(".alert");
diff --git a/static/styles/settings.css b/static/styles/settings.css
index 723b345f9ea..21614b2874f 100644
--- a/static/styles/settings.css
+++ b/static/styles/settings.css
@@ -600,13 +600,11 @@ input[type="checkbox"] {
}
.add-new-emoji-box,
-.add-new-alert-word-box,
.add-new-export-box {
margin-bottom: 20px;
}
.add-new-emoji-box .new-emoji-form,
-.add-new-alert-word-box .new-alert-word-form,
.add-new-export-box {
margin: 10px 0;
}
@@ -833,6 +831,12 @@ input[type="checkbox"] {
}
}
+#add-alert-word {
+ form {
+ margin-bottom: 4px;
+ }
+}
+
.admin-linkifier-form,
.admin-playground-form {
label {
diff --git a/static/templates/settings/add_alert_word.hbs b/static/templates/settings/add_alert_word.hbs
new file mode 100644
index 00000000000..3e758bca69f
--- /dev/null
+++ b/static/templates/settings/add_alert_word.hbs
@@ -0,0 +1,4 @@
+<form id="add-alert-word-form" class="form-horizontal">
+ <label for="add-alert-word-name">{{t "Alert word" }}</label>
+ <input type="text" name="alert-word-name" id="add-alert-word-name" class="required" maxlength=100 placeholder="{{t 'Alert word' }}" value="" />
+</form>
diff --git a/static/templates/settings/alert_word_settings.hbs b/static/templates/settings/alert_word_settings.hbs
index b8efba9efc3..706dfbe0201 100644
--- a/static/templates/settings/alert_word_settings.hbs
+++ b/static/templates/settings/alert_word_settings.hbs
@@ -10,23 +10,9 @@
</button>
<span class="alert_word_status_text"></span>
</div>
- <form id="create_alert_word_form" class="form-horizontal">
- <div class="add-new-alert-word-box grey-box">
- <div class="new-alert-word-form">
- <h4 class="new-alert-word-section-title settings-section-title no-padding">{{t "Add a new alert word"}}</h4>
- <div class="new-alert-word-form">
- <div class="input-group">
- <label for="create_alert_word_name">{{t "New alert word" }}</label>
- <input type="text" name="alert_word_name" id="create_alert_word_name" class="required"
- maxlength=100 placeholder="{{t 'Alert word' }}" value="" />
- </div>
- <button class="button rounded sea-green add-alert-word" id="create_alert_word_button" type="button">
- {{t 'Add alert word'}}
- </button>
- </div>
- </div>
- </div>
- </form>
+ <button class="button rounded sea-green" id="open-add-alert-word-modal" type="button">
+ {{t 'Add alert word'}}
+ </button>
<div class="settings_panel_list_header">
<h3>{{t "Alert words"}}</h3>
diff --git a/tools/test-js-with-node b/tools/test-js-with-node
index c7edff0a126..c955fbdadb0 100755
--- a/tools/test-js-with-node
+++ b/tools/test-js-with-node
@@ -50,6 +50,7 @@ EXEMPT_FILES = make_set(
"static/js/add_subscribers_pill.js",
"static/js/admin.js",
"static/js/alert_popup.ts",
+ "static/js/alert_words_ui.js",
"static/js/archive.js",
"static/js/attachments_ui.js",
"static/js/avatar.js",