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:
parent
2825537136
commit
cb4698bec8
@ -166,6 +166,23 @@ defmodule SimpleshopTheme.Cart do
|
||||
# Helpers
|
||||
# =============================================================================
|
||||
|
||||
@doc """
|
||||
Builds the full display state for a cart.
|
||||
|
||||
Takes raw cart items (list of {variant_id, quantity} tuples) and returns
|
||||
a map with hydrated items, count, and formatted subtotal. Single source
|
||||
of truth for cart state computation — used by CartHook.
|
||||
"""
|
||||
def build_state(raw_cart) do
|
||||
hydrated = hydrate(raw_cart)
|
||||
|
||||
%{
|
||||
items: hydrated,
|
||||
count: item_count(raw_cart),
|
||||
subtotal: format_subtotal(hydrated)
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the total item count in the cart.
|
||||
"""
|
||||
|
||||
@ -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 """
|
||||
|
||||
Loading…
Reference in New Issue
Block a user