settings: Move update_user_custom_profile_fields function.

This is a prep commit to avoid import cycles when we
would use update_user_custom_profile_fields function
in custom_profile_fields_ui.ts in next commit.
This commit is contained in:
Sahil Batra
2025-06-19 15:21:44 +05:30
committed by Tim Abbott
parent df0e70726c
commit 0a684ab0f5
2 changed files with 45 additions and 37 deletions

View File

@@ -6,10 +6,12 @@ import render_settings_custom_user_profile_field from "../templates/settings/cus
import {Typeahead} from "./bootstrap_typeahead.ts";
import * as bootstrap_typeahead from "./bootstrap_typeahead.ts";
import * as channel from "./channel.ts";
import {$t} from "./i18n.ts";
import * as people from "./people.ts";
import * as pill_typeahead from "./pill_typeahead.ts";
import * as settings_components from "./settings_components.ts";
import * as settings_ui from "./settings_ui.ts";
import {current_user, realm} from "./state_data.ts";
import * as typeahead_helper from "./typeahead_helper.ts";
import type {UserPillWidget} from "./user_pill.ts";
@@ -77,6 +79,37 @@ export function append_custom_profile_fields(element_id: string, user_id: number
}
}
export type CustomProfileFieldData = {
id: number;
value?: number[] | string;
};
function update_custom_profile_field(
field: CustomProfileFieldData,
method: channel.AjaxRequestHandler,
): void {
let data;
if (method === channel.del) {
data = JSON.stringify([field.id]);
} else {
data = JSON.stringify([field]);
}
const $spinner_element = $(
`.custom_user_field[data-field-id="${CSS.escape(field.id.toString())}"] .custom-field-status`,
).expectOne();
settings_ui.do_settings_change(method, "/json/users/me/profile_data", {data}, $spinner_element);
}
export function update_user_custom_profile_fields(
fields: CustomProfileFieldData[],
method: channel.AjaxRequestHandler,
): void {
for (const field of fields) {
update_custom_profile_field(field, method);
}
}
export type PillUpdateField = {
type: number;
field_data: string;

View File

@@ -13,7 +13,7 @@ import * as channel from "./channel.ts";
import * as common from "./common.ts";
import {csrf_token} from "./csrf.ts";
import * as custom_profile_fields_ui from "./custom_profile_fields_ui.ts";
import type {PillUpdateField} from "./custom_profile_fields_ui.ts";
import type {CustomProfileFieldData, PillUpdateField} from "./custom_profile_fields_ui.ts";
import * as dialog_widget from "./dialog_widget.ts";
import {$t_html} from "./i18n.ts";
import * as keydown_util from "./keydown_util.ts";
@@ -202,43 +202,15 @@ function settings_change_error(message_html: string, xhr?: JQuery.jqXHR): void {
dialog_widget.hide_dialog_spinner();
}
function update_custom_profile_field(
field: CustomProfileFieldData,
method: channel.AjaxRequestHandler,
): void {
let data;
if (method === channel.del) {
data = JSON.stringify([field.id]);
} else {
data = JSON.stringify([field]);
}
const $spinner_element = $(
`.custom_user_field[data-field-id="${CSS.escape(field.id.toString())}"] .custom-field-status`,
).expectOne();
settings_ui.do_settings_change(method, "/json/users/me/profile_data", {data}, $spinner_element);
}
type CustomProfileFieldData = {
id: number;
value?: number[] | string;
};
function update_user_custom_profile_fields(
fields: CustomProfileFieldData[],
method: channel.AjaxRequestHandler,
): void {
for (const field of fields) {
update_custom_profile_field(field, method);
}
}
function update_user_type_field(field: PillUpdateField, pills: UserPillWidget): void {
const user_ids = user_pill.get_user_ids(pills);
if (user_ids.length === 0) {
update_user_custom_profile_fields([{id: field.id}], channel.del);
custom_profile_fields_ui.update_user_custom_profile_fields([{id: field.id}], channel.del);
} else {
update_user_custom_profile_fields([{id: field.id, value: user_ids}], channel.patch);
custom_profile_fields_ui.update_user_custom_profile_fields(
[{id: field.id, value: user_ids}],
channel.patch,
);
}
}
@@ -745,7 +717,10 @@ export function set_up(): void {
e.stopPropagation();
const $field = $(this).closest(".custom_user_field").expectOne();
const field_id = Number.parseInt($field.attr("data-field-id")!, 10);
update_user_custom_profile_fields([{id: field_id}], channel.del);
custom_profile_fields_ui.update_user_custom_profile_fields(
[{id: field_id}],
channel.del,
);
},
);
@@ -759,10 +734,10 @@ export function set_up(): void {
);
if (value) {
fields.push({id: field_id, value});
update_user_custom_profile_fields(fields, channel.patch);
custom_profile_fields_ui.update_user_custom_profile_fields(fields, channel.patch);
} else {
fields.push({id: field_id});
update_user_custom_profile_fields(fields, channel.del);
custom_profile_fields_ui.update_user_custom_profile_fields(fields, channel.del);
}
});