FIX: Results count not always properly shown (#21)

There was a weird case where when filtering topics by solved status did not properly show the count of filtered topics. I took this opportunity to lightly refactor the backend. There was a duplicate size operation when we searched and when calculating pagination. I've eliminated the second size call and refactored the front end to be a little bit simpler while solving the bug.
This commit is contained in:
Justin DiRose 2020-12-01 13:51:42 -06:00 committed by GitHub
parent 0e610cf504
commit 63a8181fa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 28 deletions

View File

@ -1,5 +1,7 @@
import Controller from "@ember/controller"; import Controller from "@ember/controller";
import { inject } from "@ember/controller";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import { alias, readOnly, equal } from "@ember/object/computed";
import { on } from "discourse-common/utils/decorators"; import { on } from "discourse-common/utils/decorators";
import KnowledgeExplorer from "discourse/plugins/discourse-knowledge-explorer/discourse/models/knowledge-explorer"; import KnowledgeExplorer from "discourse/plugins/discourse-knowledge-explorer/discourse/models/knowledge-explorer";
import { getOwner } from "@ember/application"; import { getOwner } from "@ember/application";
@ -14,14 +16,14 @@ export default Controller.extend({
searchTerm: "search", searchTerm: "search",
selectedTopic: "topic", selectedTopic: "topic",
}, },
application: Ember.inject.controller(), application: inject(),
isLoading: false, isLoading: false,
isLoadingMore: false, isLoadingMore: false,
loadMoreUrl: Ember.computed.alias("model.topics.load_more_url"), loadMoreUrl: alias("model.topics.load_more_url"),
isTopicLoading: false, isTopicLoading: false,
categories: Ember.computed.readOnly("model.categories"), categories: readOnly("model.categories"),
topics: Ember.computed.alias("model.topics.topic_list.topics"), topics: alias("model.topics.topic_list.topics"),
tags: Ember.computed.readOnly("model.tags"), tags: readOnly("model.tags"),
filterTags: null, filterTags: null,
filterCategories: null, filterCategories: null,
filterSolved: false, filterSolved: false,
@ -31,6 +33,7 @@ export default Controller.extend({
expandedFilters: false, expandedFilters: false,
ascending: null, ascending: null,
orderColumn: null, orderColumn: null,
topicCount: alias("model.topic_count"),
@on("init") @on("init")
_setupFilters() { _setupFilters() {
@ -42,7 +45,7 @@ export default Controller.extend({
@discourseComputed("topics", "isSearching", "filterSolved") @discourseComputed("topics", "isSearching", "filterSolved")
emptyTopics(topics, isSearching, filterSolved) { emptyTopics(topics, isSearching, filterSolved) {
const filtered = isSearching || filterSolved; const filtered = isSearching || filterSolved;
return topics.length === 0 && !filtered; return this.topicCount === 0 && !filtered;
}, },
@discourseComputed("loadMoreUrl") @discourseComputed("loadMoreUrl")
@ -60,24 +63,7 @@ export default Controller.extend({
return isSearching || filterSolved; return isSearching || filterSolved;
}, },
@discourseComputed("isSearching", "model") emptyResults: equal("topicCount", 0),
searchCount(isSearching, model) {
if (isSearching) {
return model.search_count;
}
},
emptySearchResults: Ember.computed.equal("searchCount", 0),
@discourseComputed("topics")
emptyFilteredResults(topics) {
return topics.length === 0;
},
@discourseComputed("emptySearchResults", "emptyFilteredResults")
emptyResults(emptySearch, emptyFiltered) {
return emptySearch || emptyFiltered;
},
@discourseComputed @discourseComputed
canFilterSolved() { canFilterSolved() {
@ -134,6 +120,7 @@ export default Controller.extend({
selectedTopic: null, selectedTopic: null,
}); });
}, },
performSearch(term) { performSearch(term) {
if (term === "") { if (term === "") {
this.set("searchTerm", null); this.set("searchTerm", null);

View File

@ -1,7 +1,8 @@
import Controller from "@ember/controller"; import Controller from "@ember/controller";
import { inject } from "@ember/controller";
export default Controller.extend({ export default Controller.extend({
indexController: Ember.inject.controller("knowledgeExplorer.index"), indexController: inject("knowledgeExplorer.index"),
actions: { actions: {
updateSelectedCategories(category) { updateSelectedCategories(category) {
this.indexController.send("updateSelectedCategories", category); this.indexController.send("updateSelectedCategories", category);

View File

@ -63,7 +63,7 @@
{{else}} {{else}}
<div class="result-count"> <div class="result-count">
{{i18n 'knowledge_explorer.search.results' {{i18n 'knowledge_explorer.search.results'
count=searchCount count=topicCount
}} }}
</div> </div>
{{/if}} {{/if}}

View File

@ -74,7 +74,6 @@ module KnowledgeExplorer
pd.search_data @@ #{escaped_ts_query} pd.search_data @@ #{escaped_ts_query}
) )
SQL SQL
search_count = results.size
end end
if @filters[:order] == "title" if @filters[:order] == "title"
@ -126,7 +125,7 @@ module KnowledgeExplorer
topic_list['load_more_url'] = nil topic_list['load_more_url'] = nil
end end
{ tags: tags, categories: categories, topics: topic_list, search_count: search_count } { tags: tags, categories: categories, topics: topic_list, topic_count: results_length }
end end
def create_tags_object(tags) def create_tags_object(tags)

View File

@ -29,6 +29,15 @@ describe KnowledgeExplorer::KnowledgeExplorerController do
expect(tags.size).to eq(1) expect(tags.size).to eq(1)
expect(topics.size).to eq(2) expect(topics.size).to eq(2)
end end
it 'should return a topic count' do
get '/docs.json'
json = response.parsed_body
topic_count = json['topic_count']
expect(topic_count).to eq(2)
end
end end
context 'when some knowledge explorer topics are private' do context 'when some knowledge explorer topics are private' do