A platform for community discussion. Free, open, simple.
DEV: Add `can-create-topic-button` value transformer to `canCreateTopic` in navigation.gjs (#40587)
Gabriel Grubbac61d9f0262c0
diff --git a/frontend/discourse/app/components/discovery/navigation.gjs b/frontend/discourse/app/components/discovery/navigation.gjsindex 3d4e8ee5adf..8079b739af9 100644--- a/frontend/discourse/app/components/discovery/navigation.gjs+++ b/frontend/discourse/app/components/discovery/navigation.gjs@@ -14,6 +14,7 @@ import categoryColorVariable from "discourse/helpers/category-color-variable";import lazyHash from "discourse/helpers/lazy-hash";import { calculateFilterMode } from "discourse/lib/filter-mode";import { TRACKED_QUERY_PARAM_VALUE } from "discourse/lib/topic-list-tracked-filter";+import { applyValueTransformer } from "discourse/lib/transformer";import DiscourseURL from "discourse/lib/url";import Category from "discourse/models/category";import dCategoryBadge from "discourse/ui-kit/helpers/d-category-badge";@@ -39,18 +40,21 @@ export default class DiscoveryNavigation extends Component {}get canCreateTopic() {- if (!this.currentUser?.can_create_topic) {- return false;- }+ let value = this.currentUser?.can_create_topic ?? false;if (+ value &&this.siteSettings.hide_disabled_create_topic_button &&this.args.createTopicDisabled) {- return false;+ value = false;}- return true;+ return applyValueTransformer("can-create-topic-button", value, {+ category: this.args.category,+ tag: this.args.tag,+ createTopicDisabled: this.args.createTopicDisabled,+ });}get bodyClass() {diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.jsindex a455ab53612..955f0ed4cdf 100644--- a/frontend/discourse/app/lib/registry/transformers.js+++ b/frontend/discourse/app/lib/registry/transformers.js@@ -38,6 +38,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["admin-reports-show-query-params","admin-theme-card-show-full-controls","bulk-select-in-nav-controls",+ "can-create-topic-button","category-available-views","category-default-colors","category-description-text",diff --git a/frontend/discourse/tests/acceptance/transformers/can-create-topic-button-test.js b/frontend/discourse/tests/acceptance/transformers/can-create-topic-button-test.jsnew file mode 100644index 00000000000..28ff16d9ac2--- /dev/null+++ b/frontend/discourse/tests/acceptance/transformers/can-create-topic-button-test.js@@ -0,0 +1,67 @@+import { visit } from "@ember/test-helpers";+import { test } from "qunit";+import { withPluginApi } from "discourse/lib/plugin-api";+import DiscoveryFixtures from "discourse/tests/fixtures/discovery-fixtures";+import { acceptance } from "discourse/tests/helpers/qunit-helpers";++acceptance("can-create-topic-button transformer", function (needs) {+ needs.user();+ needs.pretender((server, helper) => {+ server.get("/c/writable-category/7/l/latest.json", () => {+ return helper.response(+ DiscoveryFixtures["/latest_can_create_topic.json"]+ );+ });+ });+ needs.site({+ categories: [+ {+ id: 7,+ name: "writable category",+ slug: "writable-category",+ permission: 1,+ },+ ],+ });++ test("renders the button by default", async function (assert) {+ await visit("/latest");++ assert.dom("#create-topic").exists();+ });++ test("hides the button when the transformer returns false", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer("can-create-topic-button", () => false);+ });++ await visit("/latest");++ assert.dom("#create-topic").doesNotExist();+ });++ test("the transformer receives the category from the route as context", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer(+ "can-create-topic-button",+ ({ value, context }) => {+ if (!context.category && !context.tag) {+ return false;+ }++ return value;+ }+ );+ });++ await visit("/latest");+ assert+ .dom("#create-topic")+ .doesNotExist("hidden when there is no category context");++ await visit("/c/writable-category");+ assert+ .dom("#create-topic")+ .exists("visible when there is a category context");+ });+});