defmodule CanneryWeb.Components.AmmoGroupCard do
@moduledoc """
Display card for an ammo group
"""
use CanneryWeb, :component
alias Cannery.{Ammo, Repo}
alias CanneryWeb.Endpoint
def ammo_group_card(%{ammo_group: ammo_group} = assigns) do
assigns =
%{show_container: show_container} = assigns |> assign_new(:show_container, fn -> false end)
preloads = if show_container, do: [:ammo_type, :container], else: [:ammo_type]
ammo_group = ammo_group |> Repo.preload(preloads)
assigns = assigns |> assign(:ammo_group, ammo_group)
~H"""
<%= live_patch to: Routes.ammo_group_show_path(Endpoint, :show, @ammo_group),
class: "mb-2 link" do %>
<%= @ammo_group.ammo_type.name %>
<% end %>
<%= gettext("Count:") %>
<%= if @ammo_group.count == 0, do: "Empty", else: @ammo_group.count %>
<%= if @ammo_group.notes do %>
<%= gettext("Notes:") %>
<%= @ammo_group.notes %>
<% end %>
<%= gettext("Added on:") %>
<%= @ammo_group.inserted_at |> display_datetime() %>
<%= if @ammo_group.count == 0 do %>
<%= gettext("Used up on:") %>
<%= @ammo_group |> Ammo.get_last_used_shot_group() |> Map.get(:date) |> display_date() %>
<% end %>
<%= if @ammo_group.price_paid do %>
<%= gettext("Price paid:") %>
<%= gettext("$%{amount}",
amount: @ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2)
) %>
<% end %>
<%= if @show_container and @ammo_group.container do %>
<%= gettext("Container:") %>
<%= live_patch to: Routes.container_show_path(Endpoint, :show, @ammo_group.container),
class: "link" do %>
<%= @ammo_group.container.name %>
<% end %>
<% end %>
<%= if assigns |> Map.has_key?(:inner_block) do %>
<%= render_slot(@inner_block) %>
<% end %>
"""
end
end