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, translateResults,
updateRecentSearches, updateRecentSearches,
} from "discourse/lib/search"; } from "discourse/lib/search";
import { applyBehaviorTransformer } from "discourse/lib/transformer";
import userSearch from "discourse/lib/user-search"; import userSearch from "discourse/lib/user-search";
import { escapeExpression } from "discourse/lib/utilities"; import { escapeExpression } from "discourse/lib/utilities";
import { scrollTop } from "discourse/mixins/scroll-top"; import { scrollTop } from "discourse/mixins/scroll-top";
@ -565,17 +566,24 @@ export default class FullPageSearchController extends Controller {
this._search(); this._search();
} }
@action get canLoadMore() {
loadMore() { return (
let page = this.page;
if (
this.get("model.grouped_search_result.more_full_page_results") && this.get("model.grouped_search_result.more_full_page_results") &&
!this.loading && !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.incrementProperty("page");
this._search(); this._search();
} });
} }
@action @action

View File

@ -1,6 +1,7 @@
export const BEHAVIOR_TRANSFORMERS = Object.freeze([ export const BEHAVIOR_TRANSFORMERS = Object.freeze([
// use only lowercase names // use only lowercase names
"discovery-topic-list-load-more", "discovery-topic-list-load-more",
"full-page-search-load-more",
]); ]);
export const VALUE_TRANSFORMERS = Object.freeze([ export const VALUE_TRANSFORMERS = Object.freeze([

View File

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