only filter by non-empty channels, clean up

This commit is contained in:
Inhji 2023-04-08 15:27:59 +02:00
parent 556f039162
commit 6c55df3139
2 changed files with 21 additions and 12 deletions

View file

@ -1,26 +1,25 @@
defmodule ChiyaWeb.NoteController do
use ChiyaWeb, :controller
import Plug.Conn, only: [assign: 3]
alias Chiya.Notes
alias Chiya.Notes.{Note, NoteImport}
def index(conn, %{"channel" => channel_slug}) do
channels =
Chiya.Channels.list_channels()
|> Chiya.Channels.preload_channel()
channel = Chiya.Channels.get_channel_by_slug!(channel_slug)
notes = Notes.list_notes_by_channel(channel)
render(conn, :index, notes: notes, channels: channels)
conn
|> with_channels()
|> render(:index, notes: notes)
end
def index(conn, _params) do
channels =
Chiya.Channels.list_channels()
|> Chiya.Channels.preload_channel()
notes = Notes.list_notes()
render(conn, :index, notes: notes, channels: channels)
conn
|> with_channels()
|> render(:index, notes: notes)
end
def new(conn, _params) do
@ -150,6 +149,16 @@ defmodule ChiyaWeb.NoteController do
|> redirect(to: ~p"/admin/notes")
end
defp with_channels(conn) do
assign(
conn,
:channels,
Chiya.Channels.list_channels()
|> Chiya.Channels.preload_channel()
|> Enum.filter(fn c -> not Enum.empty?(c.notes) end)
)
end
defp from_channel_ids(note_params) do
selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1)

View file

@ -12,9 +12,9 @@
</.header>
<section class="flex flex-row flex-wrap mt-4 gap-3">
<a href={~p"/admin/notes"} class="text-sm rounded-full bg-gray-300 hover:bg-gray-400 px-2 py-1">All</a>
<a href={~p"/admin/notes"} class="text-sm dark:text-gray-300 rounded-full bg-gray-300 hover:bg-gray-400 dark:bg-gray-700 dark:hover:bg-gray-600 px-2 py-1">All</a>
<%= for channel <- @channels do %>
<a href={~p"/admin/notes?channel=#{channel.slug}"} class="text-sm rounded-full bg-gray-300 hover:bg-gray-400 px-2 py-1"><%= channel.name %> <span class="text-gray-600">(<%= Enum.count(channel.notes) %>)</span></a>
<a href={~p"/admin/notes?channel=#{channel.slug}"} class="text-sm dark:text-gray-300 rounded-full bg-gray-300 hover:bg-gray-400 dark:bg-gray-700 dark:hover:bg-gray-600 px-2 py-1"><%= channel.name %> <span class="text-gray-600 dark:text-gray-500">(<%= Enum.count(channel.notes) %>)</span></a>
<% end %>
</section>