Skip to content

Instantly share code, notes, and snippets.

@Bishwas-py
Created June 28, 2024 02:09
Show Gist options
  • Save Bishwas-py/74730a470c2dab4ca3be51af69626c45 to your computer and use it in GitHub Desktop.
Save Bishwas-py/74730a470c2dab4ca3be51af69626c45 to your computer and use it in GitHub Desktop.
lib/live_view_studio_web/live/donations_live.ex
defmodule LiveViewStudioWeb.DonationsLive do
use LiveViewStudioWeb, :live_view
alias LiveViewStudio.Donations
import LiveViewStudioWeb.SortComponents
def mount(_params, _session, socket) do
donations = Donations.list_donations()
socket =
assign(socket,
donations: donations
)
{:ok, socket, temporary_assigns: [donations: []]}
end
def handle_params(unsigned_params, _uri, socket) do
sort_by = valid_sort_by(unsigned_params)
sort_order = Map.get(unsigned_params, "sort_order") || "asc"
opts = %{
sort_by: sort_by,
sort_order: sort_order |> String.to_atom()
}
donations =
Donations.list_donations(opts)
socket = assign(socket, donations: donations, opts: opts)
{:noreply, socket}
end
attr :opts, :map, required: true
attr :sort_by, :atom, required: true
slot :inner_block, required: true
defp sort_link(assigns) do
~H"""
<.link
class="flex items-center gap-2"
patch={
~p"/donations?#{%{sort_by: @sort_by, sort_order: toggled_sort_order(@opts, @sort_by)}}"
}
>
<.hero_icon sort_by={@sort_by} opts={@opts} />
<%= render_slot(@inner_block) %>
</.link>
"""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment