From a29b9643295b949999650abfa46e0641963bf035 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Wed, 8 Jan 2025 15:44:58 -0600 Subject: [PATCH] DEV: Add behavior transformer for full page search loadMore (#30648) --- .../app/controllers/full-page-search.js | 22 ++++++---- .../discourse/app/lib/transformer/registry.js | 1 + .../app/templates/full-page-search.hbs | 4 ++ .../unit/controllers/full-page-search-test.js | 41 +++++++++++++++++++ 4 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 app/assets/javascripts/discourse/tests/unit/controllers/full-page-search-test.js diff --git a/app/assets/javascripts/discourse/app/controllers/full-page-search.js b/app/assets/javascripts/discourse/app/controllers/full-page-search.js index 40f104c6a4b..764746dedc9 100644 --- a/app/assets/javascripts/discourse/app/controllers/full-page-search.js +++ b/app/assets/javascripts/discourse/app/controllers/full-page-search.js @@ -18,6 +18,7 @@ import { translateResults, updateRecentSearches, } from "discourse/lib/search"; +import { applyBehaviorTransformer } from "discourse/lib/transformer"; import userSearch from "discourse/lib/user-search"; import { escapeExpression } from "discourse/lib/utilities"; import { scrollTop } from "discourse/mixins/scroll-top"; @@ -565,17 +566,24 @@ export default class FullPageSearchController extends Controller { this._search(); } - @action - loadMore() { - let page = this.page; - if ( + get canLoadMore() { + return ( this.get("model.grouped_search_result.more_full_page_results") && !this.loading && - page < PAGE_LIMIT - ) { + this.page < PAGE_LIMIT + ); + } + + @action + loadMore() { + if (!this.canLoadMore) { + return; + } + + applyBehaviorTransformer("full-page-search-load-more", () => { this.incrementProperty("page"); this._search(); - } + }); } @action diff --git a/app/assets/javascripts/discourse/app/lib/transformer/registry.js b/app/assets/javascripts/discourse/app/lib/transformer/registry.js index d0f42558f44..219b23db263 100644 --- a/app/assets/javascripts/discourse/app/lib/transformer/registry.js +++ b/app/assets/javascripts/discourse/app/lib/transformer/registry.js @@ -1,6 +1,7 @@ export const BEHAVIOR_TRANSFORMERS = Object.freeze([ // use only lowercase names "discovery-topic-list-load-more", + "full-page-search-load-more", ]); export const VALUE_TRANSFORMERS = Object.freeze([ diff --git a/app/assets/javascripts/discourse/app/templates/full-page-search.hbs b/app/assets/javascripts/discourse/app/templates/full-page-search.hbs index 15d3527e9c8..29e9591abde 100644 --- a/app/assets/javascripts/discourse/app/templates/full-page-search.hbs +++ b/app/assets/javascripts/discourse/app/templates/full-page-search.hbs @@ -297,6 +297,10 @@ {{/if}} {{/if}} + {{/if}} diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/full-page-search-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/full-page-search-test.js new file mode 100644 index 00000000000..f10e3c15293 --- /dev/null +++ b/app/assets/javascripts/discourse/tests/unit/controllers/full-page-search-test.js @@ -0,0 +1,41 @@ +import { setupTest } from "ember-qunit"; +import { module, test } from "qunit"; +import { withPluginApi } from "discourse/lib/plugin-api"; + +module("Unit | Controller | full-page-search", function (hooks) { + setupTest(hooks); + + test("full-page-search-load-more behavior transformer", function (assert) { + withPluginApi("2.0.0", (api) => { + const controller = this.owner.lookup("controller:full-page-search"); + controller.model = { + grouped_search_result: { more_full_page_results: true }, + }; + + api.registerBehaviorTransformer( + "full-page-search-load-more", + ({ next }) => { + if (controller.blockWithTransformer) { + return; + } + next(); + } + ); + + assert.strictEqual(controller.page, 1); + + controller.loadMore(); + assert.strictEqual(controller.page, 2); + + // Block loading by setting variable on controller which transformer sees + controller.blockWithTransformer = true; + controller.loadMore(); + assert.strictEqual(controller.page, 2); + + // Now unblock and ensure next() functions + controller.blockWithTransformer = false; + controller.loadMore(); + assert.strictEqual(controller.page, 3); + }); + }); +});