diff --git a/PROGRESS.md b/PROGRESS.md index 0a011ab..61167a5 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -48,7 +48,7 @@ Issues found during hands-on testing of the deployed prod site on mobile and des - [ ] Real product variants need testing and refinement with live data ### Cart -- [ ] Should be able to change quantity in the cart drawer (currently only on cart page?) +- [x] Should be able to change quantity in the cart drawer (currently only on cart page?) - [ ] Cart drawer button → "view basket" feels redundant — streamline the flow ### Navigation & links diff --git a/lib/simpleshop_theme_web/cart_hook.ex b/lib/simpleshop_theme_web/cart_hook.ex index 895a8b9..a5770f2 100644 --- a/lib/simpleshop_theme_web/cart_hook.ex +++ b/lib/simpleshop_theme_web/cart_hook.ex @@ -8,10 +8,11 @@ defmodule SimpleshopThemeWeb.CartHook do Handles these events so individual LiveViews don't have to: - `open_cart_drawer` / `close_cart_drawer` - toggle drawer visibility - `remove_item` - remove item from cart + - `increment` / `decrement` - change item quantity - `{:cart_updated, cart}` info - cross-tab cart sync via PubSub - LiveViews with custom cart logic (add_to_cart, increment, decrement) - can call `update_cart_assigns/2` and `broadcast_and_update/2` directly. + LiveViews with custom cart logic (e.g. add_to_cart) can call + `update_cart_assigns/2` and `broadcast_and_update/2` directly. """ import Phoenix.Component, only: [assign: 3] @@ -64,6 +65,31 @@ defmodule SimpleshopThemeWeb.CartHook do {:halt, socket} end + defp handle_cart_event("increment", %{"id" => variant_id}, socket) do + cart = Cart.add_item(socket.assigns.raw_cart, variant_id, 1) + new_qty = Cart.get_quantity(cart, variant_id) + + socket = + socket + |> broadcast_and_update(cart) + |> assign(:cart_status, "Quantity updated to #{new_qty}") + + {:halt, socket} + end + + defp handle_cart_event("decrement", %{"id" => variant_id}, socket) do + current = Cart.get_quantity(socket.assigns.raw_cart, variant_id) + cart = Cart.update_quantity(socket.assigns.raw_cart, variant_id, current - 1) + new_qty = Cart.get_quantity(cart, variant_id) + + socket = + socket + |> broadcast_and_update(cart) + |> assign(:cart_status, "Quantity updated to #{new_qty}") + + {:halt, socket} + end + defp handle_cart_event(_event, _params, socket), do: {:cont, socket} # Shared info handlers diff --git a/lib/simpleshop_theme_web/components/shop_components/cart.ex b/lib/simpleshop_theme_web/components/shop_components/cart.ex index 738e0da..58138b4 100644 --- a/lib/simpleshop_theme_web/components/shop_components/cart.ex +++ b/lib/simpleshop_theme_web/components/shop_components/cart.ex @@ -105,7 +105,7 @@ defmodule SimpleshopThemeWeb.ShopComponents.Cart do