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:
jamey
2026-02-08 12:24:39 +00:00
parent 2825537136
commit cb4698bec8
2 changed files with 22 additions and 9 deletions

View File

@@ -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.
"""