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
|
# 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 """
|
@doc """
|
||||||
Returns the total item count in the cart.
|
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
|
def on_mount(:mount_cart, _params, session, socket) do
|
||||||
cart_items = Cart.get_from_session(session)
|
cart_items = Cart.get_from_session(session)
|
||||||
hydrated = Cart.hydrate(cart_items)
|
|
||||||
|
|
||||||
socket =
|
socket =
|
||||||
socket
|
socket
|
||||||
|> assign(:raw_cart, cart_items)
|
|> update_cart_assigns(cart_items)
|
||||||
|> assign(:cart_items, hydrated)
|
|
||||||
|> assign(:cart_count, Cart.item_count(cart_items))
|
|
||||||
|> assign(:cart_subtotal, Cart.format_subtotal(hydrated))
|
|
||||||
|> assign(:cart_drawer_open, false)
|
|> assign(:cart_drawer_open, false)
|
||||||
|> assign(:cart_status, nil)
|
|> assign(:cart_status, nil)
|
||||||
|> attach_hook(:cart_events, :handle_event, &handle_cart_event/3)
|
|> 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.
|
Updates all cart-related assigns from raw cart data.
|
||||||
"""
|
"""
|
||||||
def update_cart_assigns(socket, cart) do
|
def update_cart_assigns(socket, cart) do
|
||||||
hydrated = Cart.hydrate(cart)
|
%{items: items, count: count, subtotal: subtotal} = Cart.build_state(cart)
|
||||||
|
|
||||||
socket
|
socket
|
||||||
|> assign(:raw_cart, cart)
|
|> assign(:raw_cart, cart)
|
||||||
|> assign(:cart_items, hydrated)
|
|> assign(:cart_items, items)
|
||||||
|> assign(:cart_count, Cart.item_count(cart))
|
|> assign(:cart_count, count)
|
||||||
|> assign(:cart_subtotal, Cart.format_subtotal(hydrated))
|
|> assign(:cart_subtotal, subtotal)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user