Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
custom_profile_fields: Don't send extra params during editing in dialog.
Shubham Padia2c0518ff69bd
diff --git a/web/src/settings_components.ts b/web/src/settings_components.tsindex 7abb22d2fae..b69bdc636b8 100644--- a/web/src/settings_components.ts+++ b/web/src/settings_components.ts@@ -12,6 +12,7 @@ import {$t} from "./i18n";import {realm_user_settings_defaults} from "./realm_user_settings_defaults";import * as scroll_util from "./scroll_util";import * as settings_config from "./settings_config";+import type {CustomProfileField} from "./state_data";import {realm} from "./state_data";import * as stream_data from "./stream_data";import type {StreamSubscription} from "./sub_store";@@ -98,14 +99,17 @@ export function get_property_value(| RealmSettingProperties| StreamSettingProperties| keyof UserGroup+ | keyof CustomProfileField| RealmUserSettingDefaultProperties,for_realm_default_settings?: boolean,sub?: StreamSubscription,group?: UserGroup,+ custom_profile_field?: CustomProfileField,):| valueof<RealmSetting>| valueof<StreamSubscription>| valueof<UserGroup>+ | valueof<CustomProfileField>| valueof<RealmUserSettingDefaultType> {if (for_realm_default_settings) {// realm_user_default_settings are stored in a separate object.@@ -138,6 +142,15 @@ export function get_property_value(return group[property_name as keyof UserGroup];}+ if (custom_profile_field) {+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions+ const value = custom_profile_field[property_name as keyof CustomProfileField];+ if (property_name === "display_in_profile_summary" && value === undefined) {+ return false;+ }+ return value;+ }+if (property_name === "realm_org_join_restrictions") {if (realm.realm_emails_restricted_to_domains) {return "only_selected_domain";@@ -184,6 +197,10 @@ export function extract_property_name($elem: JQuery, for_realm_default_settings?return /^id_authmethod[\da-z]+_(.*)$/.exec(elem_id)![1];}+ if (elem_id.startsWith("id-custom-profile-field")) {+ return /^id_custom_profile_field_(.*)$/.exec(elem_id.replaceAll("-", "_"))![1];+ }+return /^id_(.*)$/.exec(elem_id.replaceAll("-", "_"))![1];}@@ -424,6 +441,22 @@ export function read_field_data_from_form(return undefined;}+function get_field_data_input_value($input_elem: JQuery): string | undefined {+ const $profile_field_form = $input_elem.closest(".profile-field-form");+ const profile_field_id = Number.parseInt(+ $($profile_field_form).attr("data-profile-field-id")!,+ 10,+ );+ const field = realm.custom_profile_fields.find((field) => field.id === profile_field_id)!;++ const proposed_value = read_field_data_from_form(+ field.type,+ $profile_field_form,+ JSON.parse(field.field_data),+ );+ return JSON.stringify(proposed_value);+}+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> = {};@@ -672,6 +705,8 @@ export function get_input_element_value(return get_message_retention_setting_value($(input_elem));case "dropdown-list-widget":return get_dropdown_list_widget_setting_value($input_elem);+ case "field-data-setting":+ return get_field_data_input_value($input_elem);default:return undefined;}@@ -762,6 +797,7 @@ type setting_property_type =| RealmSettingProperties| StreamSettingProperties| keyof UserGroup+ | keyof CustomProfileField| RealmUserSettingDefaultProperties;export function check_property_changed(@@ -769,6 +805,7 @@ export function check_property_changed(for_realm_default_settings: boolean,sub: StreamSubscription | undefined,group: UserGroup | undefined,+ custom_profile_field: CustomProfileField | undefined,): boolean {const $elem = $(elem);// eslint-disable-next-line @typescript-eslint/consistent-type-assertions@@ -776,7 +813,14 @@ export function check_property_changed($elem,for_realm_default_settings,) as setting_property_type;- let current_val = get_property_value(property_name, for_realm_default_settings, sub, group);+ let current_val = get_property_value(+ property_name,+ for_realm_default_settings,+ sub,+ group,+ custom_profile_field,+ );+let proposed_val;switch (property_name) {@@ -823,6 +867,9 @@ export function check_property_changed(case "stream_privacy":proposed_val = get_input_element_value(elem, "radio-group");break;+ case "field_data":+ proposed_val = get_input_element_value(elem, "field-data-setting");+ break;default:if (current_val !== undefined) {proposed_val = get_input_element_value(elem, typeof current_val);@@ -859,7 +906,7 @@ export function save_discard_widget_status_handler($subsection.find(".save-button").show();const properties_elements = get_subsection_property_elements($subsection);const show_change_process_button = properties_elements.some((elem) =>- check_property_changed(elem, for_realm_default_settings, sub, group),+ check_property_changed(elem, for_realm_default_settings, sub, group, undefined),);const $save_btn_controls = $subsection.find(".subsection-header .save-button-controls");diff --git a/web/src/settings_org.js b/web/src/settings_org.jsindex e11ce1a0bca..a17c879a240 100644--- a/web/src/settings_org.js+++ b/web/src/settings_org.js@@ -826,7 +826,13 @@ export function init_dropdown_widgets() {can_access_all_users_group_widget.setup();}-export function populate_data_for_request(subsection, for_realm_default_settings, sub, group) {+export function populate_data_for_request(+ subsection,+ for_realm_default_settings,+ sub,+ group,+ custom_profile_field = undefined,+) {let data = {};const properties_elements = settings_components.get_subsection_property_elements(subsection);@@ -838,12 +844,13 @@ export function populate_data_for_request(subsection, for_realm_default_settingsfor_realm_default_settings,sub,group,+ custom_profile_field,)) {const input_value = settings_components.get_input_element_value(input_elem);if (input_value !== undefined) {let property_name;- if (for_realm_default_settings || sub || group) {+ if (for_realm_default_settings || sub || group || custom_profile_field) {property_name = settings_components.extract_property_name($input_elem,for_realm_default_settings,diff --git a/web/src/settings_profile_fields.js b/web/src/settings_profile_fields.jsindex 734771a8604..83695fce166 100644--- a/web/src/settings_profile_fields.js+++ b/web/src/settings_profile_fields.js@@ -15,6 +15,7 @@ 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 {populate_data_for_request} from "./settings_org";import * as settings_ui from "./settings_ui";import {current_user, realm} from "./state_data";import * as ui_report from "./ui_report";@@ -454,23 +455,21 @@ function open_edit_form_modal(e) {function submit_form() {const $profile_field_form = $("#edit-custom-profile-field-form-" + field_id);- // For some reason jQuery's serialize() is not working with- // channel.patch even though it is supported by $.ajax.- const data = {};-- data.name = $profile_field_form.find("input[name=name]").val();- data.hint = $profile_field_form.find("input[name=hint]").val();- data.display_in_profile_summary = $profile_field_form- .find("input[name=display_in_profile_summary]")- .is(":checked");- data.required = $profile_field_form.find("input[name=required]").is(":checked");-- const new_field_data = read_field_data_from_form(- Number.parseInt(field.type, 10),+ const data = populate_data_for_request($profile_field_form,- field_data,+ false,+ undefined,+ undefined,+ field,);- data.field_data = JSON.stringify(new_field_data);++ // If there is no data to be changed, we display our success status+ // since save changes button is always enabled.+ if (Object.keys(data).length === 0) {+ dialog_widget.close();+ display_success_status();+ return;+ }function update_profile_field() {const url = "/json/realm/profile_fields/" + field_id;@@ -482,8 +481,8 @@ function open_edit_form_modal(e) {dialog_widget.submit_api_request(channel.patch, url, data, opts);}- if (field.type === field_types.SELECT.id) {- const new_values = new Set(Object.keys(new_field_data));+ if (field.type === field_types.SELECT.id && data.field_data !== undefined) {+ const new_values = new Set(Object.keys(JSON.parse(data.field_data)));const deleted_values = {};for (const [value, option] of Object.entries(field_data)) {if (!new_values.has(value)) {diff --git a/web/src/state_data.ts b/web/src/state_data.tsindex 84e661d1916..d0d6bd6b1f1 100644--- a/web/src/state_data.ts+++ b/web/src/state_data.ts@@ -67,6 +67,19 @@ const onboarding_step_schema = z.union([one_time_notice_schema, raw_hotspot_scheexport type OnboardingStep = z.output<typeof onboarding_step_schema>;+export const custom_profile_field_schema = z.object({+ display_in_profile_summary: z.optional(z.boolean()),+ field_data: z.string(),+ hint: z.string(),+ id: z.number(),+ name: z.string(),+ order: z.number(),+ required: z.boolean(),+ type: z.number(),+});++export type CustomProfileField = z.output<typeof custom_profile_field_schema>;+export const current_user_schema = z.object({avatar_source: z.string(),delivery_email: z.string(),@@ -81,18 +94,7 @@ export const current_user_schema = z.object({// Sync this with zerver.lib.events.do_events_register.export const realm_schema = z.object({- custom_profile_fields: z.array(- z.object({- display_in_profile_summary: z.optional(z.boolean()),- field_data: z.string(),- hint: z.string(),- id: z.number(),- name: z.string(),- order: z.number(),- required: z.boolean(),- type: z.number(),- }),- ),+ custom_profile_fields: z.array(custom_profile_field_schema),custom_profile_field_types: z.object({SHORT_TEXT: z.object({id: z.number(), name: z.string()}),LONG_TEXT: z.object({id: z.number(), name: z.string()}),diff --git a/web/templates/settings/edit_custom_profile_field_form.hbs b/web/templates/settings/edit_custom_profile_field_form.hbsindex f60faf0f180..3677441ad7e 100644--- a/web/templates/settings/edit_custom_profile_field_form.hbs+++ b/web/templates/settings/edit_custom_profile_field_form.hbs@@ -1,15 +1,15 @@{{#with profile_field_info}}-<form class="name-setting profile-field-form new-style" id="edit-custom-profile-field-form-{{id}}">+<form class="name-setting profile-field-form new-style" id="edit-custom-profile-field-form-{{id}}" data-profile-field-id="{{id}}"><div class="input-group"><label for="name">{{t "Label" }}</label>- <input type="text" name="name" class="modal_text_input" value="{{ name }}" maxlength="40" />+ <input type="text" name="name" id="id-custom-profile-field-name" class="modal_text_input prop-element" value="{{ name }}" maxlength="40" data-setting-widget-type="string" /></div><div class="input-group hint_change_container"><label for="hint">{{t "Hint" }}</label>- <input type="text" name="hint" class="modal_text_input" value="{{ hint }}" maxlength="80" />+ <input type="text" name="hint" id="id-custom-profile-field-hint" class="modal_text_input prop-element" value="{{ hint }}" maxlength="80" data-setting-widget-type="string" /></div>{{#if is_select_field }}- <div class="input-group">+ <div class="input-group prop-element" id="id-custom-profile-field-field-data" data-setting-widget-type="field-data-setting"><label for="profile_field_choices_edit">{{t "Field choices" }}</label><div class="profile-field-choices" name="profile_field_choices_edit"><hr />@@ -20,34 +20,35 @@</div></div></div>- {{/if}}- {{#if is_external_account_field}}- <div class="input-group profile_field_external_accounts_edit">- <label for="external_acc_field_type">{{t "External account type" }}</label>- <select name="external_acc_field_type" class="modal_select" disabled>- {{#each ../realm_default_external_accounts}}- <option value='{{@key}}'>{{this.text}}</option>- {{/each}}- <option value="custom">{{t 'Custom' }}</option>- </select>- </div>- <div class="input-group custom_external_account_detail">- <label for="url_pattern">{{t "URL pattern" }}</label>- <input type="url" class="modal_url_input" name="url_pattern" autocomplete="off" maxlength="80" />+ {{else if is_external_account_field}}+ <div class="prop-element" id="id-custom-profile-field-field-data" data-setting-widget-type="field-data-setting">+ <div class="input-group profile_field_external_accounts_edit" >+ <label for="external_acc_field_type">{{t "External account type" }}</label>+ <select name="external_acc_field_type" class="modal_select" disabled>+ {{#each ../realm_default_external_accounts}}+ <option value='{{@key}}'>{{this.text}}</option>+ {{/each}}+ <option value="custom">{{t 'Custom' }}</option>+ </select>+ </div>+ <div class="input-group custom_external_account_detail">+ <label for="url_pattern">{{t "URL pattern" }}</label>+ <input type="url" class="modal_url_input" name="url_pattern" autocomplete="off" maxlength="80" />+ </div></div>{{/if}}{{#if valid_to_display_in_summary}}<div class="input-group">- <label class="checkbox edit_profile_field_display_label" for="edit_display_in_profile_summary">- <input class="edit_display_in_profile_summary" type="checkbox" id="edit_display_in_profile_summary" name="display_in_profile_summary" {{#if display_in_profile_summary}} checked="checked" {{/if}}/>+ <label class="checkbox edit_profile_field_display_label" for="id-custom-profile-field-display-in-profile-summary">+ <input class="edit_display_in_profile_summary prop-element" data-setting-widget-type="boolean" type="checkbox" id="id-custom-profile-field-display-in-profile-summary" name="display_in_profile_summary" data-setting-widget-type="boolean" {{#if display_in_profile_summary}} checked="checked" {{/if}}/><span></span>{{t 'Display on user card' }}</label></div>{{/if}}<div class="input-group">- <label class="checkbox" for="edit-required">- <input class="edit-required" type="checkbox" id="edit-required" name="required" {{#if required}} checked="checked" {{/if}}/>+ <label class="checkbox" for="id-custom-profile-field-required">+ <input class="edit-required prop-element" type="checkbox" id="id-custom-profile-field-required" name="required" data-setting-widget-type="boolean" {{#if required}} checked="checked" {{/if}}/><span></span>{{t 'Required field' }}</label>