Add Algolia filtering (#72)

* Add tabs for chronological filtering

* Add Algolia filtering
This commit is contained in:
Ben Halpern 2018-03-13 12:14:27 -04:00 committed by GitHub
parent e052e63d34
commit 6a16fee77a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 10 deletions

View file

@ -105,12 +105,22 @@ function algoliaPaginate(tag){
if (tag && tag.length > 0) {
filters.push(tag);
}
articlesIndex.search("*", {
var searchObj = {
hitsPerPage: 15,
page: nextPage,
attributesToHighlight: [],
tagFilters: filters,
})
}
var homeEl = document.getElementById("index-container");
if (homeEl.dataset.feed === "base-feed") {
} else if (homeEl.dataset.feed === "latest") {
} else {
searchObj["filters"] = ('published_at_int > ' + homeEl.dataset.articlesSince);
}
articlesIndex.search("*", searchObj)
.then(function searchDone(content) {
nextPage += 1;
insertArticles(content.hits);

View file

@ -23,21 +23,24 @@ function algoliaFollowedArticles(){
var client = algoliasearch('<%= ENV["ALGOLIASEARCH_APPLICATION_ID"] %>', '<%= ENV["ALGOLIASEARCH_SEARCH_ONLY_KEY"] %>');
var index = client.initIndex('ordered_articles_<%= Rails.env %>');
var searchObj = {
hitsPerPage: 20,
page: "0",
attributesToHighlight: [],
tagFilters: [user.followed_tag_names.concat(followedUsersArray)],
}
var homeEl = document.getElementById("index-container");
if (homeEl.dataset.feed === "base-feed") {
articlesIndex = client.initIndex('ordered_articles_<%= Rails.env %>');
} else if (homeEl.dataset.feed === "latest") {
articlesIndex = client.initIndex('ordered_articles_by_published_at_<%= Rails.env %>');
} else {
searchObj["filters"] = ('published_at_int > ' + homeEl.dataset.articlesSince);
articlesIndex = client.initIndex('ordered_articles_by_positive_reactions_count_<%= Rails.env %>');
}
index.search("*", {
hitsPerPage: 20,
page: "0",
attributesToHighlight: [],
tagFilters: [user.followed_tag_names.concat(followedUsersArray)],
})
index.search("*", searchObj)
.then(function searchDone(content) {
var insertPlace = document.getElementById("article-index-hidden-div");
if (insertPlace) {

View file

@ -114,6 +114,9 @@ class Article < ApplicationRecord
attributes :title, :path, :class_name, :comments_count,
:tag_list, :positive_reactions_count, :id, :hotness_score,
:readable_publish_date
attribute :published_at_int do
published_at.to_i
end
attribute :user do
{ username: user.username,
name: user.name,

View file

@ -194,7 +194,9 @@
<div class="home <%= "sub-home" if (@user || @tag || @query)%>" id="index-container"
data-params="<%= params.to_json(only: [:tag,:username,:q]) %>" data-which="<%=@list_of%>"
data-algolia-tag="<%= @user ? "#{@user.class.name.downcase}_#{@user.id}" : nil || @tag %>"
data-feed="<%= params[:timeframe] || "base-feed"%>">
data-feed="<%= params[:timeframe] || "base-feed"%>"
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime.to_i %>"
>
<%= render "articles/sidebar" %>
<div class="articles-list" id="articles-list">
<% if @home_page || @tag %>