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.

ts: Convert settings_config module to TypeScript.

Priyank Patel6b93fe0cd78b
diff --git a/frontend_tests/node_tests/settings_config.js b/frontend_tests/node_tests/settings_config.js
index 427046c9555..96b963b87c8 100644
--- a/frontend_tests/node_tests/settings_config.js
+++ b/frontend_tests/node_tests/settings_config.js
@@ -18,6 +18,13 @@ run_test("all_notifications", () => {
page_params.enable_offline_push_notifications = false;
page_params.enable_offline_email_notifications = true;
+ // Check that it throws error if incorrect settings name
+ // is passed. In this case, we articulate that with
+ // wildcard_mentions_notify being undefined, which will be
+ // the case, if a wrong setting_name is passed.
+ assert.throws(settings_config.all_notifications, "Incorrect setting_name passed");
+
+ page_params.wildcard_mentions_notify = false;
const notifications = settings_config.all_notifications();
assert.deepEqual(notifications.general_settings, [
@@ -45,7 +52,7 @@ run_test("all_notifications", () => {
setting_name: "enable_stream_email_notifications",
},
{
- is_checked: undefined,
+ is_checked: false,
is_disabled: false,
setting_name: "wildcard_mentions_notify",
},
diff --git a/frontend_tests/node_tests/stream_edit.js b/frontend_tests/node_tests/stream_edit.js
index 3d2a1f40516..a2e3d3ce90f 100644
--- a/frontend_tests/node_tests/stream_edit.js
+++ b/frontend_tests/node_tests/stream_edit.js
@@ -41,6 +41,7 @@ const user_groups = zrequire("user_groups");
const user_group_pill = zrequire("user_group_pill");
const user_pill = zrequire("user_pill");
const stream_ui_updates = zrequire("stream_ui_updates");
+const settings_config = zrequire("settings_config");
const jill = {
@@ -291,6 +292,12 @@ test_ui("subscriber_pills", ({override, mock_template}) => {
override(stream_ui_updates, "update_toggler_for_sub", noop);
override(stream_ui_updates, "update_add_subscriptions_elements", noop);
+
+ const {stream_notification_settings, pm_mention_notification_settings} = settings_config;
+ for (const setting of [...stream_notification_settings, ...pm_mention_notification_settings]) {
+ page_params[setting] = true;
+ }
+
stream_row_handler.call(fake_this, event);
assert.ok(template_rendered);
assert.ok(input_typeahead_called);
diff --git a/static/js/page_params.ts b/static/js/page_params.ts
index fd181728e48..66bcd0f17af 100644
--- a/static/js/page_params.ts
+++ b/static/js/page_params.ts
@@ -2,6 +2,14 @@ import $ from "jquery";
const t1 = performance.now();
export const page_params: {
+ enable_desktop_notifications: boolean;
+ enable_offline_email_notifications: boolean;
+ enable_offline_push_notifications: boolean;
+ enable_sounds: boolean;
+ enable_stream_audible_notifications: boolean;
+ enable_stream_desktop_notifications: boolean;
+ enable_stream_email_notifications: boolean;
+ enable_stream_push_notifications: boolean;
language_list: {
code: string;
locale: string;
@@ -9,8 +17,10 @@ export const page_params: {
percent_translated: number | undefined;
}[];
development_environment: boolean;
+ realm_push_notifications_enabled: boolean;
request_language: string;
translation_data: Record<string, string>;
+ wildcard_mentions_notify: boolean;
} = $("#page-params").remove().data("params");
const t2 = performance.now();
export const page_params_parse_time = t2 - t1;
diff --git a/static/js/settings_config.js b/static/js/settings_config.ts
similarity index 90%
rename from static/js/settings_config.js
rename to static/js/settings_config.ts
index 53a22c84899..ba38369dd38 100644
--- a/static/js/settings_config.js
+++ b/static/js/settings_config.ts
@@ -67,7 +67,17 @@ export const twenty_four_hour_time_values = {
},
};
-export const get_all_display_settings = () => ({
+export interface DisplaySettings {
+ settings: {
+ user_display_settings: string[];
+ };
+ render_only: {
+ high_contrast_mode: boolean;
+ dense_mode: boolean;
+ };
+}
+
+export const get_all_display_settings = (): DisplaySettings => ({
settings: {
user_display_settings: [
"dense_mode",
@@ -375,7 +385,8 @@ export const stream_specific_notification_settings = [
"wildcard_mentions_notify",
];
-export const stream_notification_settings = [
+type PageParamsItem = keyof typeof page_params;
+export const stream_notification_settings: PageParamsItem[] = [
"enable_stream_desktop_notifications",
"enable_stream_audible_notifications",
"enable_stream_push_notifications",
@@ -383,7 +394,7 @@ export const stream_notification_settings = [
"wildcard_mentions_notify",
];
-const pm_mention_notification_settings = [
+export const pm_mention_notification_settings: PageParamsItem[] = [
"enable_desktop_notifications",
"enable_sounds",
"enable_offline_push_notifications",
@@ -414,7 +425,15 @@ export const all_notification_settings = other_notification_settings.concat(
stream_notification_settings,
);
-export function get_notifications_table_row_data(notify_settings) {
+type NotificationSettingCheckbox = {
+ setting_name: string;
+ is_disabled: boolean;
+ is_checked: boolean;
+};
+
+export function get_notifications_table_row_data(
+ notify_settings: PageParamsItem[],
+): NotificationSettingCheckbox[] {
return general_notifications_table_labels.realm.map((column, index) => {
const setting_name = notify_settings[index];
if (setting_name === undefined) {
@@ -424,19 +443,38 @@ export function get_notifications_table_row_data(notify_settings) {
is_checked: false,
};
}
+
+ const checked = page_params[setting_name];
+ if (typeof checked !== "boolean") {
+ throw new TypeError(`Incorrect setting_name passed: ${setting_name}`);
+ }
+
const checkbox = {
setting_name,
is_disabled: false,
+ is_checked: checked,
};
if (column === "mobile") {
checkbox.is_disabled = !page_params.realm_push_notifications_enabled;
}
- checkbox.is_checked = page_params[setting_name];
return checkbox;
});
}
-export const all_notifications = () => ({
+export interface AllNotifications {
+ general_settings: {label: string; notification_settings: NotificationSettingCheckbox[]}[];
+ settings: {
+ desktop_notification_settings: string[];
+ mobile_notification_settings: string[];
+ email_notification_settings: string[];
+ };
+ show_push_notifications_tooltip: {
+ push_notifications: boolean;
+ enable_online_push_notifications: boolean;
+ };
+}
+
+export const all_notifications = (): AllNotifications => ({
general_settings: [
{
label: $t({defaultMessage: "Streams"}),