Working tag filtering of topics

This commit is contained in:
Justin DiRose 2019-07-26 10:50:22 -05:00
parent 9eb97cbb55
commit 2c9b904e15
5 changed files with 99 additions and 20 deletions

View File

@ -4,21 +4,26 @@ module KnowledgeExplorer
before_action :init_guardian
def index
category_topic_lists = []
tag_topic_lists = []
knowledge_explorer_categories.each do |c|
if topic_list = TopicQuery.new(current_user, category: c.id).list_latest
category_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
end
filters = {
tags: params[:tags],
category: params[:category]
}
if filters[:category]
category_topic_lists = get_topics_from_categories(category_by_filter(filters[:category]))
else
category_topic_lists = get_topics_from_categories(knowledge_explorer_categories)
end
knowledge_explorer_tags.each do |t|
if topic_list = TopicQuery.new(current_user, tags: t.name).list_latest
tag_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
end
if filters[:tags]
tag_topic_lists = get_topics_from_tags(tags_by_filter(filters[:tags]))
else
tag_topic_lists = get_topics_from_tags(knowledge_explorer_tags)
end
# Deduplicate results
topics = []
category_topic_lists.each do |list|
@ -43,6 +48,30 @@ module KnowledgeExplorer
render json: topics
end
def get_topics_from_categories(categories)
category_topic_lists = []
categories.each do |c|
if topic_list = TopicQuery.new(current_user, category: c.id).list_latest
category_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
end
end
category_topic_lists
end
def get_topics_from_tags(tags)
tag_topic_lists = []
tags.each do |t|
if topic_list = TopicQuery.new(current_user, tags: t.name).list_latest
tag_topic_lists << TopicListSerializer.new(topic_list, scope: @guardian).as_json
end
end
tag_topic_lists
end
def count_tags(topics)
tags = []
@ -79,5 +108,18 @@ module KnowledgeExplorer
Tag.where('name IN (?)', selected_tags)
end
def category_by_filter(category_filter)
selected_category = category_filter
category = Category.where('slug IN (?)', selected_category)
category.select { |c| @guardian.can_see_category?(c) }
end
def tags_by_filter(tags)
selected_tags = tags.split(' ')
Tag.where('name IN (?)', selected_tags)
end
end
end

View File

@ -13,6 +13,5 @@ export default Ember.Component.extend({
}
this.set("filterTags", filter);
this.send("refreshRoute");
}
});

View File

@ -1,15 +1,44 @@
import {
default as computed,
observes,
on
} from "ember-addons/ember-computed-decorators";
import { observes } from "ember-addons/ember-computed-decorators";
function arrayContainsArray(superset, subset) {
if (0 === subset.length) {
return false;
}
return subset.every(v => superset.indexOf(v) >= 0);
}
export default Ember.Controller.extend({
application: Ember.inject.controller(),
queryParams: {
filterCategory: 'category',
filterTags: 'tags'
filterCategory: "category",
filterTags: "tags"
},
filtered: false,
filterTags: null,
filterCategory: null,
filteredTopics: null,
@observes("filterTags")
filterByTags() {
debugger;
const filterTags = this.get("filterTags");
if (filterTags != null) {
const filter = filterTags.split(" ");
const topics = this.get("topics");
const tags = this.get("tags");
if (filter != "") {
const filteredTopics = topics.filter(topic =>
arrayContainsArray(topic.tags, filter)
);
this.set("filteredTopics", filteredTopics);
this.set("filtered", true);
} else {
this.set("filteredTopics", null);
this.set("filterTags", null);
this.set("filtered", false);
}
}
}
});

View File

@ -10,5 +10,6 @@ export default Ember.Route.extend({
tags: model.tags,
topics: model.topics
});
}
},
});

View File

@ -4,6 +4,14 @@
tags=tags
filterTags=filterTags
searchResults=searchResults}}
{{#if filtered}}
{{#if filteredTopics}}
{{knowledge-explorer-topic-list topics=filteredTopics}}
{{else}}
<div class="no-result">No results match the current filter</div>
{{/if}}
{{else}}
{{knowledge-explorer-topic-list topics=topics}}
{{/if}}
</div>
</div>