As a user
When I visit the /posts page
I see a list of posts sorted by their "posted" or "created" date, and this data is displayed for each post
I can sort the posts either by created date or by number of likes
Implementation Details
Update the /posts index template to display the post.created_at date for each post
Ensure that the list of posts that are displayed on the page are sorted by created_at when the page loads. I'd suggest updating the core function that returns a list of post to return the list sorted by date using Ecto.
Then, you'll want to add buttons/links to the page that allow the use to change between sorting posts by date and by number of likes. Review this blog post for some ideas on how to get started https://elixirschool.com/blog/sorting-a-table-with-live-view-live-links/. This post might be a little outdated since I wrote it in 2019 (can you even remember back that far?? I cannot!) but it should give you a good idea of where to get started and where to go in the LiveView docs. The post talks about using live_link/2 but you'll want to use live_patch instead, see docs here.
Use live_patch to create a link to the current live view with some query params like /posts?sort=likes and /posts?sort=date
Implement a handle_params function that knows how to handle each query parameter scenario. If sort is set to date, then update the live view's socket to have posts: set to the list of posts sorted by date. If the sort param is set to likes, then set the lists of :posts in socket assigns to a list sorted by number of likes.
User Story
As a user When I visit the
/posts
page I see a list of posts sorted by their "posted" or "created" date, and this data is displayed for each post I can sort the posts either by created date or by number of likesImplementation Details
/posts
index template to display thepost.created_at
date for each postlive_link/2
but you'll want to uselive_patch
instead, see docs here.live_patch
to create a link to the current live view with some query params like/posts?sort=likes
and/posts?sort=date
handle_params
function that knows how to handle each query parameter scenario. Ifsort
is set todate
, then update the live view's socket to haveposts:
set to the list of posts sorted by date. If thesort
param is set tolikes
, then set the lists of:posts
in socket assigns to a list sorted by number of likes.