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.

refactor: Move read_field_data_from_form to settings_components.

Shubham Padiacb4c6452a564
diff --git a/web/src/settings_components.ts b/web/src/settings_components.ts
index 3b3e2ad7126..7abb22d2fae 100644
--- a/web/src/settings_components.ts
+++ b/web/src/settings_components.ts
@@ -359,6 +359,71 @@ function get_message_retention_setting_value(
return check_valid_number_input(custom_input_val);
}
+type FieldData = Record<string, Record<string, string> | string>;
+
+function read_select_field_data_from_form(
+ $profile_field_form: JQuery,
+ old_field_data: FieldData,
+): FieldData {
+ const field_data: FieldData = {};
+ let field_order = 1;
+
+ const old_option_value_map = new Map();
+ if (old_field_data !== undefined) {
+ for (const [value, choice] of Object.entries(old_field_data)) {
+ assert(typeof choice !== "string");
+ old_option_value_map.set(choice.text, value);
+ }
+ }
+ $profile_field_form.find("div.choice-row").each(function (this: HTMLElement) {
+ const text = $(this).find("input")[0].value;
+ if (text) {
+ if (old_option_value_map.get(text) !== undefined) {
+ // Resetting the data-value in the form is
+ // important if the user removed an option string
+ // and then added it back again before saving
+ // changes.
+ $(this).attr("data-value", old_option_value_map.get(text));
+ }
+ const value = $(this).attr("data-value")!;
+ field_data[value] = {text, order: field_order.toString()};
+ field_order += 1;
+ }
+ });
+
+ return field_data;
+}
+
+function read_external_account_field_data($profile_field_form: JQuery): FieldData {
+ const field_data: FieldData = {};
+ field_data.subtype = $profile_field_form
+ .find<HTMLSelectElement & {type: "select-one"}>("select[name=external_acc_field_type]")
+ .val()!;
+ if (field_data.subtype === "custom") {
+ field_data.url_pattern = $profile_field_form
+ .find<HTMLInputElement>("input[name=url_pattern]")
+ .val()!;
+ }
+ return field_data;
+}
+
+export function read_field_data_from_form(
+ field_type_id: number,
+ $profile_field_form: JQuery,
+ old_field_data: FieldData,
+): FieldData | undefined {
+ const field_types = realm.custom_profile_field_types;
+
+ // Only read field data if we are creating a select field
+ // or external account field.
+ if (field_type_id === field_types.SELECT.id) {
+ return read_select_field_data_from_form($profile_field_form, old_field_data);
+ } else if (field_type_id === field_types.EXTERNAL_ACCOUNT.id) {
+ return read_external_account_field_data($profile_field_form);
+ }
+ return undefined;
+}
+
export function sort_object_by_key(obj: Record<string, boolean>): Record<string, boolean> {
const keys = Object.keys(obj).sort();
const new_obj: Record<string, boolean> = {};
diff --git a/web/src/settings_profile_fields.js b/web/src/settings_profile_fields.js
index 6a168d2c3e4..734771a8604 100644
--- a/web/src/settings_profile_fields.js
+++ b/web/src/settings_profile_fields.js
@@ -14,6 +14,7 @@ import * as dialog_widget from "./dialog_widget";
import {$t, $t_html} from "./i18n";
import * as loading from "./loading";
import * as people from "./people";
+import {read_field_data_from_form} from "./settings_components";
import * as settings_ui from "./settings_ui";
import {current_user, realm} from "./state_data";
import * as ui_report from "./ui_report";
@@ -105,45 +106,6 @@ function delete_profile_field(e) {
});
}
-function read_select_field_data_from_form($profile_field_form, old_field_data) {
- const field_data = {};
- let field_order = 1;
-
- const old_option_value_map = new Map();
- if (old_field_data !== undefined) {
- for (const [value, choice] of Object.entries(old_field_data)) {
- old_option_value_map.set(choice.text, value);
- }
- }
-
- $profile_field_form.find("div.choice-row").each(function () {
- const text = $(this).find("input")[0].value;
- if (text) {
- if (old_option_value_map.get(text) !== undefined) {
- // Resetting the data-value in the form is
- // important if the user removed an option string
- // and then added it back again before saving
- // changes.
- $(this).attr("data-value", old_option_value_map.get(text));
- }
- const value = $(this).attr("data-value");
- field_data[value] = {text, order: field_order.toString()};
- field_order += 1;
- }
- });
-
- return field_data;
-}
-
-function read_external_account_field_data($profile_field_form) {
- const field_data = {};
- field_data.subtype = $profile_field_form.find("select[name=external_acc_field_type]").val();
- if (field_data.subtype === "custom") {
- field_data.url_pattern = $profile_field_form.find("input[name=url_pattern]").val();
- }
- return field_data;
-}
-
export function get_value_for_new_option(container) {
let value = 0;
for (const row of $(container).find(".choice-row")) {
@@ -235,19 +197,6 @@ function set_up_create_field_form() {
}
}
-function read_field_data_from_form(field_type_id, $profile_field_form, old_field_data) {
- const field_types = realm.custom_profile_field_types;
-
- // Only read field data if we are creating a select field
- // or external account field.
- if (field_type_id === field_types.SELECT.id) {
- return read_select_field_data_from_form($profile_field_form, old_field_data);
- } else if (field_type_id === field_types.EXTERNAL_ACCOUNT.id) {
- return read_external_account_field_data($profile_field_form);
- }
- return undefined;
-}
-
function open_custom_profile_field_form_modal() {
const html_body = render_add_new_custom_profile_field_form({
realm_default_external_accounts: realm.realm_default_external_accounts,