From 48c56b273907cfb804efcb12141131e228476702 Mon Sep 17 00:00:00 2001 From: Inhji Date: Tue, 12 Sep 2023 07:20:43 +0200 Subject: [PATCH] filter form --- lib/chiya/notes.ex | 3 +- lib/chiya/notes/note.ex | 14 +++++++- lib/chiya_web/components/core_components.ex | 11 +++++-- .../controllers/note_html/index.html.heex | 8 +++-- lib/chiya_web/live/note_list_live.ex | 33 +++++++++++++++++-- 5 files changed, 60 insertions(+), 9 deletions(-) diff --git a/lib/chiya/notes.ex b/lib/chiya/notes.ex index 3200a62..e09cf0b 100644 --- a/lib/chiya/notes.ex +++ b/lib/chiya/notes.ex @@ -42,7 +42,8 @@ defmodule Chiya.Notes do def list_admin_notes(params) do q = Note - |> order_by([n], desc: n.updated_at, desc: n.published_at) + |> join(:inner, [n], c in assoc(n, :channels), as: :channels) + |> order_by([n, c], desc: n.updated_at, desc: n.published_at) Chiya.Flop.validate_and_run(q, params, for: Chiya.Notes.Note) end diff --git a/lib/chiya/notes/note.ex b/lib/chiya/notes/note.ex index 04d18d4..efa9a30 100644 --- a/lib/chiya/notes/note.ex +++ b/lib/chiya/notes/note.ex @@ -13,7 +13,19 @@ defmodule Chiya.Notes.Note do @derive { Flop.Schema, - filterable: [:name, :kind], sortable: [:name], default_limit: 10, max_limit: 100 + filterable: [:name, :channels], + sortable: [:name], + default_limit: 10, + max_limit: 100, + adapter_opts: [ + join_fields: [ + channels: [ + binding: :channels, + field: :name, + ecto_type: :string + ] + ] + ] } @derive {Jason.Encoder, only: [:id, :name, :content, :slug, :channels, :tags]} schema "notes" do diff --git a/lib/chiya_web/components/core_components.ex b/lib/chiya_web/components/core_components.ex index 6bb9aab..0703beb 100644 --- a/lib/chiya_web/components/core_components.ex +++ b/lib/chiya_web/components/core_components.ex @@ -748,12 +748,17 @@ defmodule ChiyaWeb.CoreComponents do assigns = assign(assigns, form: Phoenix.Component.to_form(meta), meta: nil) ~H""" - <.form for={@form} id={@id} phx-target={@target} phx-change={@on_change} phx-submit={@on_change}> + <.form + for={@form} + id={@id} + class="stack" + phx-target={@target} + phx-change={@on_change} + phx-submit={@on_change} + > <.filter_fields :let={i} form={@form} fields={@fields}> <.input field={i.field} label={i.label} type={i.type} phx-debounce={120} {i.rest} /> - - <.button class="button" name="reset">reset """ end diff --git a/lib/chiya_web/controllers/note_html/index.html.heex b/lib/chiya_web/controllers/note_html/index.html.heex index a31efc3..517f9d7 100644 --- a/lib/chiya_web/controllers/note_html/index.html.heex +++ b/lib/chiya_web/controllers/note_html/index.html.heex @@ -13,15 +13,19 @@
All (<%= Enum.count(@notes) %>) <%= for channel <- @channels do %> <%= channel.name %> (<%= Enum.count(channel.notes) %>) diff --git a/lib/chiya_web/live/note_list_live.ex b/lib/chiya_web/live/note_list_live.ex index 891eeef..dbce6d1 100644 --- a/lib/chiya_web/live/note_list_live.ex +++ b/lib/chiya_web/live/note_list_live.ex @@ -3,8 +3,16 @@ defmodule ChiyaWeb.NoteListLive do @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(%{notes: notes, meta: meta})} + + {:ok, + socket + |> assign(%{ + channels: channels, + notes: notes, + meta: meta + })} end @impl true @@ -25,6 +33,16 @@ defmodule ChiyaWeb.NoteListLive do {: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""" @@ -42,7 +60,18 @@ defmodule ChiyaWeb.NoteListLive do
- <.filter_form fields={[name: [op: :ilike_and]]} meta={@meta} id="user-filter-form" /> + <.filter_form + fields={[ + name: [op: :ilike_and], + channels: [ + op: :ilike_and, + type: "select", + options: channel_list(assigns) + ] + ]} + meta={@meta} + id="user-filter-form" + />