Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add behavior transformer for full page search loadMore (#30648)

Mark VanLandinghama29b9643295b
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}}
</ConditionalLoadingSpinner>
{{/if}}
+ <PluginOutlet
+ @name="full-page-search-below-results"
+ @outletArgs={{hash canLoadMore=this.canLoadMore}}
+ />
</LoadMore>
</div>
{{/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);
+ });
+ });
+});