From 5cb36a24c5fecc7b45974da2b864b74fe768d3ae Mon Sep 17 00:00:00 2001 From: Jay Pfaffman Date: Tue, 26 Nov 2024 13:54:53 -0600 Subject: [PATCH] initial commmit --- README.md | 22 +++++- assets/.gitkeep | 0 common/common.scss | 0 desktop/desktop.scss | 0 javascripts/.gitkeep | 0 .../default-user-custom-field-value.js | 70 +++++++++++++++++++ mobile/mobile.scss | 0 scss/.gitkeep | 0 spec/system/.gitkeep | 0 test/acceptance/.gitkeep | 0 10 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 assets/.gitkeep create mode 100644 common/common.scss create mode 100644 desktop/desktop.scss create mode 100644 javascripts/.gitkeep create mode 100644 javascripts/discourse/api-initializers/default-user-custom-field-value.js create mode 100644 mobile/mobile.scss create mode 100644 scss/.gitkeep create mode 100644 spec/system/.gitkeep create mode 100644 test/acceptance/.gitkeep diff --git a/README.md b/README.md index e23cf4a..f852899 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,23 @@ # Default User Custom Field Value -**Theme Summary** +**Make a User Custom Field have a default value (proof of concept) ** -For more information, please see: **url to meta topic** +This sets user_custom_field_1 to "I am not important" if it is null. + +To be useful, settings would determine which field was given a default value and what the value would be. To be really useful, it would do it for an arbitray number of custom user fields. + +Also, it would be better to move the code like + +``` + // if [userfields[1] is null make it a default strring + if (userFields[1] == null) { + userFields[1] = "I am not important"; + } + console.log("got user fields!", userFields); +``` + +into a function so that it could be called in both places. + +It might be better if it called `super()` and/or just updated `this.site.get("user_fields")` rather than overriding anything. + +The other solution would be a plugin that set defaults on custom fields when a user was created and/or had a job that created some default value if none existed. An advantage of this solution is that changing the default value is easy since it doesn't actually exist, where a plugin would need to update all user custom field if there was a change. diff --git a/assets/.gitkeep b/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/common/common.scss b/common/common.scss new file mode 100644 index 0000000..e69de29 diff --git a/desktop/desktop.scss b/desktop/desktop.scss new file mode 100644 index 0000000..e69de29 diff --git a/javascripts/.gitkeep b/javascripts/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/javascripts/discourse/api-initializers/default-user-custom-field-value.js b/javascripts/discourse/api-initializers/default-user-custom-field-value.js new file mode 100644 index 0000000..540d4f5 --- /dev/null +++ b/javascripts/discourse/api-initializers/default-user-custom-field-value.js @@ -0,0 +1,70 @@ +import { apiInitializer } from "discourse/lib/api"; +import discourseComputed, { on } from "discourse-common/utils/decorators"; +import { set } from "@ember/object"; // <-- Import set function here +import { dasherize } from "@ember/string"; +import EmberObject from "@ember/object"; // Import EmberObject here +import UserCardContents from "discourse/components/user-card-contents"; +import { isEmpty } from "@ember/utils"; + +export default apiInitializer("1.8.0", ( api ) => { + console.log("hello world from api initializer!"); + + + api.modifyClass("component:user-card-contents", { + pluginId: "discourse-default-user-custom-fields", + @discourseComputed("user.user_fields.@each.value") + publicUserFields() { + // Custom logic here + console.log("hello world from publicUserFields in my theme component!"); + const siteUserFields = this.site.get("user_fields"); + if (!isEmpty(siteUserFields)) { + console.log("got site user fields!", siteUserFields); + const userFields = this.get("user.user_fields"); + // if [userfields[1] is null make it a default strring + if (userFields[1] == null) { + userFields[1] = "I am not important"; + } + console.log("got user fields!", userFields); + return siteUserFields + .filterBy("show_on_user_card", true) + .sortBy("position") + .map((field) => { + set(field, "dasherized_name", dasherize(field.get("name"))); + const value = userFields ? userFields[field.get("id")] : null; + return isEmpty(value) ? null : EmberObject.create({ value, field }); + }) + .compact(); + } + } + }); + +// Modify User controller + api.modifyClass("controller:user", { + pluginId: "discourse-default-user-custom-fields", + + @discourseComputed("model.user_fields.@each.value") + publicUserFields() { + console.log("Overriding publicUserFields in User controller..."); + const siteUserFields = this.site.get("user_fields"); + + if (!isEmpty(siteUserFields)) { + const userFields = this.get("model.user_fields"); + if (userFields[1] == null) { + userFields[1] = "I am not important"; + } + console.log("got user fields!", userFields); + return siteUserFields + .filterBy("show_on_profile", true) + .sortBy("position") + .map((field) => { + set(field, "dasherized_name", dasherize(field.get("name"))); + const value = userFields + ? userFields[field.get("id").toString()] + : null; + return isEmpty(value) ? null : EmberObject.create({ value, field }); + }) + .compact(); + } + }, + }); +}); diff --git a/mobile/mobile.scss b/mobile/mobile.scss new file mode 100644 index 0000000..e69de29 diff --git a/scss/.gitkeep b/scss/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/spec/system/.gitkeep b/spec/system/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/acceptance/.gitkeep b/test/acceptance/.gitkeep new file mode 100644 index 0000000..e69de29