From 76ee9d7cd8071f9062f15c7f09d44c3b45deedf9 Mon Sep 17 00:00:00 2001 From: Rick G Oliveira Date: Wed, 13 Dec 2023 23:33:45 -0300 Subject: [PATCH] feat: cart item live view --- .../live/cart_live/cart_live.ex | 9 +++++- .../live/cart_live/cart_live.html.heex | 4 +-- .../live/cart_live/details/details.ex | 1 + .../live/cart_live/details/details.html.heex | 28 ++--------------- .../live/cart_live/details/item/item.ex | 31 +++++++++++++++++++ 5 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 lib/food_order_web/live/cart_live/details/item/item.ex diff --git a/lib/food_order_web/live/cart_live/cart_live.ex b/lib/food_order_web/live/cart_live/cart_live.ex index 6a88a2d..512c9ed 100644 --- a/lib/food_order_web/live/cart_live/cart_live.ex +++ b/lib/food_order_web/live/cart_live/cart_live.ex @@ -1,9 +1,16 @@ defmodule FoodOrderWeb.CartLive do use FoodOrderWeb, :live_view + + alias FoodOrder.{Carts, Products} alias FoodOrderWeb.CartLive.Details def mount(_, _, socket) do - {:ok, assign(socket, total_quantity: 0)} + uuid = Ecto.UUID.generate() + Carts.create(uuid) + product = Products.list_products() |> hd + Carts.add(uuid, product) + cart = Carts.get(uuid) + {:ok, assign(socket, cart: cart)} end defp empty_cart(assigns) do diff --git a/lib/food_order_web/live/cart_live/cart_live.html.heex b/lib/food_order_web/live/cart_live/cart_live.html.heex index a304e09..ecca13e 100644 --- a/lib/food_order_web/live/cart_live/cart_live.html.heex +++ b/lib/food_order_web/live/cart_live/cart_live.html.heex @@ -1,7 +1,7 @@
- <%= if @total_quantity == 0 do %> + <%= if @cart.total_qty == 0 do %> <.empty_cart /> <% else %> - <.live_component module={Details} id="cart-details" /> + <.live_component module={Details} id="cart-details" cart={@cart} /> <% end %>
diff --git a/lib/food_order_web/live/cart_live/details/details.ex b/lib/food_order_web/live/cart_live/details/details.ex index 88ade45..b83c676 100644 --- a/lib/food_order_web/live/cart_live/details/details.ex +++ b/lib/food_order_web/live/cart_live/details/details.ex @@ -1,3 +1,4 @@ defmodule FoodOrderWeb.CartLive.Details do use FoodOrderWeb, :live_component + alias __MODULE__.Item end diff --git a/lib/food_order_web/live/cart_live/details/details.html.heex b/lib/food_order_web/live/cart_live/details/details.html.heex index 3d23bc1..88528df 100644 --- a/lib/food_order_web/live/cart_live/details/details.html.heex +++ b/lib/food_order_web/live/cart_live/details/details.html.heex @@ -5,36 +5,14 @@
-
- + <.live_component :for={item <- @cart.items} module={Item} id={item.item.id} item={item} /> -
-

Pizza

- Small -
- -
-
- - 10 Item(s) - -
-
- -
- $100 - -
-
- -
+
Total Amount: - $1000 + <%= @cart.total_price %>
diff --git a/lib/food_order_web/live/cart_live/details/item/item.ex b/lib/food_order_web/live/cart_live/details/item/item.ex new file mode 100644 index 0000000..7aca15d --- /dev/null +++ b/lib/food_order_web/live/cart_live/details/item/item.ex @@ -0,0 +1,31 @@ +defmodule FoodOrderWeb.CartLive.Details.Item do + use FoodOrderWeb, :live_component + + def render(assigns) do + ~H""" +
+ + +
+

<%= @item.item.name %>

+ <%= @item.item.size %> +
+ +
+
+ + <%= @item.qty %> Item(s) + +
+
+ +
+ <%= @item.item.price %> + +
+
+ """ + end +end