From 77dda92301395204a2b7bef0c030c9abe8d9b861 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Thu, 23 Nov 2023 18:03:11 +0000 Subject: [PATCH] More stuff --- lib/wish/wishlist/item.ex | 5 +- lib/wish_web/live/home_live/details.ex | 15 +++++ lib/wish_web/live/home_live/details.html.heex | 10 ++++ lib/wish_web/live/home_live/index.ex | 1 - lib/wish_web/live/home_live/index.html.heex | 55 +++++++------------ lib/wish_web/live/item_live/form_component.ex | 1 + lib/wish_web/router.ex | 1 + .../20231123173824_add_image_url.exs | 9 +++ 8 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 lib/wish_web/live/home_live/details.ex create mode 100644 lib/wish_web/live/home_live/details.html.heex create mode 100644 priv/repo/migrations/20231123173824_add_image_url.exs diff --git a/lib/wish/wishlist/item.ex b/lib/wish/wishlist/item.ex index 572bb1a..45b211e 100644 --- a/lib/wish/wishlist/item.ex +++ b/lib/wish/wishlist/item.ex @@ -8,6 +8,7 @@ defmodule Wish.Wishlist.Item do field :url, :string field :received, :boolean, default: false field :desire, :integer + field :image_url, :string timestamps(type: :utc_datetime) end @@ -15,7 +16,7 @@ defmodule Wish.Wishlist.Item do @doc false def changeset(item, attrs) do item - |> cast(attrs, [:title, :description, :url, :received, :desire]) - |> validate_required([:title, :description, :url, :received, :desire]) + |> cast(attrs, [:title, :description, :url, :received, :desire, :image_url]) + |> validate_required([:url]) end end diff --git a/lib/wish_web/live/home_live/details.ex b/lib/wish_web/live/home_live/details.ex new file mode 100644 index 0000000..948ed1e --- /dev/null +++ b/lib/wish_web/live/home_live/details.ex @@ -0,0 +1,15 @@ +defmodule WishWeb.HomeLive.Details do + use WishWeb, :live_view + + alias Wish.Wishlist + + def mount(_params, _session, socket) do + {:ok, socket} + end + + def handle_params(%{"id" => id}, _, socket) do + {:noreply, + socket + |> assign(:item, Wishlist.get_item!(id))} + end +end diff --git a/lib/wish_web/live/home_live/details.html.heex b/lib/wish_web/live/home_live/details.html.heex new file mode 100644 index 0000000..f0f9d1d --- /dev/null +++ b/lib/wish_web/live/home_live/details.html.heex @@ -0,0 +1,10 @@ +<.link navigate={~p"/"}> + <.icon name="hero-arrow-left" /> Back to list + +<.header> + <%= @item.title %> + + +
+ +
diff --git a/lib/wish_web/live/home_live/index.ex b/lib/wish_web/live/home_live/index.ex index d7513b8..1d526f1 100644 --- a/lib/wish_web/live/home_live/index.ex +++ b/lib/wish_web/live/home_live/index.ex @@ -2,7 +2,6 @@ defmodule WishWeb.HomeLive.Index do use WishWeb, :live_view alias Wish.Wishlist - alias Wish.Wishlist.Item @impl true def mount(_params, _session, socket) do diff --git a/lib/wish_web/live/home_live/index.html.heex b/lib/wish_web/live/home_live/index.html.heex index 846edc1..51265ce 100644 --- a/lib/wish_web/live/home_live/index.html.heex +++ b/lib/wish_web/live/home_live/index.html.heex @@ -2,38 +2,25 @@ Listing Items -<.table - id="items" - rows={@streams.items} - row_click={fn {_id, item} -> JS.navigate(~p"/items/#{item}") end} -> - <:col :let={{_id, item}} label="Title"><%= item.title %> - <:col :let={{_id, item}} label="Description"><%= item.description %> - <:col :let={{_id, item}} label="Url"><%= item.url %> - <:col :let={{_id, item}} label="Desire"><%= item.desire %> - <:action :let={{_id, item}}> -
- <.link navigate={~p"/items/#{item}"}>Show -
- <.link patch={~p"/items/#{item}/edit"}>Edit - - <:action :let={{id, item}}> - <.link - phx-click={JS.push("delete", value: %{id: item.id}) |> hide("##{id}")} - data-confirm="Are you sure?" - > - Delete - - - +
+
+ + <%= item.title %> +
+
-<.modal :if={@live_action in [:new, :edit]} id="item-modal" show on_cancel={JS.patch(~p"/items")}> - <.live_component - module={WishWeb.ItemLive.FormComponent} - id={@item.id || :new} - title={@page_title} - action={@live_action} - item={@item} - patch={~p"/items"} - /> - +
+
+ + <%= item.title %> +
+
diff --git a/lib/wish_web/live/item_live/form_component.ex b/lib/wish_web/live/item_live/form_component.ex index 9a79bac..0206993 100644 --- a/lib/wish_web/live/item_live/form_component.ex +++ b/lib/wish_web/live/item_live/form_component.ex @@ -22,6 +22,7 @@ defmodule WishWeb.ItemLive.FormComponent do <.input field={@form[:title]} type="text" label="Title" /> <.input field={@form[:description]} type="text" label="Description" /> <.input field={@form[:url]} type="text" label="Url" /> + <.input field={@form[:image_url]} type="text" label="Image URL" /> <.input field={@form[:received]} type="checkbox" label="Received" /> <.input field={@form[:desire]} type="number" label="Desire" /> <:actions> diff --git a/lib/wish_web/router.ex b/lib/wish_web/router.ex index 71d2848..9ab4a7e 100644 --- a/lib/wish_web/router.ex +++ b/lib/wish_web/router.ex @@ -21,6 +21,7 @@ defmodule WishWeb.Router do pipe_through :browser live "/", HomeLive.Index, :index + live "/details/:id", HomeLive.Details, :index end # Other scopes may use custom stacks. diff --git a/priv/repo/migrations/20231123173824_add_image_url.exs b/priv/repo/migrations/20231123173824_add_image_url.exs new file mode 100644 index 0000000..7ae1ee4 --- /dev/null +++ b/priv/repo/migrations/20231123173824_add_image_url.exs @@ -0,0 +1,9 @@ +defmodule Wish.Repo.Migrations.AddImageUrl do + use Ecto.Migration + + def change do + alter table("items") do + add :image_url, :string + end + end +end