defmodule CanneryWeb.Components.AmmoGroupCard do @moduledoc """ Display card for an ammo group """ use CanneryWeb, :component alias Cannery.{Ammo, Ammo.AmmoGroup, Repo} alias CanneryWeb.Endpoint attr :ammo_group, AmmoGroup, required: true attr :show_container, :boolean, default: false slot(:inner_block) 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"""
<.link navigate={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} class="mb-2 link">

<%= @ammo_group.ammo_type.name %>

<%= gettext("Count:") %> <%= if @ammo_group.count == 0, do: gettext("Empty"), else: @ammo_group.count %> Ammo.get_original_count() != @ammo_group.count} class="rounded-lg title text-lg" > <%= gettext("Original Count:") %> <%= @ammo_group |> Ammo.get_original_count() %> <%= gettext("Notes:") %> <%= @ammo_group.notes %> <%= gettext("Purchased on:") %> <.date date={@ammo_group.purchased_on} /> Ammo.get_last_used_shot_group()} class="rounded-lg title text-lg"> <%= gettext("Last used on:") %> <.date date={@ammo_group |> Ammo.get_last_used_shot_group() |> Map.get(:date)} /> <%= if @ammo_group.price_paid do %> <%= gettext("Price paid:") %> <%= gettext("$%{amount}", amount: @ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2) ) %> <%= gettext("CPR:") %> <%= gettext("$%{amount}", amount: @ammo_group |> Ammo.get_cpr() |> :erlang.float_to_binary(decimals: 2) ) %> <% end %> <%= gettext("Container:") %> <.link navigate={Routes.container_show_path(Endpoint, :show, @ammo_group.container)} class="link" > <%= @ammo_group.container.name %>
Map.has_key?(:inner_block)} class="mt-4 flex space-x-4 justify-center items-center" > <%= render_slot(@inner_block) %>
""" end end