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

This commit is contained in:
Mark VanLandingham 2025-01-08 15:44:58 -06:00 committed by GitHub
parent 40f7941f2b
commit a29b964329
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 7 deletions

View File

@ -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

View File

@ -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([

View File

@ -297,6 +297,10 @@
{{/if}}
</ConditionalLoadingSpinner>
{{/if}}
<PluginOutlet
@name="full-page-search-below-results"
@outletArgs={{hash canLoadMore=this.canLoadMore}}
/>
</LoadMore>
</div>
{{/if}}

View File

@ -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);
});
});
});