defmodule ChiyaWeb.NoteListLive do
use ChiyaWeb, :live_view
@impl true
def mount(_params, __session, socket) do
channels = Chiya.Channels.list_channels() |> Chiya.Channels.preload_channel()
{:ok, {notes, meta}} = Chiya.Notes.list_admin_notes(%{})
{:ok,
socket
|> assign(%{
channels: channels,
notes: notes,
meta: meta
})}
end
@impl true
def handle_params(params, _uri, socket) do
case Chiya.Notes.list_admin_notes(params) do
{:ok, {notes, meta}} ->
{:noreply, socket |> assign(%{notes: notes, meta: meta})}
{:error, data} ->
{:noreply, socket}
end
end
@impl true
def handle_event("update-filter", params, socket) do
params = Map.delete(params, "_target")
{:noreply, push_patch(socket, to: ~p"/admin/notes?#{params}")}
end
defp channel_list(assigns) do
channels =
assigns.channels
|> Enum.map(fn c ->
{"#{c.name} (#{Enum.count(c.notes)})", c.name}
end)
[{"All", nil}] ++ channels
end
@impl true
def render(assigns) do
~H"""
<.header>
<.icon name="hero-document-text" /> Notes
<:subtitle>Notes are the content, the heart of your site.
<:actions>
<.link href={~p"/admin/notes/new"}>
<.button><.icon name="hero-plus-small" /> New Note
<.link href={~p"/admin/notes/import"}>
<.button><.icon name="hero-arrow-down-tray" /> Import Note
<.filter_form
fields={[
name: [op: :ilike_and],
channels: [
op: :ilike_and,
type: "select",
options: channel_list(assigns)
]
]}
meta={@meta}
id="user-filter-form"
/>
<%= for note <- @notes do %>
<% end %>
"""
end
end