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.

custom_profile_fields: Convert edit profile field form into a modal.

yogesh sirsat87ba3c1549bd
diff --git a/frontend_tests/puppeteer_tests/custom-profile.ts b/frontend_tests/puppeteer_tests/custom-profile.ts
index 5f2cfc0afbc..337f86d64b0 100644
--- a/frontend_tests/puppeteer_tests/custom-profile.ts
+++ b/frontend_tests/puppeteer_tests/custom-profile.ts
@@ -4,9 +4,8 @@ import type {Page} from "puppeteer";
import common from "../puppeteer_lib/common";
-// These will be the row and edit form of the the custom profile we add.
-const profile_field_row = "#admin_profile_fields_table tr:nth-last-child(2)";
-const profile_field_form = "#admin_profile_fields_table tr:nth-last-child(1)";
+// This will be the row of the the custom profile field we add.
+const profile_field_row = "#admin_profile_fields_table tr:nth-last-child(1)";
async function test_add_new_profile_field(page: Page): Promise<void> {
await page.click("#add-custom-profile-field-btn");
@@ -28,7 +27,7 @@ async function test_add_new_profile_field(page: Page): Promise<void> {
await common.wait_for_micromodal_to_close(page);
await page.waitForXPath(
- '//*[@id="admin_profile_fields_table"]//tr[last()-1]/td[normalize-space()="Teams"]',
+ '//*[@id="admin_profile_fields_table"]//tr[last()]/td[normalize-space()="Teams"]',
);
assert.strictEqual(
await common.get_text_from_selector(page, `${profile_field_row} span.profile_field_type`),
@@ -37,16 +36,24 @@ async function test_add_new_profile_field(page: Page): Promise<void> {
}
async function test_edit_profile_field(page: Page): Promise<void> {
- await page.click(`${profile_field_row} button.open-edit-form`);
- await page.waitForSelector(`${profile_field_form} form.name-setting`, {visible: true});
- await common.fill_form(page, `${profile_field_form} form.name-setting`, {
+ await page.click(`${profile_field_row} button.open-edit-form-modal`);
+ await common.wait_for_micromodal_to_open(page);
+ assert.strictEqual(
+ await common.get_text_from_selector(page, ".dialog_heading"),
+ "Edit custom profile field",
+ );
+ assert.strictEqual(
+ await common.get_text_from_selector(page, "#dialog_widget_modal .dialog_submit_button"),
+ "Save changes",
+ );
+ await common.fill_form(page, "form.name-setting", {
name: "team",
});
- await page.click(`${profile_field_form} button.submit`);
+ await page.click("#dialog_widget_modal .dialog_submit_button");
+ await common.wait_for_micromodal_to_close(page);
- await page.waitForSelector("#admin-profile-field-status img", {visible: true});
await page.waitForXPath(
- '//*[@id="admin_profile_fields_table"]//tr[last()-1]/td[normalize-space()="team"]',
+ '//*[@id="admin_profile_fields_table"]//tr[last()]/td[normalize-space()="team"]',
);
assert.strictEqual(
await common.get_text_from_selector(page, `${profile_field_row} span.profile_field_type`),
diff --git a/static/js/settings_profile_fields.js b/static/js/settings_profile_fields.js
index 6ae7a3e7bc7..d7bc0002ce9 100644
--- a/static/js/settings_profile_fields.js
+++ b/static/js/settings_profile_fields.js
@@ -4,6 +4,7 @@ import {Sortable} from "sortablejs";
import render_confirm_delete_profile_field_option from "../templates/confirm_dialog/confirm_delete_profile_field_option.hbs";
import render_add_new_custom_profile_field_form from "../templates/settings/add_new_custom_profile_field_form.hbs";
import render_admin_profile_field_list from "../templates/settings/admin_profile_field_list.hbs";
+import render_edit_custom_profile_field_form from "../templates/settings/edit_custom_profile_field_form.hbs";
import render_settings_profile_field_choice from "../templates/settings/profile_field_choice.hbs";
import * as channel from "./channel";
@@ -336,50 +337,62 @@ function set_up_select_field_edit_form($profile_field_form, field_data) {
});
}
-function open_edit_form(e) {
+function open_edit_form_modal(e) {
const field_id = Number.parseInt($(e.currentTarget).attr("data-profile-field-id"), 10);
- const $profile_field_row = $(
- `tr.profile-field-row[data-profile-field-id='${CSS.escape(field_id)}']`,
- );
- const $profile_field_form = $(
- `tr.profile-field-form[data-profile-field-id='${CSS.escape(field_id)}']`,
- );
-
- $profile_field_row.hide();
- $profile_field_form.show();
const field = get_profile_field(field_id);
+
let field_data = {};
if (field.field_data) {
field_data = JSON.parse(field.field_data);
}
-
- if (Number.parseInt(field.type, 10) === field_types.SELECT.id) {
- set_up_select_field_edit_form($profile_field_form, field_data);
- }
-
- if (Number.parseInt(field.type, 10) === field_types.EXTERNAL_ACCOUNT.id) {
- $profile_field_form.find("select[name=external_acc_field_type]").val(field_data.subtype);
- set_up_external_account_field_edit_form($profile_field_form, field_data.url_pattern);
+ let choices = [];
+ if (field.type === field_types.SELECT.id) {
+ choices = parse_field_choices_from_field_data(field_data);
}
- // Set initial value in edit form
- $profile_field_form.find("input[name=name]").val(field.name);
- $profile_field_form.find("input[name=hint]").val(field.hint);
-
- $profile_field_form.find(".reset").on("click", () => {
- // If we do not turn off the click handler, the code is called twice in case
- // when the edit form is closed and then opened again. And in such case two
- // modals are opened and one of them is shown randomly.
- $profile_field_form.find(".submit").off("click");
- $profile_field_form.hide();
- $profile_field_row.show();
+ const html_body = render_edit_custom_profile_field_form({
+ profile_field_info: {
+ id: field.id,
+ name: field.name,
+ hint: field.hint,
+ choices,
+ is_select_field: field.type === field_types.SELECT.id,
+ is_external_account_field: field.type === field_types.EXTERNAL_ACCOUNT.id,
+ },
+ realm_default_external_accounts: page_params.realm_default_external_accounts,
});
- $profile_field_form.find(".submit").on("click", () => {
- e.preventDefault();
- e.stopPropagation();
+ function set_initial_values_of_profile_field() {
+ const $profile_field_form = $("#edit-custom-profile-field-form-" + field_id);
+
+ if (Number.parseInt(field.type, 10) === field_types.SELECT.id) {
+ set_up_select_field_edit_form($profile_field_form, field_data);
+ }
- const $profile_field_status = $("#admin-profile-field-status").expectOne();
+ if (Number.parseInt(field.type, 10) === field_types.EXTERNAL_ACCOUNT.id) {
+ $profile_field_form
+ .find("select[name=external_acc_field_type]")
+ .val(field_data.subtype);
+ set_up_external_account_field_edit_form($profile_field_form, field_data.url_pattern);
+ }
+
+ // Set initial value in edit form
+ $profile_field_form.find("input[name=name]").val(field.name);
+ $profile_field_form.find("input[name=hint]").val(field.hint);
+
+ $profile_field_form
+ .find(".edit_profile_field_choices_container")
+ .on("input", ".choice-row input", add_choice_row);
+ $profile_field_form
+ .find(".edit_profile_field_choices_container")
+ .on("click", "button.delete-choice", delete_choice_row);
+ $(".profile_field_external_accounts_edit select").on("change", () => {
+ set_up_external_account_field_edit_form($profile_field_form, "");
+ });
+ }
+
+ 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.
@@ -396,12 +409,8 @@ function open_edit_form(e) {
data.field_data = JSON.stringify(new_field_data);
function update_profile_field() {
- settings_ui.do_settings_change(
- channel.patch,
- "/json/realm/profile_fields/" + field_id,
- data,
- $profile_field_status,
- );
+ const url = "/json/realm/profile_fields/" + field_id;
+ dialog_widget.submit_api_request(channel.patch, url, data);
}
if (field.type === field_types.SELECT.id) {
@@ -412,29 +421,24 @@ function open_edit_form(e) {
);
if (deleted_values.size !== 0) {
- show_modal_for_deleting_options(field, deleted_values, update_profile_field);
+ const edit_select_field_modal_callback = () =>
+ show_modal_for_deleting_options(field, deleted_values, update_profile_field);
+ dialog_widget.close_modal(edit_select_field_modal_callback);
return;
}
}
update_profile_field();
- });
+ }
- $profile_field_form
- .find(".edit_profile_field_choices_container")
- .on("input", ".choice-row input", add_choice_row);
- $profile_field_form
- .find(".edit_profile_field_choices_container")
- .on("click", "button.delete-choice", delete_choice_row);
- $(".profile_field_external_accounts_edit select").on("change", (e) => {
- const field_id = Number.parseInt(
- $(e.target).closest(".profile-field-form").attr("data-profile-field-id"),
- 10,
- );
- const $profile_field_form = $(
- `tr.profile-field-form[data-profile-field-id='${CSS.escape(field_id)}']`,
- );
- set_up_external_account_field_edit_form($profile_field_form, "");
+ const edit_custom_profile_field_form_id = "edit-custom-profile-field-form-" + field_id;
+ dialog_widget.launch({
+ form_id: edit_custom_profile_field_form_id,
+ html_heading: $t_html({defaultMessage: "Edit custom profile field"}),
+ html_body,
+ on_click: submit_form,
+ post_render: set_initial_values_of_profile_field,
+ loading_spinner: true,
});
}
@@ -585,5 +589,5 @@ export function build_page() {
$("#admin_profile_fields_table").on("click", ".delete", delete_profile_field);
$("#add-custom-profile-field-btn").on("click", open_custom_profile_field_form_modal);
- $("#admin_profile_fields_table").on("click", ".open-edit-form", open_edit_form);
+ $("#admin_profile_fields_table").on("click", ".open-edit-form-modal", open_edit_form_modal);
}
diff --git a/static/styles/settings.css b/static/styles/settings.css
index bdfe551e40b..5c1d56d3af4 100644
--- a/static/styles/settings.css
+++ b/static/styles/settings.css
@@ -644,6 +644,7 @@ input[type="checkbox"] {
}
.admin_profile_fields_table,
+.edit_profile_field_choices_container,
.profile_field_choices_table {
.movable-profile-field-row {
cursor: move;
diff --git a/static/templates/settings/admin_profile_field_list.hbs b/static/templates/settings/admin_profile_field_list.hbs
index 2d0ad16749c..bb2a65a57cc 100644
--- a/static/templates/settings/admin_profile_field_list.hbs
+++ b/static/templates/settings/admin_profile_field_list.hbs
@@ -15,7 +15,7 @@
</td>
{{#if ../can_modify}}
<td>
- <button class="button rounded small btn-warning open-edit-form" title="{{t 'Edit' }}" data-profile-field-id="{{id}}">
+ <button class="button rounded small btn-warning open-edit-form-modal" title="{{t 'Edit' }}" data-profile-field-id="{{id}}">
<i class="fa fa-pencil" aria-hidden="true"></i>
</button>
<button class="button rounded small delete btn-danger" title="{{t 'Delete' }}" data-profile-field-id="{{id}}">
@@ -24,54 +24,4 @@
</td>
{{/if}}
</tr>
-<tr class="profile-field-form" data-profile-field-id="{{id}}" style="display: none;">
- <td colspan="3">
- <form class="form-horizontal name-setting">
- <div class="input-group">
- <label for="name">{{t "Label" }}</label>
- <input type="text" name="name" value="{{ name }}" maxlength="40" />
- </div>
- <div class="input-group hint_change_container">
- <label for="hint">{{t "Hint" }}</label>
- <input type="text" name="hint" value="{{ hint }}" maxlength="80" />
- </div>
- {{#if is_select_field }}
- <div class="input-group">
- <label for="profile_field_choices_edit">{{t "Field choices" }}</label>
- <div class="profile-field-choices" name="profile_field_choices_edit">
- <hr />
- <div class="edit_profile_field_choices_container">
- {{#each choices}}
- {{> profile_field_choice }}
- {{/each}}
- </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">
- {{#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" name="url_pattern" autocomplete="off" maxlength="80" />
- </div>
- {{/if}}
- <div class="input-group">
- <button type="button" class="button rounded sea-green submit">
- {{t 'Save changes' }}
- </button>
- <button type="button" class="button rounded btn-danger reset">
- {{t 'Cancel' }}
- </button>
- </div>
- </form>
- </td>
-</tr>
{{/with}}
diff --git a/static/templates/settings/edit_custom_profile_field_form.hbs b/static/templates/settings/edit_custom_profile_field_form.hbs
new file mode 100644
index 00000000000..f7a2654e261
--- /dev/null
+++ b/static/templates/settings/edit_custom_profile_field_form.hbs
@@ -0,0 +1,40 @@
+{{#with profile_field_info}}
+<form class="form-horizontal name-setting profile-field-form new-style" id="edit-custom-profile-field-form-{{id}}">
+ <div class="input-group">
+ <label for="name">{{t "Label" }}</label>
+ <input type="text" name="name" value="{{ name }}" maxlength="40" />
+ </div>
+ <div class="input-group hint_change_container">
+ <label for="hint">{{t "Hint" }}</label>
+ <input type="text" name="hint" value="{{ hint }}" maxlength="80" />
+ </div>
+ {{#if is_select_field }}
+ <div class="input-group">
+ <label for="profile_field_choices_edit">{{t "Field choices" }}</label>
+ <div class="profile-field-choices" name="profile_field_choices_edit">
+ <hr />
+ <div class="edit_profile_field_choices_container">
+ {{#each choices}}
+ {{> profile_field_choice }}
+ {{/each}}
+ </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">
+ {{#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" name="url_pattern" autocomplete="off" maxlength="80" />
+ </div>
+ {{/if}}
+</form>
+{{/with}}