feat: add filter and sort functionality to post list

This commit is contained in:
Kerby Martino authored and Kerby Martino (aider) committed 2025-05-05 12:03:24 +08:00
1 parent 49cf8db70a
commit ae9d23b507
4 files changed
+76 -131

No files matched your search

+55 -131
View File
@@ -3,47 +3,61 @@
<p><strong>Quick Tips:</strong> Keep it brief, keep it real. Share your desk moments, finds, or frustrations. Upvote what resonates!</p>
</div>
{% assign filter_type = page.filter_type | default: 'all' %}
{% assign sort_by = page.sort_by | default: 'new' %}
<!-- Filter and Sort Controls -->
<div class="mb-6 pb-4 bg-gray-100 rounded-md">
<div class="flex flex-col sm:flex-row justify-between space-y-4 sm:space-y-0">
<!-- Filter by Type -->
<div class="filter-controls flex flex-col space-y-3 sm:flex-row sm:space-y-0 sm:space-x-2 sm:items-center">
<button data-filter-type="all" class="filter-btn active w-full sm:w-auto flex items-center justify-center space-x-2 p-3 border-2 border-gray-300 rounded-md cursor-pointer hover:border-primary transition-colors duration-150 ease-in-out" title="All">
<i class="fas fa-list fa-fw text-primary"></i> <span class="text-sm font-action font-medium text-primary">All</span>
</button>
<button data-filter-type="win" class="filter-btn w-full sm:w-auto flex items-center justify-center space-x-2 p-3 border-2 border-gray-300 rounded-md cursor-pointer hover:border-primary transition-colors duration-150 ease-in-out" title="Wins">
<img src="/assets/images/icon_win.png" alt="Win" class="w-5 h-5 object-contain inline-block"> <span class="text-sm font-action font-medium text-primary">Wins</span>
</button>
<button data-filter-type="frustration" class="filter-btn w-full sm:w-auto flex items-center justify-center space-x-2 p-3 border-2 border-gray-300 rounded-md cursor-pointer hover:border-primary transition-colors duration-150 ease-in-out" title="Frustrations">
<img src="/assets/images/icon_frustrated.png" alt="Frustration" class="w-5 h-5 object-contain inline-block"> <span class="text-sm font-action font-medium text-primary">Frustrations</span>
</button>
<button data-filter-type="weird" class="filter-btn w-full sm:w-auto flex items-center justify-center space-x-2 p-3 border-2 border-gray-300 rounded-md cursor-pointer hover:border-primary transition-colors duration-150 ease-in-out" title="Weird">
<img src="/assets/images/icon_weird.png" alt="Weird" class="w-5 h-5 object-contain inline-block"> <span class="text-sm font-action font-medium text-primary">Weird</span>
</button>
<a href="/" class="filter-btn w-full sm:w-auto flex items-center justify-center space-x-2 p-3 border-2 {% if filter_type == 'all' %}border-primary bg-primary/10{% else %}border-gray-300{% endif %} rounded-md hover:border-primary transition-colors">
<i class="fas fa-list fa-fw text-primary"></i>
<span class="text-sm font-action font-medium text-primary">All</span>
</a>
<a href="/win/" class="filter-btn w-full sm:w-auto flex items-center justify-center space-x-2 p-3 border-2 {% if filter_type == 'win' %}border-primary bg-primary/10{% else %}border-gray-300{% endif %} rounded-md hover:border-primary transition-colors">
<img src="/assets/images/icon_win.png" alt="Win" class="w-5 h-5 object-contain inline-block">
<span class="text-sm font-action font-medium text-primary">Wins</span>
</a>
<a href="/frustration/" class="filter-btn w-full sm:w-auto flex items-center justify-center space-x-2 p-3 border-2 {% if filter_type == 'frustration' %}border-primary bg-primary/10{% else %}border-gray-300{% endif %} rounded-md hover:border-primary transition-colors">
<img src="/assets/images/icon_frustrated.png" alt="Frustration" class="w-5 h-5 object-contain inline-block">
<span class="text-sm font-action font-medium text-primary">Frustrations</span>
</a>
<a href="/weird/" class="filter-btn w-full sm:w-auto flex items-center justify-center space-x-2 p-3 border-2 {% if filter_type == 'weird' %}border-primary bg-primary/10{% else %}border-gray-300{% endif %} rounded-md hover:border-primary transition-colors">
<img src="/assets/images/icon_weird.png" alt="Weird" class="w-5 h-5 object-contain inline-block">
<span class="text-sm font-action font-medium text-primary">Weird</span>
</a>
</div>
<!-- Sort By Dropdown -->
<!-- Sort Dropdown (optional, static default) -->
<div class="sort-controls flex items-center space-x-2">
<label for="sort-select" class="text-sm font-medium text-gray-700">Sort by:</label>
<select id="sort-select" name="sort_by" class="sort-select text-sm font-action bg-white border border-gray-300 rounded-md px-3 py-1.5 focus:outline-none focus:ring-1 focus:ring-primary focus:border-primary cursor-pointer">
<option value="new" selected>Newest</option>
<option value="new" selected>Nearby</option>
<option value="upvotes">Most Upvotes</option>
<option value="comments">Most Comments</option>
<!-- Add other options like 'today', 'nearby' here if data/logic becomes available -->
<select id="sort-select" disabled class="cursor-not-allowed text-sm font-action bg-white border border-gray-300 rounded-md px-3 py-1.5">
<option selected>Newest</option>
</select>
</div>
</div>
</div>
<!-- Post List Container -->
<!-- Post List -->
<div id="post-list-container" class="post-list space-y-4">
{% assign sorted_posts = site.posts | sort: 'date' | reverse %}
{% for post in sorted_posts %}
<div class="post-item mb-4" data-type="{{ post.type | default: 'unknown' }}" data-date="{{ post.date | date_to_xmlschema }}" data-upvotes="{{ post.upvotes | default: 0 }}" data-comments="{{ post.comments_count | default: 0 }}">
<article class="flex flex-col md:flex-row items-start">
<div class="flex-1 text-left">
<div>
{% assign posts = site.posts %}
{% if filter_type != 'all' %}
{% assign posts = posts | where: 'type', filter_type %}
{% endif %}
{% assign posts = posts | sort: 'date' | reverse %}
{% if posts == empty %}
<div class="text-center text-gray-500 font-action py-10">
<p>No results found for this filter.</p>
</div>
{% else %}
{% for post in posts %}
<div class="post-item mb-4">
<article class="flex flex-col md:flex-row items-start">
<div class="flex-1 text-left">
<!-- Post Title -->
<div class="flex items-center space-x-2 mb-0.5">
<p class="mt-0 mb-0">
@@ -58,15 +72,15 @@
<!-- Post Metadata -->
<div class="mt-2 mb-1 text-sm flex flex-col md:flex-row items-start md:items-center md:space-x-2">
<div class="flex items-start space-x-2 mb-2 md:mb-0">
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center cursor-pointer hover:bg-link-hover">
<i class="fa-solid fa-arrow-up mr-1"></i> {{ post.upvotes }}
</span>
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center cursor-pointer hover:bg-link-hover">
<a href="/reply/"><i class="fas fa-reply mr-1"></i> REPLY</a>
</span>
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center cursor-pointer hover:bg-link-hover">
<i class="fas fa-comment mr-1"></i> {{ post.comments_count }}
</span>
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center">
<i class="fa-solid fa-arrow-up mr-1"></i> {{ post.upvotes }}
</span>
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center">
<a href="/reply/"><i class="fas fa-reply mr-1"></i> REPLY</a>
</span>
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center">
<i class="fas fa-comment mr-1"></i> {{ post.comments_count }}
</span>
</div>
<small class="text-sm font-action">
<a href="/user/" class="text-sm hover:underline">{{ post.author }}</a>
@@ -74,104 +88,14 @@
</small>
</div>
</div>
</div>
</article>
<hr class="border-border-light my-4">
</div>
{% endfor %}
</article>
<hr class="border-border-light my-4">
</div>
{% endfor %}
{% endif %}
</div>
<!-- More Link -->
<!-- More Button -->
<div class="pt-2">
<button type="submit" class="w-full md:w-auto px-6 py-3 bg-primary text-white text-sm font-medium rounded-md hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary transition duration-150 ease-in-out font-monda font-action">More</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const filterButtons = document.querySelectorAll('.filter-btn');
const sortSelect = document.getElementById('sort-select'); // Get the select dropdown
const postListContainer = document.getElementById('post-list-container');
const postItems = Array.from(postListContainer.querySelectorAll('.post-item')); // Convert NodeList to Array
let currentFilter = 'all';
let currentSort = sortSelect.value; // Default sort from selected option ('new')
function applyFiltersAndSorting() {
// 1. Filter
const filteredPosts = postItems.filter(post => {
if (currentFilter === 'all') {
return true; // Show all posts if filter is 'all'
}
return post.dataset.type === currentFilter;
});
// 2. Sort
filteredPosts.sort((a, b) => {
if (currentSort === 'new') {
// Sort by date descending (newest first)
return new Date(b.dataset.date) - new Date(a.dataset.date);
} else if (currentSort === 'upvotes') { // Changed from 'top'
// Sort by upvotes descending (highest first)
const scoreDiff = parseInt(b.dataset.upvotes) - parseInt(a.dataset.upvotes);
if (scoreDiff !== 0) {
return scoreDiff;
}
// Secondary sort by date for ties
return new Date(b.dataset.date) - new Date(a.dataset.date);
} else if (currentSort === 'comments') {
// Sort by comments descending (most first)
const commentDiff = parseInt(b.dataset.comments) - parseInt(a.dataset.comments);
if (commentDiff !== 0) {
return commentDiff;
}
// Secondary sort by date for ties
return new Date(b.dataset.date) - new Date(a.dataset.date);
}
// Add logic for other sort options like 'today', 'nearby' here if implemented
return 0; // Default no sort change
});
// 3. Update DOM
// Hide all posts initially
postItems.forEach(post => post.style.display = 'none');
// Clear container and append sorted+filtered posts
postListContainer.innerHTML = ''; // Clear current posts
filteredPosts.forEach(post => {
post.style.display = ''; // Make sure it's visible
postListContainer.appendChild(post); // Append in sorted order
});
// Update button active states
filterButtons.forEach(btn => {
const isActive = btn.dataset.filterType === currentFilter;
btn.classList.toggle('active', isActive);
// Apply styles consistent with submit.liquid active state
if (isActive) {
btn.classList.add('border-primary', 'bg-primary/10');
btn.classList.remove('border-gray-300', 'bg-transparent'); // Ensure default border is removed
} else {
btn.classList.add('border-gray-300', 'bg-transparent'); // Use bg-transparent or specific default bg
btn.classList.remove('border-primary', 'bg-primary/10');
}
});
// No active state styling needed for select dropdown itself
}
// Event Listeners for Filter Buttons
filterButtons.forEach(button => {
button.addEventListener('click', () => {
currentFilter = button.dataset.filterType;
applyFiltersAndSorting();
});
});
// Event Listener for Sort Dropdown
sortSelect.addEventListener('change', (event) => {
currentSort = event.target.value;
applyFiltersAndSorting();
});
// Initial application of filters and sorting on page load
applyFiltersAndSorting();
});
</script>
+7
View File
@@ -0,0 +1,7 @@
---
layout: default
title: Frustrations
permalink: /frustration/
filter_type: frustration
---
{% include post_list.liquid %}
+7
View File
@@ -0,0 +1,7 @@
---
layout: default
title: Weird
permalink: /weird/
filter_type: weird
---
{% include post_list.liquid %}
+7
View File
@@ -0,0 +1,7 @@
---
layout: default
title: Wins
permalink: /win/
filter_type: win
---
{% include post_list.liquid %}