refactor: extract Cart.build_state/1 as single source of truth for cart state
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -21,14 +21,10 @@ defmodule SimpleshopThemeWeb.CartHook do
|
||||
|
||||
def on_mount(:mount_cart, _params, session, socket) do
|
||||
cart_items = Cart.get_from_session(session)
|
||||
hydrated = Cart.hydrate(cart_items)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(:raw_cart, cart_items)
|
||||
|> assign(:cart_items, hydrated)
|
||||
|> assign(:cart_count, Cart.item_count(cart_items))
|
||||
|> assign(:cart_subtotal, Cart.format_subtotal(hydrated))
|
||||
|> update_cart_assigns(cart_items)
|
||||
|> assign(:cart_drawer_open, false)
|
||||
|> assign(:cart_status, nil)
|
||||
|> attach_hook(:cart_events, :handle_event, &handle_cart_event/3)
|
||||
@@ -84,13 +80,13 @@ defmodule SimpleshopThemeWeb.CartHook do
|
||||
Updates all cart-related assigns from raw cart data.
|
||||
"""
|
||||
def update_cart_assigns(socket, cart) do
|
||||
hydrated = Cart.hydrate(cart)
|
||||
%{items: items, count: count, subtotal: subtotal} = Cart.build_state(cart)
|
||||
|
||||
socket
|
||||
|> assign(:raw_cart, cart)
|
||||
|> assign(:cart_items, hydrated)
|
||||
|> assign(:cart_count, Cart.item_count(cart))
|
||||
|> assign(:cart_subtotal, Cart.format_subtotal(hydrated))
|
||||
|> assign(:cart_items, items)
|
||||
|> assign(:cart_count, count)
|
||||
|> assign(:cart_subtotal, subtotal)
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
||||
Reference in New Issue
Block a user