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:
@@ -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.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user