From 6c55df3139e75486683bd79b7d5fb29d8d6913f8 Mon Sep 17 00:00:00 2001 From: Inhji Date: Sat, 8 Apr 2023 15:27:59 +0200 Subject: [PATCH] only filter by non-empty channels, clean up --- lib/chiya_web/controllers/note_controller.ex | 29 ++++++++++++------- .../controllers/note_html/index.html.heex | 4 +-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/chiya_web/controllers/note_controller.ex b/lib/chiya_web/controllers/note_controller.ex index da02d0c..0d4a958 100644 --- a/lib/chiya_web/controllers/note_controller.ex +++ b/lib/chiya_web/controllers/note_controller.ex @@ -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) diff --git a/lib/chiya_web/controllers/note_html/index.html.heex b/lib/chiya_web/controllers/note_html/index.html.heex index 1854965..120b173 100644 --- a/lib/chiya_web/controllers/note_html/index.html.heex +++ b/lib/chiya_web/controllers/note_html/index.html.heex @@ -12,9 +12,9 @@
- All + All <%= for channel <- @channels do %> - <%= channel.name %> (<%= Enum.count(channel.notes) %>) + <%= channel.name %> (<%= Enum.count(channel.notes) %>) <% end %>