Merge pull request #25 from olinpin/feat/sort

Feat/sort
This commit is contained in:
2022-11-03 14:33:33 +01:00
committed by GitHub
6 changed files with 60 additions and 28 deletions

View File

Binary file not shown.

View File

@@ -97,16 +97,16 @@
</div>
<div class="rightcol column">
<div class="searchcontainer column">
<input class="searchbar" type="text" placeholder="Search Cluccs">
<input class="blockedwords" type="text" placeholder="Blocked words (separated by comma)">
<input class="searchbar" onchange="updateSearched(this.value)" type="text" placeholder="Search Cluccs">
<input class="blockedwords" onchange="updateBlocked(this.value)" type="text" placeholder="Blocked words (separated by comma)">
<div class="button sort-btn">
<span class="sortby">Sort By <span class="material-symbols-outlined">arrow_drop_down</span></span>
<div class="sort-dropdown">
<ul class="sort-dropdown-list">
<li>Time</li>
<li>Likes</li>
<li>Retweets</li>
<li>Replies</li>
<li onclick="updateSort('time')">Time</li>
<li onclick="updateSort('likes')">Likes</li>
<li onclick="updateSort('retweets')">Retweets</li>
<li onclick="updateSort('replies')">Replies</li>
</ul>
</div>
</div>

View File

@@ -94,26 +94,41 @@ function searchForWords(searchWords, tweets){
return filteredTweets.reverse()
}
function sortTweets(tweets, likesDes=null, retweetsDes=null, repliesDes=null, dateDes=null){
if(likesDes && likesDes!=null){
tweets.sort(function(a,b){return b.favorite_count-a.favorite_count})
} else if (!likesDes && likesDes!=null){
tweets.sort(function(a,b){return a.favorite_count-b.favorite_count})
function sortTweets(tweets, sort){
if(typeof sort.sortby == 'undefined'){
return tweets
}
if(retweetsDes && retweetsDes!=null){
tweets.sort(function(a,b){return b.retweet_count-a.retweet_count})
} else if (!retweetsDes && retweetsDes!=null){
tweets.sort(function(a,b){return a.retweet_count-b.retweet_count})
if(typeof sort.order == 'undefined'){
sort.order = 'asc'
}
if(repliesDes && repliesDes!=null){
tweets.sort(function(a,b){return b.reply_count-a.reply_count})
} else if (!repliesDes && repliesDes!=null){
tweets.sort(function(a,b){return a.reply_count-b.reply_count})
if(sort.sortby == "likes"){
if(sort.order == 'asc'){
tweets.sort(function(a,b){return b.favorite_count-a.favorite_count})
} else{
tweets.sort(function(a,b){return a.favorite_count-b.favorite_count})
}
}
if(dateDes && dateDes!=null){
tweets.sort(function(a,b){return Date(b.created_at)-Date(a.reply_count)})
} else if (!dateDes && dateDes!=null){
tweets.sort(function(a,b){return Date(a.created_at)-Date(b.reply_count)})
if(sort.sortby == "retweets"){
if(sort.order == 'asc'){
tweets.sort(function(a,b){return b.retweet_count-a.retweet_count})
} else{
tweets.sort(function(a,b){return a.retweet_count-b.retweet_count})
}
}
if(sort.sortby == "replies"){
if(sort.order == 'asc'){
tweets.sort(function(a,b){return b.reply_count-a.reply_count})
} else {
tweets.sort(function(a,b){return a.reply_count-b.reply_count})
}
}
if (sort.sortby == "date"){
if(sort.order == 'asc'){
tweets.sort(function(a,b){return Date(b.created_at)-Date(a.reply_count)})
} else{
tweets.sort(function(a,b){return Date(a.created_at)-Date(b.reply_count)})
}
}
return tweets
}

View File

@@ -80,7 +80,7 @@ $(window).on("filter", function() {
tweets = JSON.parse(tweets);
console.log(tweets.length)
tweets = filterTweets(tweets, filters.blockedWords, filters.searchedWords.concat(filters.sports), filters.sorted, filters.order);
tweets = sortTweets(tweets);
tweets = sortTweets(tweets, [filters.sorted, filters.order]);
var displayTweets = tweets.reverse().slice(0, 20).reverse();
for (i = 0; i < 19; i++){
console.log("FDS")
@@ -93,11 +93,28 @@ $(window).on("filter", function() {
$(".order-btn").on("click", function() {
if($(this).html() == "arrow_downward") { // Descending to ascending
$(this).html("arrow_upward")
// trigger an event called ascending
$(window).trigger("ascending")
filters.order = 'asc'
} else { // Ascending to descending
$(this).html("arrow_downward")
// trigger an event called descending
$(window).trigger("descending")
filters.order = 'desc'
}
$(window).trigger("filter")
})
function updateSearched(searched){
searched = searched.split(",");
filters.searchedWords = searched;
$(window).trigger("filter");
}
function updateBlocked(blocked){
blocked = blocked.split(",");
filters.blockedWords = blocked;
$(window).trigger("filter");
}
function updateSort(sort){
sortarray = []
filters.sorted = sort;
$(window).trigger("filter");
}

View File

Binary file not shown.

View File

Binary file not shown.