settings_profile_fields: Refactor `read_field_data_from_form`.

This is important setup for supporting external account type fields.
This commit is contained in:
Yashashvi Dave 2019-06-07 11:49:00 +05:30 committed by Tim Abbott
parent 0b4384f4cd
commit e3237cdee2
1 changed files with 19 additions and 17 deletions

View File

@ -62,10 +62,10 @@ function delete_profile_field(e) {
update_profile_fields_table_element();
}
function read_field_data_from_form(selector) {
function read_choice_field_data_from_form(field_elem) {
var field_data = {};
var field_order = 1;
selector.each(function () {
$(field_elem).find('div.choice-row').each(function () {
var text = $(this).find("input")[0].value;
if (text) {
field_data[field_order - 1] = {text: text, order: field_order.toString()};
@ -108,22 +108,25 @@ function clear_form_data() {
$("#profile_field_choices_row").hide();
}
function read_field_data_from_form(field_type_id, field_elem) {
// Only read field data if we are creating a choice field
if (field_type_id === field_types.CHOICE.id) {
return read_choice_field_data_from_form(field_elem);
}
}
function create_profile_field(e) {
e.preventDefault();
e.stopPropagation();
var selector = $('.admin-profile-field-form div.choice-row');
var field_data = {};
var field_type = $('#profile_field_type').val();
if (parseInt(field_type, 10) === field_types.CHOICE.id) {
// Only read choice data if we are creating a choice field.
field_data = read_field_data_from_form(selector);
}
var opts = {
success_continuation: clear_form_data,
};
field_data = read_field_data_from_form(parseInt(field_type, 10),
e.target.closest('.new-profile-field-form'));
var form_data = {
name: $("#profile_field_name").val(),
field_type: field_type,
@ -243,8 +246,8 @@ function open_edit_form(e) {
var data = {};
data.name = profile_field.form.find('input[name=name]').val();
data.hint = profile_field.form.find('input[name=hint]').val();
var selector = profile_field.form.find('div.choice-row');
data.field_data = JSON.stringify(read_field_data_from_form(selector));
data.field_data = JSON.stringify(read_field_data_from_form(parseInt(field.type, 10),
profile_field.form));
settings_ui.do_settings_change(channel.patch, "/json/realm/profile_fields/" + field_id,
data, profile_field_status);
@ -287,14 +290,12 @@ exports.do_populate_profile_fields = function (profile_fields_data) {
_.each(profile_fields_data, function (profile_field) {
order.push(profile_field.id);
var field_data = {};
if (profile_field.field_data !== "") {
if (profile_field.field_data) {
field_data = JSON.parse(profile_field.field_data);
}
var choices = exports.parse_field_choices_from_field_data(field_data);
var is_choice_field = false;
var choices = [];
if (profile_field.type === field_types.CHOICE.id) {
is_choice_field = true;
choices = exports.parse_field_choices_from_field_data(field_data);
}
profile_fields_table.append(
@ -306,7 +307,7 @@ exports.do_populate_profile_fields = function (profile_fields_data) {
hint: profile_field.hint,
type: exports.field_type_id_to_string(profile_field.type),
choices: choices,
is_choice_field: is_choice_field,
is_choice_field: profile_field.type === field_types.CHOICE.id,
},
can_modify: page_params.is_admin,
}
@ -343,7 +344,8 @@ function set_up_choices_field() {
}
$('#profile_field_type').on('change', function (e) {
if (parseInt($(e.target).val(), 10) === field_types.CHOICE.id) {
var selected_field_id = parseInt($(e.target).val(), 10);
if (selected_field_id === field_types.CHOICE.id) {
$("#profile_field_choices_row").show();
} else {
$("#profile_field_choices_row").hide();