cannery/lib/cannery_web/components/ammo_group_card.ex

92 lines
3.0 KiB
Elixir
Raw Normal View History

2022-02-11 22:47:33 -05:00
defmodule CanneryWeb.Components.AmmoGroupCard do
2022-02-05 01:59:40 -05:00
@moduledoc """
Display card for an ammo group
"""
use CanneryWeb, :component
2022-11-09 21:19:55 -05:00
alias Cannery.{Ammo, Ammo.AmmoGroup, Repo}
2022-02-05 01:59:40 -05:00
alias CanneryWeb.Endpoint
2022-11-09 21:19:55 -05:00
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)
2022-02-05 01:59:40 -05:00
~H"""
<div
id={"ammo_group-#{@ammo_group.id}"}
2022-02-15 18:13:33 -05:00
class="mx-4 my-2 px-8 py-4 flex flex-col justify-center items-center
2022-02-15 23:20:18 -05:00
border border-gray-400 rounded-lg shadow-lg hover:shadow-md
transition-all duration-300 ease-in-out"
2022-02-05 01:59:40 -05:00
>
2022-11-09 23:47:11 -05:00
<.link navigate={Routes.ammo_group_show_path(Endpoint, :show, @ammo_group)} class="mb-2 link">
2022-02-05 01:59:40 -05:00
<h1 class="title text-xl title-primary-500">
<%= @ammo_group.ammo_type.name %>
</h1>
</.link>
2022-02-05 01:59:40 -05:00
<div class="flex flex-col justify-center items-center">
<span class="rounded-lg title text-lg">
2022-02-09 00:20:04 -05:00
<%= gettext("Count:") %>
<%= if @ammo_group.count == 0, do: "Empty", else: @ammo_group.count %>
2022-02-05 01:59:40 -05:00
</span>
<%= if @ammo_group.notes do %>
<span class="rounded-lg title text-lg">
2022-02-09 00:20:04 -05:00
<%= gettext("Notes:") %>
<%= @ammo_group.notes %>
2022-02-05 01:59:40 -05:00
</span>
<% end %>
2022-05-05 21:43:03 -04:00
<span class="rounded-lg title text-lg">
<%= gettext("Added on:") %>
<%= @ammo_group.inserted_at |> display_datetime() %>
</span>
2022-11-07 01:36:28 -05:00
<%= if @ammo_group.count == 0 do %>
<span class="rounded-lg title text-lg">
<%= gettext("Used up on:") %>
<%= @ammo_group |> Ammo.get_last_used_shot_group() |> Map.get(:date) |> display_date() %>
</span>
<% end %>
2022-02-05 01:59:40 -05:00
<%= if @ammo_group.price_paid do %>
<span class="rounded-lg title text-lg">
<%= gettext("Price paid:") %>
2022-02-17 21:24:59 -05:00
<%= gettext("$%{amount}",
amount: @ammo_group.price_paid |> :erlang.float_to_binary(decimals: 2)
) %>
2022-02-05 01:59:40 -05:00
</span>
<% end %>
<%= if @show_container and @ammo_group.container do %>
<span class="rounded-lg title text-lg">
<%= gettext("Container:") %>
<.link
2022-11-09 23:47:11 -05:00
navigate={Routes.container_show_path(Endpoint, :show, @ammo_group.container)}
class="link"
>
<%= @ammo_group.container.name %>
</.link>
</span>
<% end %>
2022-02-05 01:59:40 -05:00
</div>
2022-02-15 17:33:45 -05:00
<%= if assigns |> Map.has_key?(:inner_block) do %>
<div class="mt-4 flex space-x-4 justify-center items-center">
<%= render_slot(@inner_block) %>
</div>
<% end %>
2022-02-05 01:59:40 -05:00
</div>
"""
end
end