feat: Implement dynamic post filtering and sorting in post list
This commit is contained in:
1 parent
1030c76259
commit
1ff985da96
1 file changed
+128
-14
+128
-14
@@ -3,34 +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>
|
||||
|
||||
<!-- Filter Container -->
|
||||
<div id="filter-container" class="hidden">
|
||||
<!-- Date Display -->
|
||||
<div id="date-display" class="mb-4 text-text-muted hidden">
|
||||
<p>Stories from <a href="#" class="text-primary hover:underline">April</a> <a href="#" class="text-primary hover:underline">24</a>, <a href="#" class="text-primary hover:underline">2025</a> (UTC)</p>
|
||||
<!-- Filter and Sort Controls -->
|
||||
<div class="mb-6 p-4 bg-gray-100 rounded-md">
|
||||
<div class="flex flex-col sm:flex-row justify-between items-center space-y-4 sm:space-y-0">
|
||||
<!-- Filter by Type -->
|
||||
<div class="filter-controls flex items-center space-x-2">
|
||||
<span class="text-sm font-medium text-gray-700 mr-2">Show:</span>
|
||||
<button data-filter-type="all" class="filter-btn active p-2 rounded-md hover:bg-gray-200" title="All">
|
||||
<i class="fas fa-list fa-fw"></i> <span class="hidden sm:inline">All</span>
|
||||
</button>
|
||||
<button data-filter-type="win" class="filter-btn p-2 rounded-md hover:bg-gray-200" title="Wins">
|
||||
<img src="/assets/images/icon_win.png" alt="Win" class="w-5 h-5 object-contain inline-block"> <span class="hidden sm:inline">Wins</span>
|
||||
</button>
|
||||
<button data-filter-type="frustration" class="filter-btn p-2 rounded-md hover:bg-gray-200" title="Frustrations">
|
||||
<img src="/assets/images/icon_frustrated.png" alt="Frustration" class="w-5 h-5 object-contain inline-block"> <span class="hidden sm:inline">Frustrations</span>
|
||||
</button>
|
||||
<button data-filter-type="weird" class="filter-btn p-2 rounded-md hover:bg-gray-200" title="Weird">
|
||||
<img src="/assets/images/icon_weird.png" alt="Weird" class="w-5 h-5 object-contain inline-block"> <span class="hidden sm:inline">Weird</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Filter Section -->
|
||||
<div id="filter-section" class="mb-4 text-text-muted hidden">
|
||||
<p id="filter-text"></p>
|
||||
<!-- Sort By -->
|
||||
<div class="sort-controls flex items-center space-x-2">
|
||||
<span class="text-sm font-medium text-gray-700 mr-2">Sort by:</span>
|
||||
<button data-sort-by="new" class="sort-btn active px-3 py-1 text-sm bg-white border border-gray-300 rounded-md hover:bg-gray-50">New</button>
|
||||
<button data-sort-by="top" class="sort-btn px-3 py-1 text-sm bg-white border border-gray-300 rounded-md hover:bg-gray-50">Top</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Post List -->
|
||||
<div class="post-list space-y-4">
|
||||
{% for post in site.posts %}
|
||||
<div class="mb-4">
|
||||
<!-- Post List Container -->
|
||||
<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 }}">
|
||||
<article class="flex flex-col md:flex-row items-start">
|
||||
<div class="flex-1 text-left">
|
||||
<div>
|
||||
<!-- Post Title -->
|
||||
<p class="mt-0 mb-0.5">
|
||||
<!-- Icon and Post Title -->
|
||||
<div class="flex items-center space-x-2 mb-0.5">
|
||||
{% assign post_type = post.type | default: 'unknown' %}
|
||||
{% if post_type == "frustration" %}
|
||||
<img src="/assets/images/icon_frustrated.png" alt="Frustration" class="w-5 h-5 object-contain flex-shrink-0" title="Frustration">
|
||||
{% elsif post_type == "weird" %}
|
||||
<img src="/assets/images/icon_weird.png" alt="Weird" class="w-5 h-5 object-contain flex-shrink-0" title="Weird">
|
||||
{% elsif post_type == "win" %}
|
||||
<img src="/assets/images/icon_win.png" alt="Win" class="w-5 h-5 object-contain flex-shrink-0" title="Win">
|
||||
{% endif %}
|
||||
<p class="mt-0 mb-0">
|
||||
<strong>
|
||||
<a href="{{ post.url }}" class="text-text-title font-bold text-md md:text-sm hover:underline">
|
||||
{{ post.title }}
|
||||
</a>
|
||||
</strong>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- 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">
|
||||
@@ -62,3 +89,90 @@
|
||||
<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 sortButtons = document.querySelectorAll('.sort-btn');
|
||||
const postListContainer = document.getElementById('post-list-container');
|
||||
const postItems = Array.from(postListContainer.querySelectorAll('.post-item')); // Convert NodeList to Array
|
||||
|
||||
let currentFilter = 'all';
|
||||
let currentSort = 'new'; // Default sort
|
||||
|
||||
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 === 'top') {
|
||||
// Sort by upvotes descending (highest first)
|
||||
// Add secondary sort by date for ties
|
||||
const scoreDiff = parseInt(b.dataset.upvotes) - parseInt(a.dataset.upvotes);
|
||||
if (scoreDiff !== 0) {
|
||||
return scoreDiff;
|
||||
}
|
||||
return new Date(b.dataset.date) - new Date(a.dataset.date); // Newest first if votes are equal
|
||||
}
|
||||
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 => {
|
||||
btn.classList.toggle('active', btn.dataset.filterType === currentFilter);
|
||||
// Add specific styling for active state if needed, e.g., background color
|
||||
if (btn.dataset.filterType === currentFilter) {
|
||||
btn.classList.add('bg-gray-300'); // Example active style
|
||||
} else {
|
||||
btn.classList.remove('bg-gray-300');
|
||||
}
|
||||
});
|
||||
sortButtons.forEach(btn => {
|
||||
btn.classList.toggle('active', btn.dataset.sortBy === currentSort);
|
||||
if (btn.dataset.sortBy === currentSort) {
|
||||
btn.classList.add('bg-blue-200', 'border-blue-400'); // Example active style
|
||||
} else {
|
||||
btn.classList.remove('bg-blue-200', 'border-blue-400');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Event Listeners for Filter Buttons
|
||||
filterButtons.forEach(button => {
|
||||
button.addEventListener('click', () => {
|
||||
currentFilter = button.dataset.filterType;
|
||||
applyFiltersAndSorting();
|
||||
});
|
||||
});
|
||||
|
||||
// Event Listeners for Sort Buttons
|
||||
sortButtons.forEach(button => {
|
||||
button.addEventListener('click', () => {
|
||||
currentSort = button.dataset.sortBy;
|
||||
applyFiltersAndSorting();
|
||||
});
|
||||
});
|
||||
|
||||
// Initial application of filters and sorting on page load
|
||||
applyFiltersAndSorting();
|
||||
});
|
||||
</script>
|
||||
Reference in new issue
Block a user