Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions guides/data_modelling/cross_context_boundaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,23 +461,25 @@ We created a view to render our `show.html` template and aliased our `ShoppingCa
Next we can create the template at `lib/hello_web/controllers/cart_html/show.html.heex`:

```heex
<.header>
My Cart
<:subtitle :if={@cart.items == []}>Your cart is empty</:subtitle>
</.header>

<div :if={@cart.items !== []}>
<.form :let={f} for={@changeset} action={~p"/cart"}>
<.inputs_for :let={%{data: item} = item_form} field={f[:items]}>
<.input field={item_form[:quantity]} type="number" label={item.product.title} />
{currency_to_str(ShoppingCart.total_item_price(item))}
</.inputs_for>
<.button>Update cart</.button>
</.form>
<b>Total</b>: {currency_to_str(ShoppingCart.total_cart_price(@cart))}
</div>

<.button navigate={~p"/products"}>Back to products</.button>
<Layouts.app flash={@flash}>
<.header>
My Cart
<:subtitle :if={@cart.items == []}>Your cart is empty</:subtitle>
</.header>

<div :if={@cart.items !== []}>
<.form :let={f} for={@changeset} action={~p"/cart"}>
<.inputs_for :let={%{data: item} = item_form} field={f[:items]}>
<.input field={item_form[:quantity]} type="number" label={item.product.title} />
{currency_to_str(ShoppingCart.total_item_price(item))}
</.inputs_for>
<.button>Update cart</.button>
</.form>
<b>Total</b>: {currency_to_str(ShoppingCart.total_cart_price(@cart))}
</div>

<.button navigate={~p"/products"}>Back to products</.button>
</Layouts.app>
```

We started by showing the empty cart message if our preloaded `cart.items` is empty. If we have items, we use the `form` component provided by our `HelloWeb.CoreComponents` to take our cart changeset that we assigned in the `CartController.show/2` action and create a form which maps to our cart controller `update/2` action. Within the form, we use the [`inputs_for`](https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#inputs_for/1) component to render inputs for the nested cart items. This will allow us to map item inputs back together when the form is submitted. Next, we display a number input for the item quantity and label it with the product title. We finish the item form by converting the item price to string. We haven't written the `ShoppingCart.total_item_price/1` function yet, but again we employed the idea of clear, descriptive public interfaces for our contexts. After rendering inputs for all the cart items, we show an "update cart" submit button, along with the total price of the entire cart. This is accomplished with another new `ShoppingCart.total_cart_price/1` function which we'll implement in a moment. Finally, we added a `back` component to go back to our products page.
Expand Down Expand Up @@ -547,7 +549,7 @@ Head back over to your shopping cart context in `lib/hello/shopping_cart.ex` and
|> Repo.transact()
|> case do
{:ok, %{cart: cart}} ->
broadcast(scope, {:updated, cart})
broadcast_cart(scope, {:updated, cart})
{:ok, cart}

{:error, :cart, changeset, _changes_so_far} ->
Expand Down
42 changes: 22 additions & 20 deletions guides/data_modelling/more_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ From our requirements alone, we can start to see why a generic `create_order` fu
|> Repo.transact()
|> case do
{:ok, %{order: order}} ->
broadcast(scope, {:created, order})
broadcast_order(scope, {:created, order})
{:ok, order}

{:error, name, value, _changes_so_far} ->
Expand Down Expand Up @@ -266,25 +266,27 @@ end
Next we can create the template at `lib/hello_web/controllers/order_html/show.html.heex`:

```heex
<.header>
Thank you for your order!
<:subtitle>
<strong>Email: </strong>{@current_scope.user.email}
</:subtitle>
</.header>

<.table id="items" rows={@order.line_items}>
<:col :let={item} label="Title">{item.product.title}</:col>
<:col :let={item} label="Quantity">{item.quantity}</:col>
<:col :let={item} label="Price">
{HelloWeb.CartHTML.currency_to_str(item.price)}
</:col>
</.table>

<strong>Total price:</strong>
{HelloWeb.CartHTML.currency_to_str(@order.total_price)}

<.button navigate={~p"/products"}>Back to products</.button>
<Layouts.app flash={@flash}>
<.header>
Thank you for your order!
<:subtitle>
<strong>Email: </strong>{@current_scope.user.email}
</:subtitle>
</.header>

<.table id="items" rows={@order.line_items}>
<:col :let={item} label="Title">{item.product.title}</:col>
<:col :let={item} label="Quantity">{item.quantity}</:col>
<:col :let={item} label="Price">
{HelloWeb.CartHTML.currency_to_str(item.price)}
</:col>
</.table>

<strong>Total price:</strong>
{HelloWeb.CartHTML.currency_to_str(@order.total_price)}

<.button navigate={~p"/products"}>Back to products</.button>
</Layouts.app>
```

To show our completed order, we displayed the order's user, followed by the line item listing with product title, quantity, and the price we "transacted" when completing the order, along with the total price.
Expand Down