LV.html
<%= if @live_action in [:edit] do %>
<%= live_modal @socket, ExWeb.Stock.InventoryLive.FormComponent,
id: @inventory.id,
title: @page_title,
action: @live_action,
inventory: @inventory,
return_to: Routes.inventory_index_path(@socket, :index, @uuid) %>
<% end %>
<%= live_component @socket, ExWeb.Stock.InventoryLive.AddComponent, id: "add" %>
<table class="table is-fullwidth is-hoverable is-striped">
<thead>
<tr>
<th>Favourite</th>
<th>Add</th>
<th>Name</th>
<th>Unit</th>
<th>Description</th>
<th>Last bought</th>
<th></th>
</tr>
</thead>
<tbody id="inventories" phx-update="prepend">
<%= for inventory <- @inventories do %>
<tr id="inventory-<%= inventory.id %>" <%= if inventory.__meta__.state == :deleted do %>class="is-hidden"<% end %>>
<td>
<%= link to: "#", phx_click: "toggle-fav", phx_value_id: inventory.id do %>
<%= if inventory.is_fav do %>
<span class="icon has-text-warning has-tooltip-arrow" data-tooltip="Remove from favorites">
<i class="fas fa-star fa-lg"></i>
<% else %>
<span class="icon has-tooltip-arrow" data-tooltip="Add to favorites">
<i class="far fa-star fa-lg"></i>
<% end %>
<% end %>
</td>
<td>
<%= link to: "#", phx_click: "cart", phx_value_id: inventory.id do %>
<span class="icon has-tooltip-arrow" data-tooltip="Add to current shopping list">
<i class="fas fa-cart-plus fa-lg"></i>
<% end %>
</td>
<td><%= inventory.name %></td>
<td><%= inventory.unit %></td>
<td><%= inventory.description %></td>
<td><%= inventory.last_bought %></td>
<td>
<%= live_patch to: Routes.inventory_index_path(@socket, :edit, @uuid, inventory) do %>
<span class="icon has-tooltip-arrow" data-tooltip="Edit">
<i class="far fa-edit"></i>
</span>
<% end %>
<%= link to: "#", phx_click: "delete", phx_value_id: inventory.id, data: [confirm: "Are you sure?"] do %>
<span class="icon has-tooltip-arrow" data-tooltip="Delete">
<i class="far fa-trash-alt"></i>
</span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
Component
defmodule ExWeb.Stock.InventoryLive.AddComponent do
use ExWeb, :live_component
alias Ex.Stock
alias Ex.Stock.Inventory
@impl true
def mount(socket) do
changeset = Stock.change_inventory(%Inventory{})
{:ok,
socket
|> assign(:units, Stock.units())
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("add", %{"inventory" => inventory_params}, socket) do
case Stock.create_inventory(inventory_params) do
{:ok, inventory} ->
send(self(), {:new, inventory})
# why the fuck are you not setting?
{:noreply, assign(socket, changeset: Stock.change_inventory(%Inventory{}))}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
@impl true
def render(assigns) do
~L"""
<%= f = form_for @changeset, "#",
id: "add-inventory-form",
phx_target: @myself,
phx_submit: "add" %>
<%= label f, :unit %>
<%= select f, :unit, @units %>
<%= error_tag f, :unit %>
<%= label f, :name %>
<%= text_input f, :name, required: true %>
<%= error_tag f, :name %>
<%= label f, :description %>
<%= text_input f, :description %>
<%= error_tag f, :description %>
<%= submit "WTF phoenix!!!", phx_disable_with: "Saving..." %>
</form>
"""
end
end