From cb7c15c345ea212f3e376b5c782cccb7f392014d Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Mon, 27 Nov 2023 16:49:59 +0000 Subject: [PATCH] Improve list view and add cookie --- assets/js/app.js | 4 + lib/wish_web/layout.ex | 16 +++ lib/wish_web/live/home_live/index.ex | 24 ++-- lib/wish_web/live/home_live/index.html.heex | 138 +++++++++++++------- lib/wish_web/router.ex | 6 +- 5 files changed, 129 insertions(+), 59 deletions(-) create mode 100644 lib/wish_web/layout.ex diff --git a/assets/js/app.js b/assets/js/app.js index df0cdd9..c17dfa9 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -30,6 +30,10 @@ topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"}) window.addEventListener("phx:page-loading-start", _info => topbar.show(300)) window.addEventListener("phx:page-loading-stop", _info => topbar.hide()) +window.addEventListener("toggle_view_state", e => { + document.cookie = `user_display=${e.detail.grid ? "grid" : "list"}; SameSite=lax;` +}) + // connect if there are any LiveViews on the page liveSocket.connect() diff --git a/lib/wish_web/layout.ex b/lib/wish_web/layout.ex new file mode 100644 index 0000000..4466c7a --- /dev/null +++ b/lib/wish_web/layout.ex @@ -0,0 +1,16 @@ +defmodule WishWeb.Plug.GetLayoutFromCookie do + import Plug.Conn + + def init(_) do + %{} + end + + def call(conn, _opts) do + conn = fetch_cookies(conn) + + case conn.cookies["user_display"] do + nil -> conn + other -> conn |> put_session(:user_display, other) + end + end +end diff --git a/lib/wish_web/live/home_live/index.ex b/lib/wish_web/live/home_live/index.ex index ea0d081..ac0ee07 100644 --- a/lib/wish_web/live/home_live/index.ex +++ b/lib/wish_web/live/home_live/index.ex @@ -4,19 +4,19 @@ defmodule WishWeb.HomeLive.Index do alias Wish.Wishlist @impl true - def mount(_params, _session, socket) do - {:ok, assign(socket, :items, Wishlist.list_items()) |> assign(:display, :grid)} + def mount(_params, session, socket) do + grid? = + case Map.get(session, "user_display", "grid") do + "grid" -> true + _ -> false + end + + {:ok, assign(socket, :items, Wishlist.list_items()) |> assign(:grid, grid?)} end @impl true - def handle_event("toggle", _, socket) do - new_state = - case socket.assigns.display do - :row -> :grid - :grid -> :row - end - - {:noreply, assign(socket, :display, new_state)} + def handle_event("toggle_view_state", _, socket) do + {:noreply, assign(socket, :grid, !socket.assigns.grid)} end @impl true @@ -25,7 +25,9 @@ defmodule WishWeb.HomeLive.Index do case Wishlist.toggle_received(item) do {:ok, updated_item} -> - {:noreply, assign(socket, :item, updated_item)} + index = Enum.find_index(socket.assigns.items, &(&1.id == updated_item.id)) + updated_list = List.update_at(socket.assigns.items, index, fn _ -> updated_item end) + {:noreply, assign(socket, :items, updated_list)} {:error, _} -> {:noreply, socket} diff --git a/lib/wish_web/live/home_live/index.html.heex b/lib/wish_web/live/home_live/index.html.heex index 4f9076d..9dc02e2 100644 --- a/lib/wish_web/live/home_live/index.html.heex +++ b/lib/wish_web/live/home_live/index.html.heex @@ -1,60 +1,104 @@ <.header> Listing Items <:actions> - <.button phx-click="toggle"> + <.button phx-click={ + JS.push("toggle_view_state") + |> JS.dispatch("toggle_view_state", detail: %{"grid" => !@grid}) + }> Toggle Display -
"grid grid-cols-3 gap-2" - :row -> "" - end - } - id="items-grid" -> -
"h-72" - :row -> "flex flex-row h-24" - end - ]} - > -
- {item.title} -
- <%= if item.received do %> -
- <.icon name="hero-check-circle" />Received +<%= if @grid do %> +
+
+
+ {item.title} + <%= if item.received do %> +
+ <.icon name="hero-check-circle" />Received +
+ <% end %>
- <% end %> -
- <%= item.title %> -
- <.icon name="hero-ellipsis-vertical" class="w-7 h-7" /> -
item.id})} - hidden - > - Mark visible +
+ <%= item.title %> +
+ <.icon name="hero-ellipsis-vertical" class="w-7 h-7" /> +
-
+<% else %> +
+
+
+ {item.title} + <%= if item.received do %> +
+ <.icon name="hero-check-circle" />Received +
+ <% end %> +
+
+ <%= item.title %> +
+
+ <.icon name="hero-ellipsis-vertical" class="w-7 h-7" /> + +
+
+
+<% end %> diff --git a/lib/wish_web/router.ex b/lib/wish_web/router.ex index 9ab4a7e..74f826c 100644 --- a/lib/wish_web/router.ex +++ b/lib/wish_web/router.ex @@ -17,8 +17,12 @@ defmodule WishWeb.Router do plug :accepts, ["json"] end + pipeline :get_layout do + plug WishWeb.Plug.GetLayoutFromCookie + end + scope "/", WishWeb do - pipe_through :browser + pipe_through [:browser, :get_layout] live "/", HomeLive.Index, :index live "/details/:id", HomeLive.Details, :index