From e5fbb163b2a1d94aa42157a579a79871c5dce8a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= Date: Mon, 16 Dec 2024 16:29:57 -0300 Subject: [PATCH] DEV: Replace `TextField` with native `input` on CategoryNameFields --- .../addon/utils/decorators.js | 25 ++++++++++++ .../app/components/category-name-fields.gjs | 39 +++++++++++++++++++ .../app/components/category-name-fields.hbs | 26 ------------- .../app/components/category-name-fields.js | 3 -- .../discourse/app/models/category.js | 6 ++- 5 files changed, 69 insertions(+), 30 deletions(-) create mode 100644 app/assets/javascripts/discourse/app/components/category-name-fields.gjs delete mode 100644 app/assets/javascripts/discourse/app/components/category-name-fields.hbs delete mode 100644 app/assets/javascripts/discourse/app/components/category-name-fields.js diff --git a/app/assets/javascripts/discourse-common/addon/utils/decorators.js b/app/assets/javascripts/discourse-common/addon/utils/decorators.js index 1bc54021b1f..404bbc9542e 100644 --- a/app/assets/javascripts/discourse-common/addon/utils/decorators.js +++ b/app/assets/javascripts/discourse-common/addon/utils/decorators.js @@ -1,3 +1,4 @@ +import { assert } from "@ember/debug"; import { observer } from "@ember/object"; import { alias as EmberAlias, @@ -32,6 +33,7 @@ import { import CoreObject from "@ember/object/core"; import { on as emberOn } from "@ember/object/evented"; import { bind as emberBind, schedule } from "@ember/runloop"; +import { classify } from "@ember/string"; import { observes as emberObservesDecorator, on as emberOnDecorator, @@ -156,6 +158,29 @@ export function observes(...observeParams) { }; } +export function settable(self, prop, descriptor) { + const setterFunctionName = `set${classify(prop)}`; + + Object.defineProperty(self, setterFunctionName, { + configurable: true, + get() { + const bound = emberBind(this, function (event) { + assert( + `\`${setterFunctionName}\` should only receive an Event object as argument. Use it to set the value of \`${prop}\` when using the \`on\` modifier, e.g. \`{{on "input" this.${setterFunctionName}}}\``, + event instanceof Event + ); + this[prop] = event.target.value; + }); + + Object.defineProperty(this, `set${classify(prop)}`, { value: bound }); + + return bound; + }, + }); + + return descriptor; +} + export const alias = macroAlias(EmberAlias); export const and = macroAlias(EmberAnd); export const bool = macroAlias(EmberBool); diff --git a/app/assets/javascripts/discourse/app/components/category-name-fields.gjs b/app/assets/javascripts/discourse/app/components/category-name-fields.gjs new file mode 100644 index 00000000000..7c9e26c60e6 --- /dev/null +++ b/app/assets/javascripts/discourse/app/components/category-name-fields.gjs @@ -0,0 +1,39 @@ +import { hash } from "@ember/helper"; +import { on } from "@ember/modifier"; +import PluginOutlet from "discourse/components/plugin-outlet"; +import { i18n } from "discourse-i18n"; + +const CategoryNameFields = ; + +export default CategoryNameFields; diff --git a/app/assets/javascripts/discourse/app/components/category-name-fields.hbs b/app/assets/javascripts/discourse/app/components/category-name-fields.hbs deleted file mode 100644 index 82784f133a1..00000000000 --- a/app/assets/javascripts/discourse/app/components/category-name-fields.hbs +++ /dev/null @@ -1,26 +0,0 @@ - -
- {{#unless this.category.isUncategorizedCategory}} -
- - -
- {{/unless}} -
- - -
-
-
\ No newline at end of file diff --git a/app/assets/javascripts/discourse/app/components/category-name-fields.js b/app/assets/javascripts/discourse/app/components/category-name-fields.js deleted file mode 100644 index edc8b634fd0..00000000000 --- a/app/assets/javascripts/discourse/app/components/category-name-fields.js +++ /dev/null @@ -1,3 +0,0 @@ -import Component from "@ember/component"; - -export default class CategoryNameFields extends Component {} diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.js index 73014fa9759..aae2c6c9745 100644 --- a/app/assets/javascripts/discourse/app/models/category.js +++ b/app/assets/javascripts/discourse/app/models/category.js @@ -1,3 +1,4 @@ +import { tracked } from "@glimmer/tracking"; import { warn } from "@ember/debug"; import { computed, get } from "@ember/object"; import { service } from "@ember/service"; @@ -10,7 +11,7 @@ import Site from "discourse/models/site"; import Topic from "discourse/models/topic"; import { getOwnerWithFallback } from "discourse-common/lib/get-owner"; import getURL from "discourse-common/lib/get-url"; -import discourseComputed from "discourse-common/utils/decorators"; +import discourseComputed, { settable } from "discourse-common/utils/decorators"; import { MultiCache } from "discourse-common/utils/multi-cache"; const STAFF_GROUP_NAME = "staff"; @@ -451,6 +452,9 @@ export default class Category extends RestModel { @service currentUser; + @settable @tracked name; + @settable @tracked slug; + permissions = null; init() {