From 833b119ebbdc97cf0577faaacd3e2ca70d3047fd Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Thu, 30 Nov 2023 10:43:48 -0500 Subject: [PATCH 01/17] Convert assigns to streams --- .../live_admin/live/page_editor_live/index.ex | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 16d9bea7..302e023e 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -9,20 +9,15 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do @impl true def menu_link(_, :index), do: {:root, "Pages"} - @impl true - def mount(_params, _session, socket) do - {:ok, assign(socket, :pages, [])} - end - @impl true def handle_params(%{"query" => query}, _uri, socket) do pages = list_pages(socket.assigns.beacon_page.site, query: query) - {:noreply, assign(socket, :pages, pages)} + {:noreply, stream(socket, :pages, pages, reset: true)} end def handle_params(_params, _uri, socket) do pages = list_pages(socket.assigns.beacon_page.site) - {:noreply, assign(socket, :pages, pages)} + {:noreply, stream(socket, :pages, pages)} end @impl true @@ -54,11 +49,11 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do <.main_content class="h-[calc(100vh_-_210px)]"> - <.table id="pages" rows={@pages} row_click={fn page -> JS.navigate(beacon_live_admin_path(@socket, @beacon_page.site, "/pages/#{page.id}")) end}> - <:col :let={page} label="Title"><%= page.title %> - <:col :let={page} label="Path"><%= page.path %> - <:col :let={page} label="Status"><%= display_status(page.status) %> - <:action :let={page}> + <.table id="pages" rows={@streams.pages} row_click={fn {_id, page} -> JS.navigate(beacon_live_admin_path(@socket, @beacon_page.site, "/pages/#{page.id}")) end}> + <:col :let={{_, page}} label="Title"><%= page.title %> + <:col :let={{_, page}} label="Path"><%= page.path %> + <:col :let={{_, page}} label="Status"><%= display_status(page.status) %> + <:action :let={{_, page}}>
<.link navigate={beacon_live_admin_path(@socket, @beacon_page.site, "/pages/#{page.id}")}>Show
From 66d24bd8f4ce1b966fbddab0e49da965cb7cd661 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Thu, 30 Nov 2023 11:21:47 -0500 Subject: [PATCH 02/17] Add default opt value for offset --- lib/beacon/live_admin/content.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/beacon/live_admin/content.ex b/lib/beacon/live_admin/content.ex index 82fa2db8..97e6d52f 100644 --- a/lib/beacon/live_admin/content.ex +++ b/lib/beacon/live_admin/content.ex @@ -98,6 +98,7 @@ defmodule Beacon.LiveAdmin.Content do opts |> Keyword.put_new(:query, nil) |> Keyword.put_new(:per_page, 20) + |> Keyword.put_new(:offset, 0) call(site, Beacon.Content, :list_pages, [site, opts]) end From 8231bba82c7bb159d8949653f95da9c730a4ecc7 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Thu, 30 Nov 2023 11:50:34 -0500 Subject: [PATCH 03/17] Count total pages per page amount --- lib/beacon/live_admin/content.ex | 4 ++++ .../live_admin/live/page_editor_live/index.ex | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/beacon/live_admin/content.ex b/lib/beacon/live_admin/content.ex index 97e6d52f..0fcd966a 100644 --- a/lib/beacon/live_admin/content.ex +++ b/lib/beacon/live_admin/content.ex @@ -103,6 +103,10 @@ defmodule Beacon.LiveAdmin.Content do call(site, Beacon.Content, :list_pages, [site, opts]) end + def count_pages(site) do + call(site, Beacon.Content, :count_pages, [site]) + end + def change_page_variant(site, variant, attrs \\ %{}) do call(site, Beacon.Content, :change_page_variant, [variant, attrs]) end diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 302e023e..44bb06ed 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -6,9 +6,16 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:page_editor, :index}} + @per_page 20 + @impl true def menu_link(_, :index), do: {:root, "Pages"} + @impl true + def mount(_params, _session, socket) do + {:ok, assign(socket, pages: number_of_pages(socket.assigns.beacon_page.site))} + end + @impl true def handle_params(%{"query" => query}, _uri, socket) do pages = list_pages(socket.assigns.beacon_page.site, query: query) @@ -74,6 +81,13 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do end) end + defp number_of_pages(site) do + site + |> Content.count_pages() + |> Kernel./(@per_page) + |> ceil() + end + defp display_status(:unpublished), do: "Unpublished" defp display_status(:published), do: "Published" defp display_status(:created), do: "Draft" From 4daa41d016fef54915f6fdb9770c760734d8ce46 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Wed, 6 Dec 2023 11:49:37 -0500 Subject: [PATCH 04/17] Add basic pagination component --- .../live_admin/components/pagination.ex | 15 +++++++++++++++ lib/beacon/live_admin/content.ex | 6 ------ .../live_admin/live/page_editor_live/index.ex | 19 ++++++++++++++++--- 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 lib/beacon/live_admin/components/pagination.ex diff --git a/lib/beacon/live_admin/components/pagination.ex b/lib/beacon/live_admin/components/pagination.ex new file mode 100644 index 00000000..cda9d9d1 --- /dev/null +++ b/lib/beacon/live_admin/components/pagination.ex @@ -0,0 +1,15 @@ +defmodule Beacon.LiveAdmin.Components.Pagination do + use Phoenix.LiveComponent + + def render(assigns) do + ~H""" +
+ prev + + <%= page %> + + next +
+ """ + end +end diff --git a/lib/beacon/live_admin/content.ex b/lib/beacon/live_admin/content.ex index 0fcd966a..72b27041 100644 --- a/lib/beacon/live_admin/content.ex +++ b/lib/beacon/live_admin/content.ex @@ -94,12 +94,6 @@ defmodule Beacon.LiveAdmin.Content do end def list_pages(site, opts \\ []) do - opts = - opts - |> Keyword.put_new(:query, nil) - |> Keyword.put_new(:per_page, 20) - |> Keyword.put_new(:offset, 0) - call(site, Beacon.Content, :list_pages, [site, opts]) end diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 44bb06ed..05699b22 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -3,6 +3,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do use Beacon.LiveAdmin.PageBuilder alias Beacon.LiveAdmin.Content + alias Beacon.LiveAdmin.Components.Pagination on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:page_editor, :index}} @@ -23,8 +24,8 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do end def handle_params(_params, _uri, socket) do - pages = list_pages(socket.assigns.beacon_page.site) - {:noreply, stream(socket, :pages, pages)} + pages = list_pages(socket.assigns.beacon_page.site, per_page: @per_page) + {:noreply, stream(socket, :pages, pages, reset: true)} end @impl true @@ -39,6 +40,14 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do {:noreply, push_patch(socket, to: path)} end + def handle_event("set-page", %{"page" => page}, socket) do + page = String.to_integer(page) + offset = set_offset(page) + pages = list_pages(socket.assigns.beacon_page.site, offset: offset, per_page: @per_page) + + {:noreply, stream(socket, :pages, pages, reset: true)} + end + @impl true def render(assigns) do ~H""" @@ -69,11 +78,13 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do + + <.live_component module={Pagination} id="pagination" pages={@pages} /> """ end - defp list_pages(site, opts \\ []) do + defp list_pages(site, opts) do site |> Content.list_pages(opts) |> Enum.map(fn page -> @@ -88,6 +99,8 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do |> ceil() end + defp set_offset(page), do: page * @per_page - @per_page + defp display_status(:unpublished), do: "Unpublished" defp display_status(:published), do: "Published" defp display_status(:created), do: "Draft" From b6b715cd2ffbc21f726ac5cbad694ec67a4fe1db Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Wed, 6 Dec 2023 12:22:38 -0500 Subject: [PATCH 05/17] Add basic styling --- lib/beacon/live_admin/components/pagination.ex | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/beacon/live_admin/components/pagination.ex b/lib/beacon/live_admin/components/pagination.ex index cda9d9d1..13ac3313 100644 --- a/lib/beacon/live_admin/components/pagination.ex +++ b/lib/beacon/live_admin/components/pagination.ex @@ -3,12 +3,10 @@ defmodule Beacon.LiveAdmin.Components.Pagination do def render(assigns) do ~H""" -
- prev - - <%= page %> - - next +
+ + +
""" end From 534208f2e67d89bfc2aaebfb98e5a8790971a6d3 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Wed, 6 Dec 2023 12:54:24 -0500 Subject: [PATCH 06/17] Add lateral pagination support --- .../live_admin/components/pagination.ex | 4 +-- .../live_admin/live/page_editor_live/index.ex | 30 +++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/beacon/live_admin/components/pagination.ex b/lib/beacon/live_admin/components/pagination.ex index 13ac3313..f82bbb7a 100644 --- a/lib/beacon/live_admin/components/pagination.ex +++ b/lib/beacon/live_admin/components/pagination.ex @@ -4,9 +4,9 @@ defmodule Beacon.LiveAdmin.Components.Pagination do def render(assigns) do ~H"""
- + - +
""" end diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 05699b22..20d8976d 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -14,7 +14,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do @impl true def mount(_params, _session, socket) do - {:ok, assign(socket, pages: number_of_pages(socket.assigns.beacon_page.site))} + {:ok, assign(socket, page: 1, pages: number_of_pages(socket.assigns.beacon_page.site))} end @impl true @@ -41,11 +41,35 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do end def handle_event("set-page", %{"page" => page}, socket) do - page = String.to_integer(page) + page + |> String.to_integer() + |> set_page(socket) + end + + def handle_event("prev-page", _, %{assigns: %{page: 1}} = socket) do + {:noreply, socket} + end + + def handle_event("prev-page", _, socket) do + set_page(socket.assigns.page - 1, socket) + end + + def handle_event("next-page", _, %{assigns: %{page: page, pages: page}} = socket) do + {:noreply, socket} + end + + def handle_event("next-page", _, socket) do + set_page(socket.assigns.page + 1, socket) + end + + defp set_page(page, socket) do offset = set_offset(page) pages = list_pages(socket.assigns.beacon_page.site, offset: offset, per_page: @per_page) - {:noreply, stream(socket, :pages, pages, reset: true)} + {:noreply, + socket + |> assign(page: page) + |> stream(:pages, pages, reset: true)} end @impl true From dad767a792311b771d0f2427be91526710c97608 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Wed, 6 Dec 2023 14:13:51 -0500 Subject: [PATCH 07/17] Add active page styling --- lib/beacon/live_admin/components/pagination.ex | 15 ++++++++++++--- .../live_admin/live/page_editor_live/index.ex | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/beacon/live_admin/components/pagination.ex b/lib/beacon/live_admin/components/pagination.ex index f82bbb7a..bcd93965 100644 --- a/lib/beacon/live_admin/components/pagination.ex +++ b/lib/beacon/live_admin/components/pagination.ex @@ -4,10 +4,19 @@ defmodule Beacon.LiveAdmin.Components.Pagination do def render(assigns) do ~H"""
- - - + + +
""" end + + defp active_page(page, page), do: "text-indigo-700" + defp active_page(_, _), do: "" + + defp active_prev(1), do: "text-gray-400 disabled" + defp active_prev(_), do: "" + + defp active_next(page, page), do: "text-gray-400 disabled" + defp active_next(_, _), do: "" end diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 20d8976d..df0a183c 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -103,7 +103,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do - <.live_component module={Pagination} id="pagination" pages={@pages} /> + <.live_component module={Pagination} id="pagination" current_page={@page} pages={@pages} /> """ end From 9b21e52d135f2bf91859ddb1054bd6f91101fc5e Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Wed, 6 Dec 2023 15:18:03 -0500 Subject: [PATCH 08/17] Add query param support for page --- .../live_admin/live/page_editor_live/index.ex | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index df0a183c..9382a3a8 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -18,13 +18,18 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do end @impl true - def handle_params(%{"query" => query}, _uri, socket) do - pages = list_pages(socket.assigns.beacon_page.site, query: query) - {:noreply, stream(socket, :pages, pages, reset: true)} - end + def handle_params(params, _uri, socket) do + query = params["query"] + offset = set_offset(params["page"]) + socket = set_page(offset, params["page"], socket) + + pages = + list_pages(socket.assigns.beacon_page.site, + per_page: @per_page, + offset: offset, + query: query + ) - def handle_params(_params, _uri, socket) do - pages = list_pages(socket.assigns.beacon_page.site, per_page: @per_page) {:noreply, stream(socket, :pages, pages, reset: true)} end @@ -63,13 +68,17 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do end defp set_page(page, socket) do - offset = set_offset(page) - pages = list_pages(socket.assigns.beacon_page.site, offset: offset, per_page: @per_page) + path = + beacon_live_admin_path( + socket, + socket.assigns.beacon_page.site, + "/pages?page=#{page}" + ) {:noreply, socket |> assign(page: page) - |> stream(:pages, pages, reset: true)} + |> push_patch(to: path)} end @impl true @@ -123,8 +132,13 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do |> ceil() end + defp set_offset(nil), do: 0 + defp set_offset(page) when is_binary(page), do: String.to_integer(page) * @per_page - @per_page defp set_offset(page), do: page * @per_page - @per_page + defp set_page(0, _page, socket), do: socket + defp set_page(_offset, page, socket), do: assign(socket, page: String.to_integer(page)) + defp display_status(:unpublished), do: "Unpublished" defp display_status(:published), do: "Published" defp display_status(:created), do: "Draft" From 5dc8fdb979a1ff9ad6ec788470917ae5cc1c43d1 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Thu, 7 Dec 2023 12:38:17 -0500 Subject: [PATCH 09/17] Add page query param when searching --- lib/beacon/live_admin/live/page_editor_live/index.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 9382a3a8..1fbeb706 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -39,7 +39,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do beacon_live_admin_path( socket, socket.assigns.beacon_page.site, - "/pages?query=#{query}" + "/pages?query=#{query}&page=#{socket.assigns.page}" ) {:noreply, push_patch(socket, to: path)} From 6f32a5f8ab20883cb9c765fccb00952eb060a5cd Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Thu, 14 Dec 2023 15:54:04 -0500 Subject: [PATCH 10/17] Add sorting support --- .../live_admin/live/page_editor_live/index.ex | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 1fbeb706..c17bd9dc 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -8,29 +8,40 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:page_editor, :index}} @per_page 20 + @default_sort :title @impl true def menu_link(_, :index), do: {:root, "Pages"} @impl true def mount(_params, _session, socket) do - {:ok, assign(socket, page: 1, pages: number_of_pages(socket.assigns.beacon_page.site))} + {:ok, + assign(socket, + page: 1, + pages: number_of_pages(socket.assigns.beacon_page.site), + sort: @default_sort + )} end @impl true def handle_params(params, _uri, socket) do query = params["query"] offset = set_offset(params["page"]) + sort = set_sort(params["sort"], socket) socket = set_page(offset, params["page"], socket) pages = list_pages(socket.assigns.beacon_page.site, per_page: @per_page, offset: offset, - query: query + query: query, + sort: sort ) - {:noreply, stream(socket, :pages, pages, reset: true)} + {:noreply, + socket + |> assign(sort: sort) + |> stream(:pages, pages, reset: true)} end @impl true @@ -39,12 +50,23 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do beacon_live_admin_path( socket, socket.assigns.beacon_page.site, - "/pages?query=#{query}&page=#{socket.assigns.page}" + "/pages?query=#{query}&page=#{socket.assigns.page}&sort=#{socket.assigns.sort}" ) {:noreply, push_patch(socket, to: path)} end + def handle_event("sort", %{"sort" => sort}, socket) do + path = + beacon_live_admin_path( + socket, + socket.assigns.beacon_page.site, + "/pages?page=#{socket.assigns.page}&sort=#{sort}" + ) + + {:noreply, push_navigate(socket, to: path)} + end + def handle_event("set-page", %{"page" => page}, socket) do page |> String.to_integer() @@ -97,6 +119,13 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do <.input field={f[:query]} type="search" autofocus={true} placeholder="Search by path or title (showing up to 20 results)" /> +
+ +
+ <.main_content class="h-[calc(100vh_-_210px)]"> <.table id="pages" rows={@streams.pages} row_click={fn {_id, page} -> JS.navigate(beacon_live_admin_path(@socket, @beacon_page.site, "/pages/#{page.id}")) end}> <:col :let={{_, page}} label="Title"><%= page.title %> @@ -139,6 +168,9 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do defp set_page(0, _page, socket), do: socket defp set_page(_offset, page, socket), do: assign(socket, page: String.to_integer(page)) + defp set_sort(nil, socket), do: socket.assigns.sort + defp set_sort(sort, _socket), do: String.to_atom(sort) + defp display_status(:unpublished), do: "Unpublished" defp display_status(:published), do: "Published" defp display_status(:created), do: "Draft" From 8217b296b7d0972dd109c6f717232b77627783a6 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Fri, 15 Dec 2023 12:21:08 -0500 Subject: [PATCH 11/17] Inline tailwind classes by state --- lib/beacon/live_admin/components/pagination.ex | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/beacon/live_admin/components/pagination.ex b/lib/beacon/live_admin/components/pagination.ex index bcd93965..abfc43eb 100644 --- a/lib/beacon/live_admin/components/pagination.ex +++ b/lib/beacon/live_admin/components/pagination.ex @@ -4,19 +4,12 @@ defmodule Beacon.LiveAdmin.Components.Pagination do def render(assigns) do ~H"""
- - - + + +
""" end - - defp active_page(page, page), do: "text-indigo-700" - defp active_page(_, _), do: "" - - defp active_prev(1), do: "text-gray-400 disabled" - defp active_prev(_), do: "" - - defp active_next(page, page), do: "text-gray-400 disabled" - defp active_next(_, _), do: "" end From 44a74dfc73f7a003c32386c32d923223543b4e54 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Fri, 15 Dec 2023 12:49:28 -0500 Subject: [PATCH 12/17] Turn pagination into function component --- .../live_admin/components/admin_components.ex | 19 +++++++++++++++++++ .../live_admin/components/pagination.ex | 15 --------------- .../live_admin/live/page_editor_live/index.ex | 3 +-- 3 files changed, 20 insertions(+), 17 deletions(-) delete mode 100644 lib/beacon/live_admin/components/pagination.ex diff --git a/lib/beacon/live_admin/components/admin_components.ex b/lib/beacon/live_admin/components/admin_components.ex index 4271b750..98adde90 100644 --- a/lib/beacon/live_admin/components/admin_components.ex +++ b/lib/beacon/live_admin/components/admin_components.ex @@ -621,4 +621,23 @@ defmodule Beacon.LiveAdmin.AdminComponents do
""" end + + @doc """ + Renders navigation by defined pagination. + + """ + attr :current_page, :integer, required: true + attr :pages, :integer, required: true + + def pagination(assigns) do + ~H""" +
+ + + +
+ """ + end end diff --git a/lib/beacon/live_admin/components/pagination.ex b/lib/beacon/live_admin/components/pagination.ex deleted file mode 100644 index abfc43eb..00000000 --- a/lib/beacon/live_admin/components/pagination.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule Beacon.LiveAdmin.Components.Pagination do - use Phoenix.LiveComponent - - def render(assigns) do - ~H""" -
- - - -
- """ - end -end diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index c17bd9dc..43547f34 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -3,7 +3,6 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do use Beacon.LiveAdmin.PageBuilder alias Beacon.LiveAdmin.Content - alias Beacon.LiveAdmin.Components.Pagination on_mount {Beacon.LiveAdmin.Hooks.Authorized, {:page_editor, :index}} @@ -141,7 +140,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do - <.live_component module={Pagination} id="pagination" current_page={@page} pages={@pages} /> + <.pagination current_page={@page} pages={@pages} /> """ end From 7862f69d52e399da725d927fa645d31ac28f4349 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Fri, 12 Jan 2024 14:14:17 -0500 Subject: [PATCH 13/17] Include input select on form --- .../live_admin/live/page_editor_live/index.ex | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 43547f34..8b90f33d 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -44,26 +44,31 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do end @impl true - def handle_event("search", %{"search" => %{"query" => query}}, socket) do + # handles sorting only (uses push navigate) + def handle_event("search", %{"search" => %{"query" => "", "sort" => sort}}, socket) do path = beacon_live_admin_path( socket, socket.assigns.beacon_page.site, - "/pages?query=#{query}&page=#{socket.assigns.page}&sort=#{socket.assigns.sort}" + "/pages?page=#{socket.assigns.page}&sort=#{sort}" ) - {:noreply, push_patch(socket, to: path)} + {:noreply, + socket + |> assign(sort: set_sort(sort, socket)) + |> push_navigate(to: path)} end - def handle_event("sort", %{"sort" => sort}, socket) do + # handles text searching (uses push patch) + def handle_event("search", %{"search" => %{"query" => query}}, socket) do path = beacon_live_admin_path( socket, socket.assigns.beacon_page.site, - "/pages?page=#{socket.assigns.page}&sort=#{sort}" + "/pages?#{query_param(query)}page=#{socket.assigns.page}&sort=#{socket.assigns.sort}" ) - {:noreply, push_navigate(socket, to: path)} + {:noreply, push_patch(socket, to: path)} end def handle_event("set-page", %{"page" => page}, socket) do @@ -115,16 +120,16 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do <.simple_form :let={f} for={%{}} as={:search} phx-change="search"> - <.input field={f[:query]} type="search" autofocus={true} placeholder="Search by path or title (showing up to 20 results)" /> +
+
+ <.input field={f[:query]} type="search" autofocus={true} placeholder="Search by path or title (showing up to 20 results)" /> +
+
+ <.input type="select" field={f[:sort]} value={@sort} options={[{"Title", "title"}, {"Path", "path"}]} /> +
+
-
- -
- <.main_content class="h-[calc(100vh_-_210px)]"> <.table id="pages" rows={@streams.pages} row_click={fn {_id, page} -> JS.navigate(beacon_live_admin_path(@socket, @beacon_page.site, "/pages/#{page.id}")) end}> <:col :let={{_, page}} label="Title"><%= page.title %> @@ -168,9 +173,13 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do defp set_page(_offset, page, socket), do: assign(socket, page: String.to_integer(page)) defp set_sort(nil, socket), do: socket.assigns.sort + defp set_sort("", socket), do: socket.assigns.sort defp set_sort(sort, _socket), do: String.to_atom(sort) defp display_status(:unpublished), do: "Unpublished" defp display_status(:published), do: "Published" defp display_status(:created), do: "Draft" + + defp query_param(""), do: "" + defp query_param(query), do: "query=#{query}&" end From 3708bcc39ef3558617e1bf4803ba5eb13c02c5dc Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Fri, 12 Jan 2024 15:03:33 -0500 Subject: [PATCH 14/17] Add query into url state --- .../live_admin/live/page_editor_live/index.ex | 36 +++++++------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 8b90f33d..af0d6c09 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -15,11 +15,14 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do @impl true def mount(_params, _session, socket) do {:ok, - assign(socket, + socket + |> assign( page: 1, pages: number_of_pages(socket.assigns.beacon_page.site), - sort: @default_sort - )} + sort: @default_sort, + query: "" + ) + |> stream_configure(:pages, dom_id: &"#{Ecto.UUID.generate()}-#{&1.id}")} end @impl true @@ -39,36 +42,23 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do {:noreply, socket - |> assign(sort: sort) + |> assign(sort: sort, query: query) |> stream(:pages, pages, reset: true)} end @impl true - # handles sorting only (uses push navigate) - def handle_event("search", %{"search" => %{"query" => "", "sort" => sort}}, socket) do + def handle_event("search", %{"search" => %{"query" => query, "sort" => sort}}, socket) do path = beacon_live_admin_path( socket, socket.assigns.beacon_page.site, - "/pages?page=#{socket.assigns.page}&sort=#{sort}" + "/pages?page=#{socket.assigns.page}&sort=#{sort}#{query_param(query)}" ) {:noreply, socket |> assign(sort: set_sort(sort, socket)) - |> push_navigate(to: path)} - end - - # handles text searching (uses push patch) - def handle_event("search", %{"search" => %{"query" => query}}, socket) do - path = - beacon_live_admin_path( - socket, - socket.assigns.beacon_page.site, - "/pages?#{query_param(query)}page=#{socket.assigns.page}&sort=#{socket.assigns.sort}" - ) - - {:noreply, push_patch(socket, to: path)} + |> push_patch(to: path)} end def handle_event("set-page", %{"page" => page}, socket) do @@ -122,7 +112,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do <.simple_form :let={f} for={%{}} as={:search} phx-change="search">
- <.input field={f[:query]} type="search" autofocus={true} placeholder="Search by path or title (showing up to 20 results)" /> + <.input field={f[:query]} value={@query} type="search" autofocus={true} placeholder="Search by path or title (showing up to 20 results)" />
<.input type="select" field={f[:sort]} value={@sort} options={[{"Title", "title"}, {"Path", "path"}]} /> @@ -131,7 +121,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do <.main_content class="h-[calc(100vh_-_210px)]"> - <.table id="pages" rows={@streams.pages} row_click={fn {_id, page} -> JS.navigate(beacon_live_admin_path(@socket, @beacon_page.site, "/pages/#{page.id}")) end}> + <.table id="pages" rows={@streams.pages} row_click={fn {dom_id, page} -> JS.navigate(beacon_live_admin_path(@socket, @beacon_page.site, "/pages/#{page.id}")) end}> <:col :let={{_, page}} label="Title"><%= page.title %> <:col :let={{_, page}} label="Path"><%= page.path %> <:col :let={{_, page}} label="Status"><%= display_status(page.status) %> @@ -181,5 +171,5 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do defp display_status(:created), do: "Draft" defp query_param(""), do: "" - defp query_param(query), do: "query=#{query}&" + defp query_param(query), do: "&query=#{query}" end From 4750701f7c37b28be58b385fe3bc77a2884582b9 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Fri, 12 Jan 2024 15:18:22 -0500 Subject: [PATCH 15/17] Underscored unused variable --- lib/beacon/live_admin/live/page_editor_live/index.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index af0d6c09..33a997dd 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -121,7 +121,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do <.main_content class="h-[calc(100vh_-_210px)]"> - <.table id="pages" rows={@streams.pages} row_click={fn {dom_id, page} -> JS.navigate(beacon_live_admin_path(@socket, @beacon_page.site, "/pages/#{page.id}")) end}> + <.table id="pages" rows={@streams.pages} row_click={fn {_dom_id, page} -> JS.navigate(beacon_live_admin_path(@socket, @beacon_page.site, "/pages/#{page.id}")) end}> <:col :let={{_, page}} label="Title"><%= page.title %> <:col :let={{_, page}} label="Path"><%= page.path %> <:col :let={{_, page}} label="Status"><%= display_status(page.status) %> From db652dbd8162bd980d69289cea9e7f9c69bb6d2e Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Fri, 26 Jan 2024 11:22:45 -0500 Subject: [PATCH 16/17] Add conditional rendering --- lib/beacon/live_admin/live/page_editor_live/index.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/beacon/live_admin/live/page_editor_live/index.ex b/lib/beacon/live_admin/live/page_editor_live/index.ex index 33a997dd..5ecd60af 100644 --- a/lib/beacon/live_admin/live/page_editor_live/index.ex +++ b/lib/beacon/live_admin/live/page_editor_live/index.ex @@ -114,7 +114,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do
<.input field={f[:query]} value={@query} type="search" autofocus={true} placeholder="Search by path or title (showing up to 20 results)" />
-
+
0} class="basis-1/12"> <.input type="select" field={f[:sort]} value={@sort} options={[{"Title", "title"}, {"Path", "path"}]} />
@@ -135,7 +135,7 @@ defmodule Beacon.LiveAdmin.PageEditorLive.Index do - <.pagination current_page={@page} pages={@pages} /> + <.pagination :if={@pages > 0} current_page={@page} pages={@pages} /> """ end From 7c03f0266b3aaa5dd76a10b6ac81cc0ce7fcba36 Mon Sep 17 00:00:00 2001 From: ChristianTovar Date: Fri, 26 Jan 2024 14:54:05 -0500 Subject: [PATCH 17/17] Update beacon --- mix.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.lock b/mix.lock index ff67215c..18a44c6a 100644 --- a/mix.lock +++ b/mix.lock @@ -1,6 +1,6 @@ %{ "accent": {:hex, :accent, "1.1.1", "20257356446d45078b19b91608f74669b407b39af891ee3db9ee6824d1cae19d", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.3", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "6d5afa50d4886e3370e04fa501468cbaa6c4b5fe926f72ccfa844ad9e259adae"}, - "beacon": {:git, "https://github.com/beaconCMS/beacon.git", "4fa1eca70e84ca7157b0e5dcbb4cc60f9f10b25f", []}, + "beacon": {:git, "https://github.com/beaconCMS/beacon.git", "3edd59790e8d28b9d1610203f41396d3bf434015", []}, "castore": {:hex, :castore, "1.0.4", "ff4d0fb2e6411c0479b1d965a814ea6d00e51eb2f58697446e9c41a97d940b28", [:mix], [], "hexpm", "9418c1b8144e11656f0be99943db4caf04612e3eaecefb5dae9a2a87565584f8"}, "cc_precompiler": {:hex, :cc_precompiler, "0.1.7", "77de20ac77f0e53f20ca82c563520af0237c301a1ec3ab3bc598e8a96c7ee5d9", [:mix], [{:elixir_make, "~> 0.7.3", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "2768b28bf3c2b4f788c995576b39b8cb5d47eb788526d93bd52206c1d8bf4b75"}, "cowboy": {:hex, :cowboy, "2.10.0", "ff9ffeff91dae4ae270dd975642997afe2a1179d94b1887863e43f681a203e26", [:make, :rebar3], [{:cowlib, "2.12.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "3afdccb7183cc6f143cb14d3cf51fa00e53db9ec80cdcd525482f5e99bc41d6b"},