FIX: Reset search controller state (#19402)

Fixes an issue on mobile where navigating away from search and returning
results in confusing UI where there are no results but headings says "N
results found".
This commit is contained in:
Penar Musaraj 2022-12-13 09:50:44 -05:00 committed by GitHub
parent 337a033f3b
commit d2efc84cba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 9 deletions

View File

@ -62,6 +62,7 @@ export default Controller.extend({
page: 1,
resultCount: null,
searchTypes: null,
selected: [],
init() {
this._super(...arguments);
@ -76,7 +77,6 @@ export default Controller.extend({
},
{ name: I18n.t("search.type.users"), id: SEARCH_TYPE_USERS },
]);
this.selected = [];
},
@discourseComputed("resultCount")
@ -298,6 +298,7 @@ export default Controller.extend({
if (args.page === 1) {
this.set("bulkSelectEnabled", false);
this.selected.clear();
this.set("searching", true);
scrollTop();
@ -392,6 +393,22 @@ export default Controller.extend({
}
},
_afterTransition() {
this._showFooter();
if (Object.keys(this.model).length === 0) {
this.reset();
}
},
reset() {
this.setProperties({
searching: false,
page: 1,
resultCount: null,
selected: [],
});
},
@action
createTopic(searchTerm, event) {
event?.preventDefault();

View File

@ -40,7 +40,6 @@ export default DiscourseRoute.extend({
}
const searchKey = getSearchKey(args);
if (cached && cached.data.searchKey === searchKey) {
// extend expiry
setTransient("lastSearch", { searchKey, model: cached.data.model }, 5);
@ -54,12 +53,7 @@ export default DiscourseRoute.extend({
return null;
}
}).then(async (results) => {
const grouped_search_result = results
? results.grouped_search_result
: {};
const model = (results && (await translateResults(results))) || {
grouped_search_result,
};
const model = (results && (await translateResults(results))) || {};
setTransient("lastSearch", { searchKey, model }, 5);
return model;
});
@ -67,7 +61,7 @@ export default DiscourseRoute.extend({
@action
didTransition() {
this.controllerFor("full-page-search")._showFooter();
this.controllerFor("full-page-search")._afterTransition();
return true;
},
});

View File

@ -0,0 +1,40 @@
# frozen_string_literal: true
module PageObjects
module Pages
class Search < PageObjects::Pages::Base
def type_in_search(input)
find("input.full-page-search").send_keys(input)
self
end
def heading_text
find("h1.search-page-heading").text
end
def click_search_button
find(".search-cta").click
end
def click_home_logo
find(".d-header .logo-mobile").click
end
def click_search_icon
find(".d-header #search-button").click
end
def has_search_result?
within(".search-results") do
page.has_selector?(".fps-result", visible: true)
end
end
def is_search_page
has_css?("body.search-page")
end
end
end
end

View File

@ -0,0 +1,41 @@
# frozen_string_literal: true
describe 'Search', type: :system, js: true do
let(:search_page) { PageObjects::Pages::Search.new }
fab!(:topic) { Fabricate(:topic) }
fab!(:post) { Fabricate(:post, topic: topic, raw: "This is a test post in a test topic") }
describe "when using full page search on mobile" do
before do
SearchIndexer.enable
SearchIndexer.index(topic, force: true)
end
after do
SearchIndexer.disable
end
it "works and clears search page state", mobile: true do
visit("/search")
search_page.type_in_search("test")
search_page.click_search_button
expect(search_page).to have_search_result
expect(search_page.heading_text).not_to eq("Search")
search_page.click_home_logo
expect(search_page.is_search_page).to be_falsey
page.go_back
# ensure results are still there when using browser's history
expect(search_page).to have_search_result
search_page.click_home_logo
search_page.click_search_icon
expect(search_page).not_to have_search_result
expect(search_page.heading_text).to eq("Search")
end
end
end