A platform for community discussion. Free, open, simple.
FIX: Add new groups to the site with the server's group shape (#45058)
Sérgio Saquetim6691d02e0d8b
diff --git a/frontend/discourse/app/controllers/groups/new.js b/frontend/discourse/app/controllers/groups/new.jsindex 54cd8aa3b29..740217beda0 100644--- a/frontend/discourse/app/controllers/groups/new.js+++ b/frontend/discourse/app/controllers/groups/new.js@@ -42,14 +42,18 @@ export default class GroupsNewController extends Controller {group.create()- .then(() => {+ .then((created) => {+ // Matches the shape `SiteSerializer#groups` sends.+ const fullName = created.full_name || created.name;this.site.groups.push({- id: group.id,- name: group.name,- flair_url: group.flair_url,- flair_bg_color: group.flair_bg_color,- flair_color: group.flair_color,- automatic: false,+ id: created.id,+ name: created.name,+ full_name: fullName,+ display_name: fullName,+ flair_url: created.flair_url,+ flair_bg_color: created.flair_bg_color,+ flair_color: created.flair_color,+ automatic: created.automatic,});this.router.transitionTo("group.members", group.name);})diff --git a/frontend/discourse/app/models/group.js b/frontend/discourse/app/models/group.jsindex 3b22b94f85c..bedca7521da 100644--- a/frontend/discourse/app/models/group.js+++ b/frontend/discourse/app/models/group.js@@ -410,6 +410,11 @@ export default class Group extends RestModel {return { ...attrs, ...extraSavePropertiesFor("group", this) };}+ /**+ * Creates the group on the server.+ *+ * @returns {Promise<object>} The created group, as the server serializes it.+ */async create() {await applyModelCallbacks("group", "beforeCreate", this);@@ -426,6 +431,7 @@ export default class Group extends RestModel {await this.reloadMembers();await applyModelCallbacks("group", "afterCreate", this, response);+ return response.basic_group;}async save(opts = {}) {diff --git a/frontend/discourse/tests/acceptance/groups-new-test.js b/frontend/discourse/tests/acceptance/groups-new-test.jsindex 74ee0a51071..c0e105e1df1 100644--- a/frontend/discourse/tests/acceptance/groups-new-test.js+++ b/frontend/discourse/tests/acceptance/groups-new-test.js@@ -1,6 +1,7 @@import { click, fillIn, visit } from "@ember/test-helpers";import { test } from "qunit";import Site from "discourse/models/site";+import pretender, { response } from "discourse/tests/helpers/create-pretender";import { acceptance } from "discourse/tests/helpers/qunit-helpers";import { i18n } from "discourse-i18n";@@ -21,11 +22,13 @@ acceptance("New Group - Authenticated", function (needs) {return helper.response(200, {basic_group: {id: 99,- name: "site-moderators",- flair_url: null,- flair_bg_color: null,- flair_color: null,automatic: false,+ name: "site-moderators",+ full_name: "Site Moderators",+ user_count: 0,+ flair_url: "/uploads/default/flair.png",+ flair_bg_color: "ff0000",+ flair_color: "ffffff",},});});@@ -125,6 +128,46 @@ acceptance("New Group - Authenticated", function (needs) {const newGroup = Site.current().groups.find((g) => g.name === "site-moderators");- assert.strictEqual(newGroup.id, 99);+ assert.deepEqual(+ newGroup,+ {+ id: 99,+ name: "site-moderators",+ full_name: "Site Moderators",+ display_name: "Site Moderators",+ flair_url: "/uploads/default/flair.png",+ flair_bg_color: "ff0000",+ flair_color: "ffffff",+ automatic: false,+ },+ "the new group has the keys the server sends for site groups"+ );+ });++ test("Creating a group without a full name names it after the group", async function (assert) {+ pretender.post("/admin/groups", () =>+ response(200, {+ basic_group: {+ id: 99,+ automatic: false,+ name: "site-moderators",+ full_name: null,+ flair_url: null,+ flair_bg_color: null,+ flair_color: null,+ },+ })+ );++ await visit("/g/custom/new");+ await fillIn("input[name='name']", "site-moderators");+ await click(".group-form-save");++ const newGroup = Site.current().groups.find(+ (g) => g.name === "site-moderators"+ );+ assert.strictEqual(newGroup.full_name, "site-moderators");+ assert.strictEqual(newGroup.display_name, "site-moderators");+ assert.strictEqual(newGroup.flair_url, null);});});