A platform for community discussion. Free, open, simple.
FIX: Custom groups save button not clearing on admin user page (#37625)
Régis Hanol4acd9c675353
diff --git a/frontend/discourse/admin/controllers/admin-user/index.js b/frontend/discourse/admin/controllers/admin-user/index.jsindex 505750458f0..b2933f1948c 100644--- a/frontend/discourse/admin/controllers/admin-user/index.js+++ b/frontend/discourse/admin/controllers/admin-user/index.js@@ -1,3 +1,4 @@+import { tracked } from "@glimmer/tracking";import Controller from "@ember/controller";import { action, computed } from "@ember/object";import { and, notEmpty } from "@ember/object/computed";@@ -24,8 +25,8 @@ export default class AdminUserIndexController extends Controller {@service siteSettings;@service modal;+ @tracked customGroupIdsBuffer = null;originalPrimaryGroupId = null;- customGroupIdsBuffer = null;availableGroups = null;userTitleValue = null;ssoExternalEmail = null;@@ -44,13 +45,14 @@ export default class AdminUserIndexController extends Controller {@fmt("model.username_lower", userPath("%@/preferences")) preferencesPath;- @discourseComputed("model.customGroups")- customGroupIds(customGroups) {- return customGroups.map((group) => group.id);+ get customGroupIds() {+ return this.model.customGroups.map((group) => group.id);}- @discourseComputed("customGroupIdsBuffer", "customGroupIds")- customGroupsDirty(buffer, original) {+ get customGroupsDirty() {+ const buffer = this.customGroupIdsBuffer;+ const original = this.customGroupIds;+if (buffer === null) {return false;}@@ -60,9 +62,8 @@ export default class AdminUserIndexController extends Controller {: true;}- @discourseComputed("model.automaticGroups")- automaticGroups(automaticGroups) {- return automaticGroups+ get automaticGroups() {+ return this.model.automaticGroups.map((group) => {const name = htmlSafe(group.name);return `<a href="/g/${name}">${name}</a>`;@@ -143,13 +144,13 @@ export default class AdminUserIndexController extends Controller {}groupAdded(added) {- this.model+ return this.model.groupAdded(added).catch(() => this.dialog.alert(i18n("generic_error")));}groupRemoved(groupId) {- this.model+ return this.model.groupRemoved(groupId).then(() => {if (groupId === this.originalPrimaryGroupId) {@@ -588,27 +589,28 @@ export default class AdminUserIndexController extends Controller {}@action- saveCustomGroups() {+ async saveCustomGroups() {const currentIds = this.customGroupIds;const bufferedIds = this.customGroupIdsBuffer;const availableGroups = this.availableGroups;- bufferedIds- .filter((id) => !currentIds.includes(id))- .forEach((id) =>- this.groupAdded(availableGroups.find((value) => value.id === id))- );+ const addedIds = bufferedIds.filter((id) => !currentIds.includes(id));+ for (const id of addedIds) {+ await this.groupAdded(availableGroups.find((value) => value.id === id));+ }++ const removedIds = currentIds.filter((id) => !bufferedIds.includes(id));+ for (const id of removedIds) {+ await this.groupRemoved(id);+ }- currentIds- .filter((id) => !bufferedIds.includes(id))- .forEach((id) => this.groupRemoved(id));+ this.resetCustomGroups();}@actionresetCustomGroups() {- this.set(- "customGroupIdsBuffer",- this.model.customGroups.map((group) => group.id)+ this.customGroupIdsBuffer = this.model.customGroups.map(+ (group) => group.id);}diff --git a/frontend/discourse/admin/models/admin-user.js b/frontend/discourse/admin/models/admin-user.jsindex c7754e46cd9..f2207e8ee09 100644--- a/frontend/discourse/admin/models/admin-user.js+++ b/frontend/discourse/admin/models/admin-user.js@@ -1,4 +1,4 @@-import { filter, gt, lt, not, or } from "@ember/object/computed";+import { gt, lt, not, or } from "@ember/object/computed";import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";import { propertyNotEqual } from "discourse/lib/computed";@@ -6,7 +6,6 @@ import discourseComputed from "discourse/lib/decorators";import getURL from "discourse/lib/get-url";import { trackedArray } from "discourse/lib/tracked-tools";import { userPath } from "discourse/lib/url";-import Group from "discourse/models/group";import User from "discourse/models/user";import { i18n } from "discourse-i18n";@@ -35,8 +34,6 @@ export default class AdminUser extends User {@trackedArray groups;- @filter("groups", (g) => !g.automatic && Group.create(g)) customGroups;- @filter("groups", (g) => g.automatic && Group.create(g)) automaticGroups;@or("active", "staged") canViewProfile;@gt("bounce_score", 0) canResetBounceScore;@propertyNotEqual("originalTrustLevel", "trust_level") dirty;@@ -44,6 +41,14 @@ export default class AdminUser extends User {@not("staff") canSuspend;@not("staff") canSilence;+ get customGroups() {+ return this.groups?.filter((g) => !g.automatic) ?? [];+ }++ get automaticGroups() {+ return this.groups?.filter((g) => g.automatic) ?? [];+ }+@discourseComputed("bounce_score", "reset_bounce_score_after")bounceScore(bounce_score, reset_bounce_score_after) {if (bounce_score > 0) {diff --git a/frontend/discourse/admin/templates/admin-user/index.gjs b/frontend/discourse/admin/templates/admin-user/index.gjsindex 3cbacc288e7..40a94df1d06 100644--- a/frontend/discourse/admin/templates/admin-user/index.gjs+++ b/frontend/discourse/admin/templates/admin-user/index.gjs@@ -674,7 +674,7 @@ export default <template><div class="field">{{i18n "admin.groups.automatic"}}</div><div class="value">{{htmlSafe @controller.automaticGroups}}</div></div>- <div class="display-row">+ <div class="display-row admin-user__custom-groups"><div class="field">{{i18n "admin.groups.custom"}}</div><div class="value"><GroupChooserdiff --git a/spec/system/admin_user_spec.rb b/spec/system/admin_user_spec.rbindex d2179959ae0..fe21e344f1c 100644--- a/spec/system/admin_user_spec.rb+++ b/spec/system/admin_user_spec.rb@@ -264,6 +264,43 @@ describe "Admin User Page", type: :system doendend+ describe "custom groups" do+ fab!(:user)+ fab!(:group)++ it "saves and displays the added group" do+ admin_user_page.visit(user)++ group_chooser = admin_user_page.custom_groups_chooser+ group_chooser.expand+ group_chooser.select_row_by_value(group.id)++ expect(admin_user_page).to have_custom_groups_save_button+ admin_user_page.save_custom_groups+ expect(admin_user_page).to have_no_custom_groups_save_button+ expect(admin_user_page).to have_custom_group(group.name)++ expect(GroupUser.exists?(user:, group:)).to eq(true)+ end++ it "saves and removes the group from the list" do+ group.add(user)+ admin_user_page.visit(user)+ expect(admin_user_page).to have_custom_group(group.name)++ group_chooser = admin_user_page.custom_groups_chooser+ group_chooser.expand+ group_chooser.unselect_by_name(group.name)++ expect(admin_user_page).to have_custom_groups_save_button+ admin_user_page.save_custom_groups+ expect(admin_user_page).to have_no_custom_groups_save_button+ expect(admin_user_page).to have_no_custom_group(group.name)++ expect(GroupUser.exists?(user:, group:)).to eq(false)+ end+ end+context "when logged in as a moderator" dofab!(:current_user, :moderator)diff --git a/spec/system/page_objects/pages/admin_user.rb b/spec/system/page_objects/pages/admin_user.rbindex 6514e13565b..3610b97fb7f 100644--- a/spec/system/page_objects/pages/admin_user.rb+++ b/spec/system/page_objects/pages/admin_user.rb@@ -52,6 +52,30 @@ module PageObjectsfind(".btn-danger.unsilence-user").clickend+ def custom_groups_chooser+ PageObjects::Components::SelectKit.new(".admin-user__custom-groups .group-chooser")+ end++ def save_custom_groups+ find(".admin-user__custom-groups .ok").click+ end++ def has_custom_groups_save_button?+ has_css?(".admin-user__custom-groups .ok")+ end++ def has_no_custom_groups_save_button?+ has_no_css?(".admin-user__custom-groups .ok")+ end++ def has_custom_group?(name)+ has_css?(".admin-user__custom-groups .formatted-selection", text: name)+ end++ def has_no_custom_group?(name)+ has_no_css?(".admin-user__custom-groups .formatted-selection", text: name)+ end+def similar_users_warningfind(".penalty-similar-users .alert-warning")["innerHTML"]end