A platform for community discussion. Free, open, simple.
dev: add `custom-homepage-model` transformer (#39324)
Gabriel Grubba7a49f9edfb8c
diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.jsindex 80759a31a33..a8b8332bd22 100644--- a/frontend/discourse/app/lib/registry/transformers.js+++ b/frontend/discourse/app/lib/registry/transformers.js@@ -52,6 +52,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["composer-toggles-class","create-topic-button-class","create-topic-label",+ "custom-homepage-model","flag-button-disabled-state","flag-button-dynamic-class","flag-button-render-decision",diff --git a/frontend/discourse/app/routes/discovery/custom.js b/frontend/discourse/app/routes/discovery/custom.jsnew file mode 100644index 00000000000..9dbde1a8174--- /dev/null+++ b/frontend/discourse/app/routes/discovery/custom.js@@ -0,0 +1,14 @@+import { applyValueTransformer } from "discourse/lib/transformer";+import DiscourseRoute from "discourse/routes/discourse";++export default class DiscoveryCustomRoute extends DiscourseRoute {+ queryParams = {+ q: { refreshModel: true },+ };++ model(data) {+ return applyValueTransformer("custom-homepage-model", null, {+ queryParams: data,+ });+ }+}diff --git a/frontend/discourse/app/templates/discovery/custom.gjs b/frontend/discourse/app/templates/discovery/custom.gjsindex d321aa5936d..28bddacdbe9 100644--- a/frontend/discourse/app/templates/discovery/custom.gjs+++ b/frontend/discourse/app/templates/discovery/custom.gjs@@ -1,11 +1,15 @@import BlockOutlet from "discourse/blocks/block-outlet";import PluginOutlet from "discourse/components/plugin-outlet";+import lazyHash from "discourse/helpers/lazy-hash";import { i18n } from "discourse-i18n";export default <template>- <BlockOutlet @name="homepage-blocks">+ <BlockOutlet @name="homepage-blocks" @outletArgs={{lazyHash model=@model}}><:after as |hasBlocks|>- <PluginOutlet @name="custom-homepage">+ <PluginOutlet+ @name="custom-homepage"+ @outletArgs={{lazyHash model=@model}}+ >{{#if @controller.currentUser.admin}}{{#unless hasBlocks}}<p class="alert alert-info">diff --git a/frontend/discourse/tests/unit/routes/discovery-custom-test.js b/frontend/discourse/tests/unit/routes/discovery-custom-test.jsnew file mode 100644index 00000000000..31942dbe501--- /dev/null+++ b/frontend/discourse/tests/unit/routes/discovery-custom-test.js@@ -0,0 +1,71 @@+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import { withPluginApi } from "discourse/lib/plugin-api";++module("Unit | Route | discovery-custom", function (hooks) {+ setupTest(hooks);++ test("model returns null by default when no transformer is registered", async function (assert) {+ const route = this.owner.lookup("route:discovery.custom");++ assert.strictEqual(await route.model({}), null);+ });++ test("custom-homepage-model value transformer can provide the model", async function (assert) {+ const route = this.owner.lookup("route:discovery.custom");+ const homepageModel = { latest: [{ id: 1 }], hot: [{ id: 2 }] };++ withPluginApi((api) => {+ api.registerValueTransformer(+ "custom-homepage-model",+ () => homepageModel+ );+ });++ assert.strictEqual(await route.model({}), homepageModel);+ });++ test("custom-homepage-model value transformer can be async", async function (assert) {+ const route = this.owner.lookup("route:discovery.custom");+ const homepageModel = { leaderboard: { users: [] } };++ withPluginApi((api) => {+ api.registerValueTransformer(+ "custom-homepage-model",+ async () => homepageModel+ );+ });++ assert.strictEqual(await route.model({}), homepageModel);+ });++ test("custom-homepage-model value transformer receives queryParams in context", async function (assert) {+ const route = this.owner.lookup("route:discovery.custom");+ let receivedContext;++ withPluginApi((api) => {+ api.registerValueTransformer("custom-homepage-model", ({ context }) => {+ receivedContext = context;+ return null;+ });+ });++ await route.model({ q: "search term" });++ assert.deepEqual(receivedContext.queryParams, { q: "search term" });+ });++ test("multiple value transformers compose, each receiving the previous value", async function (assert) {+ const route = this.owner.lookup("route:discovery.custom");++ withPluginApi((api) => {+ api.registerValueTransformer("custom-homepage-model", () => ({ a: 1 }));+ api.registerValueTransformer("custom-homepage-model", ({ value }) => ({+ ...value,+ b: 2,+ }));+ });++ assert.deepEqual(await route.model({}), { a: 1, b: 2 });+ });+});