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.

inline_topic_edit: Show invalid input error visually on empty topic.

Sayam Samal196fcd5c0a33
diff --git a/web/src/message_edit.ts b/web/src/message_edit.ts
index d6c431167b3..38938c91e7a 100644
--- a/web/src/message_edit.ts
+++ b/web/src/message_edit.ts
@@ -445,6 +445,16 @@ function handle_inline_topic_edit_keydown(this: HTMLElement, e: JQuery.KeyDownEv
}
}
+function handle_inline_topic_edit_change(this: HTMLInputElement): void {
+ const $inline_topic_edit_input = $(this);
+ if ($inline_topic_edit_input.hasClass("invalid-input")) {
+ // If invalid-input class is present on the inline topic edit
+ // input field, remove it as soon as the user starts typing
+ // as that probably means the user is trying to fix the error.
+ $inline_topic_edit_input.removeClass("invalid-input");
+ }
+}
+
function timer_text(seconds_left: number): string {
const minutes = Math.floor(seconds_left / 60);
const seconds = seconds_left % 60;
@@ -921,6 +931,8 @@ export function start_inline_topic_edit($recipient_row: JQuery): void {
);
$form.on("keydown", handle_inline_topic_edit_keydown);
+
+ $inline_topic_edit_input.on("input", handle_inline_topic_edit_change);
}
export function end_inline_topic_edit($row: JQuery): void {
@@ -980,7 +992,8 @@ export function try_save_inline_topic_edit($row: JQuery): void {
const message = message_lists.current.get(message_id);
assert(message?.type === "stream");
const old_topic = message.topic;
- const new_topic = $row.find<HTMLInputElement>("input.inline_topic_edit").val()?.trim();
+ const $inline_topic_edit_input = $row.find<HTMLInputElement>("input.inline_topic_edit");
+ const new_topic = $inline_topic_edit_input.val()?.trim();
assert(new_topic !== undefined);
const topic_changed = new_topic !== old_topic;
@@ -991,6 +1004,15 @@ export function try_save_inline_topic_edit($row: JQuery): void {
return;
}
+ if (realm.realm_mandatory_topics && util.is_topic_name_considered_empty(new_topic)) {
+ // When the topic is mandatory in a realm and the new topic is considered
+ // empty, we don't allow the user to save the topic. Instead, we show the
+ // error visually via the invalid-input class and focus on the input field.
+ $inline_topic_edit_input.addClass("invalid-input");
+ $inline_topic_edit_input.trigger("focus");
+ return;
+ }
+
const $message_header = $row.find(".message_header").expectOne();
const stream_id = Number.parseInt($message_header.attr("data-stream-id")!, 10);
const stream_topics = stream_topic_history.get_recent_topic_names(stream_id);
diff --git a/web/src/util.ts b/web/src/util.ts
index 28588a776e8..faa45f74c97 100644
--- a/web/src/util.ts
+++ b/web/src/util.ts
@@ -547,6 +547,17 @@ export function get_final_topic_display_name(topic_name: string): string {
return topic_name;
}
+export function is_topic_name_considered_empty(topic: string): boolean {
+ // NOTE: Use this check only when realm.realm_mandatory_topics is set to true.
+ topic = topic.trim();
+ // When the topic is mandatory in a realm via realm_mandatory_topics, the topic
+ // can't be an empty string, "(no topic)", or realm_empty_topic_display_name.
+ if (topic === "" || topic === "(no topic)" || topic === realm.realm_empty_topic_display_name) {
+ return true;
+ }
+ return false;
+}
+
export function get_retry_backoff_seconds(
xhr: JQuery.jqXHR<unknown>,
attempts: number,
diff --git a/web/styles/app_components.css b/web/styles/app_components.css
index b94ee6417fd..6df35d25456 100644
--- a/web/styles/app_components.css
+++ b/web/styles/app_components.css
@@ -1408,3 +1408,15 @@ input.settings_text_input {
outline: none;
}
}
+
+/* Styling for invalid inputs */
+input.invalid-input {
+ /* The use of !important is necessary here to override
+ border-color specified for text input elements in
+ dark_theme.css which has a higher specificity. */
+ border-color: var(--color-invalid-input-border) !important;
+
+ &:focus {
+ box-shadow: var(--color-invalid-input-box-shadow);
+ }
+}
diff --git a/web/tests/util.test.cjs b/web/tests/util.test.cjs
index 3db34efce55..705f5a1841b 100644
--- a/web/tests/util.test.cjs
+++ b/web/tests/util.test.cjs
@@ -511,6 +511,22 @@ run_test("get_final_topic_display_name", ({override}) => {
assert.deepEqual(util.get_final_topic_display_name(""), "random topic name");
});
+run_test("is_topic_name_considered_empty", ({override}) => {
+ // Topic is not considered empty if it is distinct string
+ // other than "(no topic)", or realm_empty_topic_display_name.
+ assert.ok(!util.is_topic_name_considered_empty("some topic"));
+
+ // Topic is considered empty if it is an empty string.
+ assert.ok(util.is_topic_name_considered_empty(""));
+
+ // Topic is considered empty if it is equal to "(no topic)".
+ assert.ok(util.is_topic_name_considered_empty("(no topic)"));
+
+ // Topic name is considered empty if it is equal to realm_empty_topic_display_name.
+ override(realm, "realm_empty_topic_display_name", "general chat");
+ assert.ok(util.is_topic_name_considered_empty("general chat"));
+});
+
run_test("get_retry_backoff_seconds", () => {
const xhr_500_error = {
status: 500,