DEV: Fix a `userFields` computed property override (#14988)

DEV: Fix a userFields computed property override (#14988)

in preferences/profile controller

Co-authored-by: Penar Musaraj pmusaraj@gmail.com

diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/profile.js b/app/assets/javascripts/discourse/app/controllers/preferences/profile.js
index 6e9403d..26cd76c 100644
--- a/app/assets/javascripts/discourse/app/controllers/preferences/profile.js
+++ b/app/assets/javascripts/discourse/app/controllers/preferences/profile.js
@@ -34,21 +34,20 @@ export default Controller.extend({
 
   @discourseComputed("model.user_fields.@each.value")
   userFields() {
-    let siteUserFields = this.site.get("user_fields");
-    if (!isEmpty(siteUserFields)) {
-      const userFields = this.get("model.user_fields");
+    let siteUserFields = this.site.user_fields;
+    if (isEmpty(siteUserFields)) {
+      return;
+    }
 
-      // Staff can edit fields that are not `editable`
-      if (!this.get("currentUser.staff")) {
-        siteUserFields = siteUserFields.filterBy("editable", true);
-      }
-      return siteUserFields.sortBy("position").map(function (field) {
-        const value = userFields
-          ? userFields[field.get("id").toString()]
-          : null;
-        return EmberObject.create({ value, field });
-      });
+    // Staff can edit fields that are not `editable`
+    if (!this.currentUser.staff) {
+      siteUserFields = siteUserFields.filterBy("editable", true);
     }
+
+    return siteUserFields.sortBy("position").map((field) => {
+      const value = this.model.user_fields?.[field.id.toString()];
+      return EmberObject.create({ field, value });
+    });
   },
 
   @discourseComputed("model.default_calendar")
diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/preferences-profile-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/preferences-profile-test.js
index b301d71..ff1caac 100644
--- a/app/assets/javascripts/discourse/tests/unit/controllers/preferences-profile-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/controllers/preferences-profile-test.js
@@ -1,7 +1,7 @@
 import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
 import { test } from "qunit";
-import EmberObject from "@ember/object";
 import User from "discourse/models/user";
+import Site from "discourse/models/site";
 
 discourseModule("Unit | Controller | preferences/profile", function () {
   test("prepare custom field data", function (assert) {
@@ -11,35 +11,30 @@ discourseModule("Unit | Controller | preferences/profile", function () {
         second_factor_enabled: true,
         is_anonymous: true,
         user_fields: {
-          field_1: "1",
-          field_2: "2",
-          field_3: "3",
+          1: "2",
+          2: null,
+          3: [],
         },
       }),
       currentUser: {
         id: 1234,
       },
     });
-    controller.set("userFields", [
-      EmberObject.create({ value: "2", field: { id: "field_1" } }),
-      EmberObject.create({ value: null, field: { id: "field_2" } }),
-      EmberObject.create({ value: [], field: { id: "field_3" } }),
+
+    Site.currentProp("user_fields", [
+      { position: 1, id: 1, editable: true },
+      { position: 2, id: 2, editable: true },
+      { position: 3, id: 3, editable: true },
     ]);
+
+    // Since there are no injections in unit tests
+    controller.set("site", Site.current());
+
     controller.send("_updateUserFields");
-    assert.strictEqual(
-      controller.model.user_fields.field_1,
-      "2",
-      "updates string value"
-    );
-    assert.strictEqual(
-      controller.model.user_fields.field_2,
-      null,
-      "updates null"
-    );
-    assert.strictEqual(
-      controller.model.user_fields.field_3,
-      null,
-      "updates empty array as null"
-    );
+
+    const fields = controller.model.user_fields;
+    assert.strictEqual(fields[1], "2", "updates string value");
+    assert.strictEqual(fields[2], null, "updates null");
+    assert.strictEqual(fields[3], null, "updates empty array as null");
   });
 });

GitHub sha: bf34d3524b05e7f18cbdda11f357d11d642df3bb

This commit appears in #14988 which was approved by pmusaraj. It was merged by CvX.