custom profile fields: Pass value as part of a dictionary.

While we're at it, we remove the JSON parsing that was part of the
user field code path, since this function isn't responsible for
rendering user fields.
This commit is contained in:
Hemanth V. Alluri
2018-12-31 11:11:06 +05:30
committed by Tim Abbott
parent bdaeccbca1
commit 28d344b4b5
8 changed files with 38 additions and 22 deletions

View File

@@ -981,7 +981,9 @@ exports.set_custom_profile_field_data = function (user_id, field) {
blueslip.error("Unknown field id " + field.id);
return;
}
people_by_user_id_dict.get(user_id).profile_data[field.id] = field.value;
people_by_user_id_dict.get(user_id).profile_data[field.id] = {
value: field.value,
};
};
exports.is_current_user = function (email) {

View File

@@ -236,21 +236,28 @@ exports.show_user_profile = function (user) {
profile_field.is_user_field = false;
profile_field.is_link = field_type === field_types.URL.id;
profile_field.type = field_type;
if (!field_value) {
return;
}
if (!field_value.value) {
return;
}
switch (field_type) {
case field_types.DATE.id:
profile_field.value = moment(field_value).format(localFormat);
profile_field.value = moment(field_value.value).format(localFormat);
break;
case field_types.USER.id:
profile_field.id = field.id;
profile_field.is_user_field = true;
profile_field.value = field_value;
profile_field.value = field_value.value;
break;
case field_types.CHOICE.id:
var field_choice_dict = JSON.parse(field.field_data);
profile_field.value = field_choice_dict[field_value].text;
profile_field.value = field_choice_dict[field_value.value].text;
break;
default:
profile_field.value = field_value;
profile_field.value = field_value.value;
break;
}
profile_data.push(profile_field);

View File

@@ -70,7 +70,10 @@ exports.append_custom_profile_fields = function (element_id, user_id) {
all_custom_fields.forEach(function (field) {
var field_type = field.type;
var type;
var value = people.get_custom_profile_data(user_id, field.id);
var field_value = people.get_custom_profile_data(user_id, field.id);
if (field_value === undefined || field_value === null) {
field_value = {value: "", rendered_value: ""};
}
var is_long_text = field_type === field_types.LONG_TEXT.id;
var is_choice_field = field_type === field_types.CHOICE.id;
var is_user_field = field_type === field_types.USER.id;
@@ -87,7 +90,7 @@ exports.append_custom_profile_fields = function (element_id, user_id) {
field_choices[field_choice_dict[choice].order] = {
value: choice,
text: field_choice_dict[choice].text,
selected: choice === value,
selected: choice === field_value.value,
};
}
}
@@ -101,15 +104,10 @@ exports.append_custom_profile_fields = function (element_id, user_id) {
blueslip.error("Undefined field type.");
}
if (value === undefined || value === null) {
// If user has not set value for field.
value = "";
}
var html = templates.render("custom-user-profile-field", {
field: field,
field_type: type,
field_value: value,
field_value: field_value,
is_long_text_field: is_long_text,
is_choice_field: is_choice_field,
is_user_field: is_user_field,
@@ -134,6 +132,10 @@ exports.intialize_custom_user_type_fields = function (element_id, user_id, is_ed
page_params.custom_profile_fields.forEach(function (field) {
var field_value_raw = people.get_custom_profile_data(user_id, field.id);
if (field_value_raw) {
field_value_raw = field_value_raw.value;
}
// If field is not editable and field value is null, we don't expect
// pill container for that field and proceed further
if (field.type === field_types.USER.id && (field_value_raw || is_editable)) {