forked from shibao/cannery
		
	rename ammo type to type
This commit is contained in:
		@@ -1,9 +1,9 @@
 | 
			
		||||
defmodule CanneryWeb.Components.AmmoTypeTableComponent do
 | 
			
		||||
defmodule CanneryWeb.Components.TypeTableComponent do
 | 
			
		||||
  @moduledoc """
 | 
			
		||||
  A component that displays a list of ammo type
 | 
			
		||||
  A component that displays a list of types
 | 
			
		||||
  """
 | 
			
		||||
  use CanneryWeb, :live_component
 | 
			
		||||
  alias Cannery.{Accounts.User, ActivityLog, Ammo, Ammo.AmmoType}
 | 
			
		||||
  alias Cannery.{Accounts.User, ActivityLog, Ammo, Ammo.Type}
 | 
			
		||||
  alias CanneryWeb.Components.TableComponent
 | 
			
		||||
  alias Ecto.UUID
 | 
			
		||||
  alias Phoenix.LiveView.{Rendered, Socket}
 | 
			
		||||
@@ -13,30 +13,30 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do
 | 
			
		||||
          %{
 | 
			
		||||
            required(:id) => UUID.t(),
 | 
			
		||||
            required(:current_user) => User.t(),
 | 
			
		||||
            optional(:class) => AmmoType.class() | nil,
 | 
			
		||||
            optional(:class) => Type.class() | nil,
 | 
			
		||||
            optional(:show_used) => boolean(),
 | 
			
		||||
            optional(:ammo_types) => [AmmoType.t()],
 | 
			
		||||
            optional(:types) => [Type.t()],
 | 
			
		||||
            optional(:actions) => Rendered.t(),
 | 
			
		||||
            optional(any()) => any()
 | 
			
		||||
          },
 | 
			
		||||
          Socket.t()
 | 
			
		||||
        ) :: {:ok, Socket.t()}
 | 
			
		||||
  def update(%{id: _id, ammo_types: _ammo_types, current_user: _current_user} = assigns, socket) do
 | 
			
		||||
  def update(%{id: _id, types: _types, current_user: _current_user} = assigns, socket) do
 | 
			
		||||
    socket =
 | 
			
		||||
      socket
 | 
			
		||||
      |> assign(assigns)
 | 
			
		||||
      |> assign_new(:show_used, fn -> false end)
 | 
			
		||||
      |> assign_new(:class, fn -> :all end)
 | 
			
		||||
      |> assign_new(:actions, fn -> [] end)
 | 
			
		||||
      |> display_ammo_types()
 | 
			
		||||
      |> display_types()
 | 
			
		||||
 | 
			
		||||
    {:ok, socket}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp display_ammo_types(
 | 
			
		||||
  defp display_types(
 | 
			
		||||
         %{
 | 
			
		||||
           assigns: %{
 | 
			
		||||
             ammo_types: ammo_types,
 | 
			
		||||
             types: types,
 | 
			
		||||
             current_user: current_user,
 | 
			
		||||
             show_used: show_used,
 | 
			
		||||
             class: class,
 | 
			
		||||
@@ -92,8 +92,8 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do
 | 
			
		||||
        # remove columns if all values match defaults
 | 
			
		||||
        default_value = if type == :atom, do: false, else: nil
 | 
			
		||||
 | 
			
		||||
        ammo_types
 | 
			
		||||
        |> Enum.any?(fn ammo_type -> Map.get(ammo_type, key, default_value) != default_value end)
 | 
			
		||||
        types
 | 
			
		||||
        |> Enum.any?(fn type -> Map.get(type, key, default_value) != default_value end)
 | 
			
		||||
      end)
 | 
			
		||||
 | 
			
		||||
    columns =
 | 
			
		||||
@@ -152,17 +152,17 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do
 | 
			
		||||
      )
 | 
			
		||||
      |> TableComponent.maybe_compose_columns(%{label: gettext("Name"), key: :name, type: :name})
 | 
			
		||||
 | 
			
		||||
    round_counts = ammo_types |> Ammo.get_round_count_for_ammo_types(current_user)
 | 
			
		||||
    packs_count = ammo_types |> Ammo.get_packs_count_for_types(current_user)
 | 
			
		||||
    average_costs = ammo_types |> Ammo.get_average_cost_for_ammo_types(current_user)
 | 
			
		||||
    round_counts = types |> Ammo.get_round_count_for_types(current_user)
 | 
			
		||||
    packs_count = types |> Ammo.get_packs_count_for_types(current_user)
 | 
			
		||||
    average_costs = types |> Ammo.get_average_cost_for_types(current_user)
 | 
			
		||||
 | 
			
		||||
    [used_counts, historical_round_counts, historical_pack_counts, used_pack_counts] =
 | 
			
		||||
      if show_used do
 | 
			
		||||
        [
 | 
			
		||||
          ammo_types |> ActivityLog.get_used_count_for_ammo_types(current_user),
 | 
			
		||||
          ammo_types |> Ammo.get_historical_count_for_ammo_types(current_user),
 | 
			
		||||
          ammo_types |> Ammo.get_packs_count_for_types(current_user, true),
 | 
			
		||||
          ammo_types |> Ammo.get_used_packs_count_for_types(current_user)
 | 
			
		||||
          types |> ActivityLog.get_used_count_for_types(current_user),
 | 
			
		||||
          types |> Ammo.get_historical_count_for_types(current_user),
 | 
			
		||||
          types |> Ammo.get_packs_count_for_types(current_user, true),
 | 
			
		||||
          types |> Ammo.get_used_packs_count_for_types(current_user)
 | 
			
		||||
        ]
 | 
			
		||||
      else
 | 
			
		||||
        [nil, nil, nil, nil]
 | 
			
		||||
@@ -181,9 +181,9 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    rows =
 | 
			
		||||
      ammo_types
 | 
			
		||||
      |> Enum.map(fn ammo_type ->
 | 
			
		||||
        ammo_type |> get_ammo_type_values(columns, extra_data)
 | 
			
		||||
      types
 | 
			
		||||
      |> Enum.map(fn type ->
 | 
			
		||||
        type |> get_type_values(columns, extra_data)
 | 
			
		||||
      end)
 | 
			
		||||
 | 
			
		||||
    socket |> assign(columns: columns, rows: rows)
 | 
			
		||||
@@ -198,92 +198,92 @@ defmodule CanneryWeb.Components.AmmoTypeTableComponent do
 | 
			
		||||
    """
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_values(ammo_type, columns, extra_data) do
 | 
			
		||||
  defp get_type_values(type, columns, extra_data) do
 | 
			
		||||
    columns
 | 
			
		||||
    |> Map.new(fn %{key: key, type: type} ->
 | 
			
		||||
      {key, get_ammo_type_value(type, key, ammo_type, extra_data)}
 | 
			
		||||
    |> Map.new(fn %{key: key, type: column_type} ->
 | 
			
		||||
      {key, get_type_value(column_type, key, type, extra_data)}
 | 
			
		||||
    end)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(:atom, key, ammo_type, _other_data),
 | 
			
		||||
    do: ammo_type |> Map.get(key) |> humanize()
 | 
			
		||||
  defp get_type_value(:atom, key, type, _other_data),
 | 
			
		||||
    do: type |> Map.get(key) |> humanize()
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(:round_count, _key, %{id: ammo_type_id}, %{round_counts: round_counts}),
 | 
			
		||||
    do: Map.get(round_counts, ammo_type_id, 0)
 | 
			
		||||
  defp get_type_value(:round_count, _key, %{id: type_id}, %{round_counts: round_counts}),
 | 
			
		||||
    do: Map.get(round_counts, type_id, 0)
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(
 | 
			
		||||
  defp get_type_value(
 | 
			
		||||
         :historical_round_count,
 | 
			
		||||
         _key,
 | 
			
		||||
         %{id: ammo_type_id},
 | 
			
		||||
         %{id: type_id},
 | 
			
		||||
         %{historical_round_counts: historical_round_counts}
 | 
			
		||||
       ) do
 | 
			
		||||
    Map.get(historical_round_counts, ammo_type_id, 0)
 | 
			
		||||
    Map.get(historical_round_counts, type_id, 0)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(
 | 
			
		||||
  defp get_type_value(
 | 
			
		||||
         :used_round_count,
 | 
			
		||||
         _key,
 | 
			
		||||
         %{id: ammo_type_id},
 | 
			
		||||
         %{id: type_id},
 | 
			
		||||
         %{used_counts: used_counts}
 | 
			
		||||
       ) do
 | 
			
		||||
    Map.get(used_counts, ammo_type_id, 0)
 | 
			
		||||
    Map.get(used_counts, type_id, 0)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(
 | 
			
		||||
  defp get_type_value(
 | 
			
		||||
         :historical_pack_count,
 | 
			
		||||
         _key,
 | 
			
		||||
         %{id: ammo_type_id},
 | 
			
		||||
         %{id: type_id},
 | 
			
		||||
         %{historical_pack_counts: historical_pack_counts}
 | 
			
		||||
       ) do
 | 
			
		||||
    Map.get(historical_pack_counts, ammo_type_id, 0)
 | 
			
		||||
    Map.get(historical_pack_counts, type_id, 0)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(
 | 
			
		||||
  defp get_type_value(
 | 
			
		||||
         :used_pack_count,
 | 
			
		||||
         _key,
 | 
			
		||||
         %{id: ammo_type_id},
 | 
			
		||||
         %{id: type_id},
 | 
			
		||||
         %{used_pack_counts: used_pack_counts}
 | 
			
		||||
       ) do
 | 
			
		||||
    Map.get(used_pack_counts, ammo_type_id, 0)
 | 
			
		||||
    Map.get(used_pack_counts, type_id, 0)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(:ammo_count, _key, %{id: ammo_type_id}, %{packs_count: packs_count}),
 | 
			
		||||
    do: Map.get(packs_count, ammo_type_id)
 | 
			
		||||
  defp get_type_value(:ammo_count, _key, %{id: type_id}, %{packs_count: packs_count}),
 | 
			
		||||
    do: Map.get(packs_count, type_id)
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(
 | 
			
		||||
  defp get_type_value(
 | 
			
		||||
         :avg_price_paid,
 | 
			
		||||
         _key,
 | 
			
		||||
         %{id: ammo_type_id},
 | 
			
		||||
         %{id: type_id},
 | 
			
		||||
         %{average_costs: average_costs}
 | 
			
		||||
       ) do
 | 
			
		||||
    case Map.get(average_costs, ammo_type_id) do
 | 
			
		||||
    case Map.get(average_costs, type_id) do
 | 
			
		||||
      nil -> {0, gettext("No cost information")}
 | 
			
		||||
      count -> {count, gettext("$%{amount}", amount: display_currency(count))}
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(:name, _key, %{name: ammo_type_name} = ammo_type, _other_data) do
 | 
			
		||||
    assigns = %{ammo_type: ammo_type}
 | 
			
		||||
  defp get_type_value(:name, _key, %{name: type_name} = type, _other_data) do
 | 
			
		||||
    assigns = %{type: type}
 | 
			
		||||
 | 
			
		||||
    {ammo_type_name,
 | 
			
		||||
    {type_name,
 | 
			
		||||
     ~H"""
 | 
			
		||||
     <.link navigate={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)} class="link">
 | 
			
		||||
       <%= @ammo_type.name %>
 | 
			
		||||
     <.link navigate={Routes.type_show_path(Endpoint, :show, @type)} class="link">
 | 
			
		||||
       <%= @type.name %>
 | 
			
		||||
     </.link>
 | 
			
		||||
     """}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(:actions, _key, ammo_type, %{actions: actions}) do
 | 
			
		||||
    assigns = %{actions: actions, ammo_type: ammo_type}
 | 
			
		||||
  defp get_type_value(:actions, _key, type, %{actions: actions}) do
 | 
			
		||||
    assigns = %{actions: actions, type: type}
 | 
			
		||||
 | 
			
		||||
    ~H"""
 | 
			
		||||
    <%= render_slot(@actions, @ammo_type) %>
 | 
			
		||||
    <%= render_slot(@actions, @type) %>
 | 
			
		||||
    """
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(nil, _key, _ammo_type, _other_data), do: nil
 | 
			
		||||
  defp get_type_value(nil, _key, _type, _other_data), do: nil
 | 
			
		||||
 | 
			
		||||
  defp get_ammo_type_value(_other, key, ammo_type, _other_data), do: ammo_type |> Map.get(key)
 | 
			
		||||
  defp get_type_value(_other, key, type, _other_data), do: type |> Map.get(key)
 | 
			
		||||
 | 
			
		||||
  @spec display_currency(float()) :: String.t()
 | 
			
		||||
  defp display_currency(float), do: :erlang.float_to_binary(float, decimals: 2)
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@
 | 
			
		||||
>
 | 
			
		||||
  <.link navigate={Routes.pack_show_path(Endpoint, :show, @pack)} class="mb-2 link">
 | 
			
		||||
    <h1 class="title text-xl title-primary-500">
 | 
			
		||||
      <%= @pack.ammo_type.name %>
 | 
			
		||||
      <%= @pack.type.name %>
 | 
			
		||||
    </h1>
 | 
			
		||||
  </.link>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -44,7 +44,7 @@
 | 
			
		||||
        </li>
 | 
			
		||||
        <li class="mx-2 my-1">
 | 
			
		||||
          <.link
 | 
			
		||||
            navigate={Routes.ammo_type_index_path(Endpoint, :index)}
 | 
			
		||||
            navigate={Routes.type_index_path(Endpoint, :index)}
 | 
			
		||||
            class="text-white hover:underline"
 | 
			
		||||
          >
 | 
			
		||||
            <%= gettext("Catalog") %>
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,7 @@ defmodule CanneryWeb.Components.PackTableComponent do
 | 
			
		||||
            required(:current_user) => User.t(),
 | 
			
		||||
            required(:packs) => [Pack.t()],
 | 
			
		||||
            required(:show_used) => boolean(),
 | 
			
		||||
            optional(:ammo_type) => Rendered.t(),
 | 
			
		||||
            optional(:type) => Rendered.t(),
 | 
			
		||||
            optional(:range) => Rendered.t(),
 | 
			
		||||
            optional(:container) => Rendered.t(),
 | 
			
		||||
            optional(:actions) => Rendered.t(),
 | 
			
		||||
@@ -31,7 +31,7 @@ defmodule CanneryWeb.Components.PackTableComponent do
 | 
			
		||||
    socket =
 | 
			
		||||
      socket
 | 
			
		||||
      |> assign(assigns)
 | 
			
		||||
      |> assign_new(:ammo_type, fn -> [] end)
 | 
			
		||||
      |> assign_new(:type, fn -> [] end)
 | 
			
		||||
      |> assign_new(:range, fn -> [] end)
 | 
			
		||||
      |> assign_new(:container, fn -> [] end)
 | 
			
		||||
      |> assign_new(:actions, fn -> [] end)
 | 
			
		||||
@@ -45,7 +45,7 @@ defmodule CanneryWeb.Components.PackTableComponent do
 | 
			
		||||
           assigns: %{
 | 
			
		||||
             packs: packs,
 | 
			
		||||
             current_user: current_user,
 | 
			
		||||
             ammo_type: ammo_type,
 | 
			
		||||
             type: type,
 | 
			
		||||
             range: range,
 | 
			
		||||
             container: container,
 | 
			
		||||
             actions: actions,
 | 
			
		||||
@@ -92,8 +92,8 @@ defmodule CanneryWeb.Components.PackTableComponent do
 | 
			
		||||
        key: :count
 | 
			
		||||
      })
 | 
			
		||||
      |> TableComponent.maybe_compose_columns(
 | 
			
		||||
        %{label: gettext("Ammo type"), key: :ammo_type},
 | 
			
		||||
        ammo_type != []
 | 
			
		||||
        %{label: gettext("Type"), key: :type},
 | 
			
		||||
        type != []
 | 
			
		||||
      )
 | 
			
		||||
 | 
			
		||||
    containers =
 | 
			
		||||
@@ -103,7 +103,7 @@ defmodule CanneryWeb.Components.PackTableComponent do
 | 
			
		||||
 | 
			
		||||
    extra_data = %{
 | 
			
		||||
      current_user: current_user,
 | 
			
		||||
      ammo_type: ammo_type,
 | 
			
		||||
      type: type,
 | 
			
		||||
      columns: columns,
 | 
			
		||||
      container: container,
 | 
			
		||||
      containers: containers,
 | 
			
		||||
@@ -144,15 +144,15 @@ defmodule CanneryWeb.Components.PackTableComponent do
 | 
			
		||||
  @spec get_value_for_key(atom(), Pack.t(), additional_data :: map()) ::
 | 
			
		||||
          any() | {any(), Rendered.t()}
 | 
			
		||||
  defp get_value_for_key(
 | 
			
		||||
         :ammo_type,
 | 
			
		||||
         %{ammo_type: %{name: ammo_type_name} = ammo_type},
 | 
			
		||||
         %{ammo_type: ammo_type_block}
 | 
			
		||||
         :type,
 | 
			
		||||
         %{type: %{name: type_name} = type},
 | 
			
		||||
         %{type: type_block}
 | 
			
		||||
       ) do
 | 
			
		||||
    assigns = %{ammo_type: ammo_type, ammo_type_block: ammo_type_block}
 | 
			
		||||
    assigns = %{type: type, type_block: type_block}
 | 
			
		||||
 | 
			
		||||
    {ammo_type_name,
 | 
			
		||||
    {type_name,
 | 
			
		||||
     ~H"""
 | 
			
		||||
     <%= render_slot(@ammo_type_block, @ammo_type) %>
 | 
			
		||||
     <%= render_slot(@type_block, @type) %>
 | 
			
		||||
     """}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -96,10 +96,10 @@ defmodule CanneryWeb.Components.ShotRecordTableComponent do
 | 
			
		||||
  defp get_row_value(:name, %{pack_id: pack_id}, %{packs: packs}) do
 | 
			
		||||
    assigns = %{pack: pack = Map.fetch!(packs, pack_id)}
 | 
			
		||||
 | 
			
		||||
    {pack.ammo_type.name,
 | 
			
		||||
    {pack.type.name,
 | 
			
		||||
     ~H"""
 | 
			
		||||
     <.link navigate={Routes.pack_show_path(Endpoint, :show, @pack)} class="link">
 | 
			
		||||
       <%= @pack.ammo_type.name %>
 | 
			
		||||
       <%= @pack.type.name %>
 | 
			
		||||
     </.link>
 | 
			
		||||
     """}
 | 
			
		||||
  end
 | 
			
		||||
 
 | 
			
		||||
@@ -3,27 +3,27 @@ defmodule CanneryWeb.ExportController do
 | 
			
		||||
  alias Cannery.{ActivityLog, Ammo, Containers}
 | 
			
		||||
 | 
			
		||||
  def export(%{assigns: %{current_user: current_user}} = conn, %{"mode" => "json"}) do
 | 
			
		||||
    ammo_types = Ammo.list_ammo_types(current_user, :all)
 | 
			
		||||
    used_counts = ammo_types |> ActivityLog.get_used_count_for_ammo_types(current_user)
 | 
			
		||||
    round_counts = ammo_types |> Ammo.get_round_count_for_ammo_types(current_user)
 | 
			
		||||
    pack_counts = ammo_types |> Ammo.get_packs_count_for_types(current_user)
 | 
			
		||||
    types = Ammo.list_types(current_user, :all)
 | 
			
		||||
    used_counts = types |> ActivityLog.get_used_count_for_types(current_user)
 | 
			
		||||
    round_counts = types |> Ammo.get_round_count_for_types(current_user)
 | 
			
		||||
    pack_counts = types |> Ammo.get_packs_count_for_types(current_user)
 | 
			
		||||
 | 
			
		||||
    total_pack_counts = ammo_types |> Ammo.get_packs_count_for_types(current_user, true)
 | 
			
		||||
    total_pack_counts = types |> Ammo.get_packs_count_for_types(current_user, true)
 | 
			
		||||
 | 
			
		||||
    average_costs = ammo_types |> Ammo.get_average_cost_for_ammo_types(current_user)
 | 
			
		||||
    average_costs = types |> Ammo.get_average_cost_for_types(current_user)
 | 
			
		||||
 | 
			
		||||
    ammo_types =
 | 
			
		||||
      ammo_types
 | 
			
		||||
      |> Enum.map(fn %{id: ammo_type_id} = ammo_type ->
 | 
			
		||||
        ammo_type
 | 
			
		||||
    types =
 | 
			
		||||
      types
 | 
			
		||||
      |> Enum.map(fn %{id: type_id} = type ->
 | 
			
		||||
        type
 | 
			
		||||
        |> Jason.encode!()
 | 
			
		||||
        |> Jason.decode!()
 | 
			
		||||
        |> Map.merge(%{
 | 
			
		||||
          "average_cost" => Map.get(average_costs, ammo_type_id),
 | 
			
		||||
          "round_count" => Map.get(round_counts, ammo_type_id, 0),
 | 
			
		||||
          "used_count" => Map.get(used_counts, ammo_type_id, 0),
 | 
			
		||||
          "pack_count" => Map.get(pack_counts, ammo_type_id, 0),
 | 
			
		||||
          "total_pack_count" => Map.get(total_pack_counts, ammo_type_id, 0)
 | 
			
		||||
          "average_cost" => Map.get(average_costs, type_id),
 | 
			
		||||
          "round_count" => Map.get(round_counts, type_id, 0),
 | 
			
		||||
          "used_count" => Map.get(used_counts, type_id, 0),
 | 
			
		||||
          "pack_count" => Map.get(pack_counts, type_id, 0),
 | 
			
		||||
          "total_pack_count" => Map.get(total_pack_counts, type_id, 0)
 | 
			
		||||
        })
 | 
			
		||||
      end)
 | 
			
		||||
 | 
			
		||||
@@ -66,7 +66,7 @@ defmodule CanneryWeb.ExportController do
 | 
			
		||||
 | 
			
		||||
    json(conn, %{
 | 
			
		||||
      user: current_user,
 | 
			
		||||
      ammo_types: ammo_types,
 | 
			
		||||
      types: types,
 | 
			
		||||
      packs: packs,
 | 
			
		||||
      shot_records: shot_records,
 | 
			
		||||
      containers: containers
 | 
			
		||||
 
 | 
			
		||||
@@ -1,16 +1,16 @@
 | 
			
		||||
defmodule CanneryWeb.AmmoTypeLive.FormComponent do
 | 
			
		||||
defmodule CanneryWeb.TypeLive.FormComponent do
 | 
			
		||||
  @moduledoc """
 | 
			
		||||
  Livecomponent that can update or create an Cannery.Ammo.AmmoType
 | 
			
		||||
  Livecomponent that can update or create an Cannery.Ammo.Type
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb, :live_component
 | 
			
		||||
  alias Cannery.{Accounts.User, Ammo, Ammo.AmmoType}
 | 
			
		||||
  alias Cannery.{Accounts.User, Ammo, Ammo.Type}
 | 
			
		||||
  alias Ecto.Changeset
 | 
			
		||||
  alias Phoenix.LiveView.Socket
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
  @spec update(
 | 
			
		||||
          %{:ammo_type => AmmoType.t(), :current_user => User.t(), optional(any) => any},
 | 
			
		||||
          %{:type => Type.t(), :current_user => User.t(), optional(any) => any},
 | 
			
		||||
          Socket.t()
 | 
			
		||||
        ) :: {:ok, Socket.t()}
 | 
			
		||||
  def update(%{current_user: _current_user} = assigns, socket) do
 | 
			
		||||
@@ -18,21 +18,21 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
  def handle_event("validate", %{"ammo_type" => ammo_type_params}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign_changeset(ammo_type_params)}
 | 
			
		||||
  def handle_event("validate", %{"type" => type_params}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign_changeset(type_params)}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event(
 | 
			
		||||
        "save",
 | 
			
		||||
        %{"ammo_type" => ammo_type_params},
 | 
			
		||||
        %{"type" => type_params},
 | 
			
		||||
        %{assigns: %{action: action}} = socket
 | 
			
		||||
      ) do
 | 
			
		||||
    save_ammo_type(socket, action, ammo_type_params)
 | 
			
		||||
    save_type(socket, action, type_params)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp assign_changeset(
 | 
			
		||||
         %{assigns: %{action: action, ammo_type: ammo_type, current_user: user}} = socket,
 | 
			
		||||
         ammo_type_params
 | 
			
		||||
         %{assigns: %{action: action, type: type, current_user: user}} = socket,
 | 
			
		||||
         type_params
 | 
			
		||||
       ) do
 | 
			
		||||
    changeset_action =
 | 
			
		||||
      case action do
 | 
			
		||||
@@ -43,10 +43,10 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
 | 
			
		||||
    changeset =
 | 
			
		||||
      case action do
 | 
			
		||||
        create when create in [:new, :clone] ->
 | 
			
		||||
          ammo_type |> AmmoType.create_changeset(user, ammo_type_params)
 | 
			
		||||
          type |> Type.create_changeset(user, type_params)
 | 
			
		||||
 | 
			
		||||
        :edit ->
 | 
			
		||||
          ammo_type |> AmmoType.update_changeset(ammo_type_params)
 | 
			
		||||
          type |> Type.update_changeset(type_params)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
    changeset =
 | 
			
		||||
@@ -58,16 +58,15 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
 | 
			
		||||
    socket |> assign(changeset: changeset)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp save_ammo_type(
 | 
			
		||||
         %{assigns: %{ammo_type: ammo_type, current_user: current_user, return_to: return_to}} =
 | 
			
		||||
           socket,
 | 
			
		||||
  defp save_type(
 | 
			
		||||
         %{assigns: %{type: type, current_user: current_user, return_to: return_to}} = socket,
 | 
			
		||||
         :edit,
 | 
			
		||||
         ammo_type_params
 | 
			
		||||
         type_params
 | 
			
		||||
       ) do
 | 
			
		||||
    socket =
 | 
			
		||||
      case Ammo.update_ammo_type(ammo_type, ammo_type_params, current_user) do
 | 
			
		||||
        {:ok, %{name: ammo_type_name}} ->
 | 
			
		||||
          prompt = dgettext("prompts", "%{name} updated successfully", name: ammo_type_name)
 | 
			
		||||
      case Ammo.update_type(type, type_params, current_user) do
 | 
			
		||||
        {:ok, %{name: type_name}} ->
 | 
			
		||||
          prompt = dgettext("prompts", "%{name} updated successfully", name: type_name)
 | 
			
		||||
          socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
 | 
			
		||||
 | 
			
		||||
        {:error, %Changeset{} = changeset} ->
 | 
			
		||||
@@ -77,16 +76,16 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
 | 
			
		||||
    {:noreply, socket}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp save_ammo_type(
 | 
			
		||||
  defp save_type(
 | 
			
		||||
         %{assigns: %{current_user: current_user, return_to: return_to}} = socket,
 | 
			
		||||
         action,
 | 
			
		||||
         ammo_type_params
 | 
			
		||||
         type_params
 | 
			
		||||
       )
 | 
			
		||||
       when action in [:new, :clone] do
 | 
			
		||||
    socket =
 | 
			
		||||
      case Ammo.create_ammo_type(ammo_type_params, current_user) do
 | 
			
		||||
        {:ok, %{name: ammo_type_name}} ->
 | 
			
		||||
          prompt = dgettext("prompts", "%{name} created successfully", name: ammo_type_name)
 | 
			
		||||
      case Ammo.create_type(type_params, current_user) do
 | 
			
		||||
        {:ok, %{name: type_name}} ->
 | 
			
		||||
          prompt = dgettext("prompts", "%{name} created successfully", name: type_name)
 | 
			
		||||
          socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
 | 
			
		||||
 | 
			
		||||
        {:error, %Changeset{} = changeset} ->
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@
 | 
			
		||||
  <.form
 | 
			
		||||
    :let={f}
 | 
			
		||||
    for={@changeset}
 | 
			
		||||
    id="ammo_type-form"
 | 
			
		||||
    id="type-form"
 | 
			
		||||
    phx-target={@myself}
 | 
			
		||||
    phx-change="validate"
 | 
			
		||||
    phx-submit="save"
 | 
			
		||||
@@ -37,7 +37,7 @@
 | 
			
		||||
 | 
			
		||||
    <%= label(f, :desc, gettext("Description"), class: "title text-lg text-primary-600") %>
 | 
			
		||||
    <%= textarea(f, :desc,
 | 
			
		||||
      id: "ammo-type-form-desc",
 | 
			
		||||
      id: "type-form-desc",
 | 
			
		||||
      class: "text-center col-span-2 input input-primary",
 | 
			
		||||
      phx_hook: "MaintainAttrs",
 | 
			
		||||
      phx_update: "ignore"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,18 +1,18 @@
 | 
			
		||||
defmodule CanneryWeb.AmmoTypeLive.Index do
 | 
			
		||||
defmodule CanneryWeb.TypeLive.Index do
 | 
			
		||||
  @moduledoc """
 | 
			
		||||
  Liveview for showing a Cannery.Ammo.AmmoType index
 | 
			
		||||
  Liveview for showing a Cannery.Ammo.Type index
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb, :live_view
 | 
			
		||||
  alias Cannery.{Ammo, Ammo.AmmoType}
 | 
			
		||||
  alias Cannery.{Ammo, Ammo.Type}
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
  def mount(%{"search" => search}, _session, socket) do
 | 
			
		||||
    {:ok, socket |> assign(class: :all, show_used: false, search: search) |> list_ammo_types()}
 | 
			
		||||
    {:ok, socket |> assign(class: :all, show_used: false, search: search) |> list_types()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def mount(_params, _session, socket) do
 | 
			
		||||
    {:ok, socket |> assign(class: :all, show_used: false, search: nil) |> list_ammo_types()}
 | 
			
		||||
    {:ok, socket |> assign(class: :all, show_used: false, search: nil) |> list_types()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
@@ -21,28 +21,28 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
 | 
			
		||||
    %{name: ammo_type_name} = ammo_type = Ammo.get_ammo_type!(id, current_user)
 | 
			
		||||
    %{name: type_name} = type = Ammo.get_type!(id, current_user)
 | 
			
		||||
 | 
			
		||||
    socket
 | 
			
		||||
    |> assign(
 | 
			
		||||
      page_title: gettext("Edit %{ammo_type_name}", ammo_type_name: ammo_type_name),
 | 
			
		||||
      ammo_type: ammo_type
 | 
			
		||||
      page_title: gettext("Edit %{type_name}", type_name: type_name),
 | 
			
		||||
      type: type
 | 
			
		||||
    )
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp apply_action(%{assigns: %{current_user: current_user}} = socket, :clone, %{"id" => id}) do
 | 
			
		||||
    socket
 | 
			
		||||
    |> assign(
 | 
			
		||||
      page_title: gettext("New Ammo type"),
 | 
			
		||||
      ammo_type: %{Ammo.get_ammo_type!(id, current_user) | id: nil}
 | 
			
		||||
      page_title: gettext("New Type"),
 | 
			
		||||
      type: %{Ammo.get_type!(id, current_user) | id: nil}
 | 
			
		||||
    )
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp apply_action(socket, :new, _params) do
 | 
			
		||||
    socket
 | 
			
		||||
    |> assign(
 | 
			
		||||
      page_title: gettext("New Ammo type"),
 | 
			
		||||
      ammo_type: %AmmoType{}
 | 
			
		||||
      page_title: gettext("New Type"),
 | 
			
		||||
      type: %Type{}
 | 
			
		||||
    )
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -51,9 +51,9 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
 | 
			
		||||
    |> assign(
 | 
			
		||||
      page_title: gettext("Catalog"),
 | 
			
		||||
      search: nil,
 | 
			
		||||
      ammo_type: nil
 | 
			
		||||
      type: nil
 | 
			
		||||
    )
 | 
			
		||||
    |> list_ammo_types()
 | 
			
		||||
    |> list_types()
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp apply_action(socket, :search, %{"search" => search}) do
 | 
			
		||||
@@ -61,54 +61,54 @@ defmodule CanneryWeb.AmmoTypeLive.Index do
 | 
			
		||||
    |> assign(
 | 
			
		||||
      page_title: gettext("Catalog"),
 | 
			
		||||
      search: search,
 | 
			
		||||
      ammo_type: nil
 | 
			
		||||
      type: nil
 | 
			
		||||
    )
 | 
			
		||||
    |> list_ammo_types()
 | 
			
		||||
    |> list_types()
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
  def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
 | 
			
		||||
    %{name: name} = Ammo.get_ammo_type!(id, current_user) |> Ammo.delete_ammo_type!(current_user)
 | 
			
		||||
    %{name: name} = Ammo.get_type!(id, current_user) |> Ammo.delete_type!(current_user)
 | 
			
		||||
    prompt = dgettext("prompts", "%{name} deleted succesfully", name: name)
 | 
			
		||||
    {:noreply, socket |> put_flash(:info, prompt) |> list_ammo_types()}
 | 
			
		||||
    {:noreply, socket |> put_flash(:info, prompt) |> list_types()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:show_used, !show_used) |> list_ammo_types()}
 | 
			
		||||
    {:noreply, socket |> assign(:show_used, !show_used) |> list_types()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
 | 
			
		||||
    {:noreply, socket |> push_patch(to: Routes.ammo_type_index_path(Endpoint, :index))}
 | 
			
		||||
    {:noreply, socket |> push_patch(to: Routes.type_index_path(Endpoint, :index))}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
 | 
			
		||||
    search_path = Routes.ammo_type_index_path(Endpoint, :search, search_term)
 | 
			
		||||
    search_path = Routes.type_index_path(Endpoint, :search, search_term)
 | 
			
		||||
    {:noreply, socket |> push_patch(to: search_path)}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :rifle) |> list_ammo_types()}
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "rifle"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :rifle) |> list_types()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :shotgun) |> list_ammo_types()}
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "shotgun"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :shotgun) |> list_types()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :pistol) |> list_ammo_types()}
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "pistol"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :pistol) |> list_types()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :all) |> list_ammo_types()}
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => _all}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :all) |> list_types()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp list_ammo_types(
 | 
			
		||||
  defp list_types(
 | 
			
		||||
         %{assigns: %{class: class, search: search, current_user: current_user}} = socket
 | 
			
		||||
       ) do
 | 
			
		||||
    socket
 | 
			
		||||
    |> assign(
 | 
			
		||||
      ammo_types: Ammo.list_ammo_types(search, current_user, class),
 | 
			
		||||
      ammo_types_count: Ammo.get_ammo_types_count!(current_user)
 | 
			
		||||
      types: Ammo.list_types(search, current_user, class),
 | 
			
		||||
      types_count: Ammo.get_types_count!(current_user)
 | 
			
		||||
    )
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -3,25 +3,25 @@
 | 
			
		||||
    <%= gettext("Catalog") %>
 | 
			
		||||
  </h1>
 | 
			
		||||
 | 
			
		||||
  <%= if @ammo_types_count == 0 do %>
 | 
			
		||||
  <%= if @types_count == 0 do %>
 | 
			
		||||
    <h2 class="title text-xl text-primary-600">
 | 
			
		||||
      <%= gettext("No Ammo types") %>
 | 
			
		||||
      <%= gettext("No Types") %>
 | 
			
		||||
      <%= display_emoji("😔") %>
 | 
			
		||||
    </h2>
 | 
			
		||||
 | 
			
		||||
    <.link patch={Routes.ammo_type_index_path(Endpoint, :new)} class="btn btn-primary">
 | 
			
		||||
    <.link patch={Routes.type_index_path(Endpoint, :new)} class="btn btn-primary">
 | 
			
		||||
      <%= dgettext("actions", "Add your first type!") %>
 | 
			
		||||
    </.link>
 | 
			
		||||
  <% else %>
 | 
			
		||||
    <.link patch={Routes.ammo_type_index_path(Endpoint, :new)} class="btn btn-primary">
 | 
			
		||||
      <%= dgettext("actions", "New Ammo type") %>
 | 
			
		||||
    <.link patch={Routes.type_index_path(Endpoint, :new)} class="btn btn-primary">
 | 
			
		||||
      <%= dgettext("actions", "New Type") %>
 | 
			
		||||
    </.link>
 | 
			
		||||
 | 
			
		||||
    <div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-2xl">
 | 
			
		||||
      <.form
 | 
			
		||||
        :let={f}
 | 
			
		||||
        for={%{}}
 | 
			
		||||
        as={:ammo_type}
 | 
			
		||||
        as={:type}
 | 
			
		||||
        phx-change="change_class"
 | 
			
		||||
        phx-submit="change_class"
 | 
			
		||||
        class="flex items-center"
 | 
			
		||||
@@ -66,49 +66,43 @@
 | 
			
		||||
      </.toggle_button>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <%= if @ammo_types |> Enum.empty?() do %>
 | 
			
		||||
    <%= if @types |> Enum.empty?() do %>
 | 
			
		||||
      <h2 class="title text-xl text-primary-600">
 | 
			
		||||
        <%= gettext("No Ammo types") %>
 | 
			
		||||
        <%= gettext("No Types") %>
 | 
			
		||||
        <%= display_emoji("😔") %>
 | 
			
		||||
      </h2>
 | 
			
		||||
    <% else %>
 | 
			
		||||
      <.live_component
 | 
			
		||||
        module={CanneryWeb.Components.AmmoTypeTableComponent}
 | 
			
		||||
        id="ammo_types_index_table"
 | 
			
		||||
        module={CanneryWeb.Components.TypeTableComponent}
 | 
			
		||||
        id="types_index_table"
 | 
			
		||||
        action={@live_action}
 | 
			
		||||
        ammo_types={@ammo_types}
 | 
			
		||||
        types={@types}
 | 
			
		||||
        current_user={@current_user}
 | 
			
		||||
        show_used={@show_used}
 | 
			
		||||
        class={@class}
 | 
			
		||||
      >
 | 
			
		||||
        <:actions :let={ammo_type}>
 | 
			
		||||
        <:actions :let={type}>
 | 
			
		||||
          <div class="px-4 py-2 space-x-4 flex justify-center items-center">
 | 
			
		||||
            <.link
 | 
			
		||||
              navigate={Routes.ammo_type_show_path(Endpoint, :show, ammo_type)}
 | 
			
		||||
              navigate={Routes.type_show_path(Endpoint, :show, type)}
 | 
			
		||||
              class="text-primary-600 link"
 | 
			
		||||
              aria-label={
 | 
			
		||||
                dgettext("actions", "View %{ammo_type_name}", ammo_type_name: ammo_type.name)
 | 
			
		||||
              }
 | 
			
		||||
              aria-label={dgettext("actions", "View %{type_name}", type_name: type.name)}
 | 
			
		||||
            >
 | 
			
		||||
              <i class="fa-fw fa-lg fas fa-eye"></i>
 | 
			
		||||
            </.link>
 | 
			
		||||
 | 
			
		||||
            <.link
 | 
			
		||||
              patch={Routes.ammo_type_index_path(Endpoint, :edit, ammo_type)}
 | 
			
		||||
              patch={Routes.type_index_path(Endpoint, :edit, type)}
 | 
			
		||||
              class="text-primary-600 link"
 | 
			
		||||
              aria-label={
 | 
			
		||||
                dgettext("actions", "Edit %{ammo_type_name}", ammo_type_name: ammo_type.name)
 | 
			
		||||
              }
 | 
			
		||||
              aria-label={dgettext("actions", "Edit %{type_name}", type_name: type.name)}
 | 
			
		||||
            >
 | 
			
		||||
              <i class="fa-fw fa-lg fas fa-edit"></i>
 | 
			
		||||
            </.link>
 | 
			
		||||
 | 
			
		||||
            <.link
 | 
			
		||||
              patch={Routes.ammo_type_index_path(Endpoint, :clone, ammo_type)}
 | 
			
		||||
              patch={Routes.type_index_path(Endpoint, :clone, type)}
 | 
			
		||||
              class="text-primary-600 link"
 | 
			
		||||
              aria-label={
 | 
			
		||||
                dgettext("actions", "Clone %{ammo_type_name}", ammo_type_name: ammo_type.name)
 | 
			
		||||
              }
 | 
			
		||||
              aria-label={dgettext("actions", "Clone %{type_name}", type_name: type.name)}
 | 
			
		||||
            >
 | 
			
		||||
              <i class="fa-fw fa-lg fas fa-copy"></i>
 | 
			
		||||
            </.link>
 | 
			
		||||
@@ -117,17 +111,15 @@
 | 
			
		||||
              href="#"
 | 
			
		||||
              class="text-primary-600 link"
 | 
			
		||||
              phx-click="delete"
 | 
			
		||||
              phx-value-id={ammo_type.id}
 | 
			
		||||
              phx-value-id={type.id}
 | 
			
		||||
              data-confirm={
 | 
			
		||||
                dgettext(
 | 
			
		||||
                  "prompts",
 | 
			
		||||
                  "Are you sure you want to delete %{name}? This will delete all %{name} type ammo as well!",
 | 
			
		||||
                  name: ammo_type.name
 | 
			
		||||
                  name: type.name
 | 
			
		||||
                )
 | 
			
		||||
              }
 | 
			
		||||
              aria-label={
 | 
			
		||||
                dgettext("actions", "Delete %{ammo_type_name}", ammo_type_name: ammo_type.name)
 | 
			
		||||
              }
 | 
			
		||||
              aria-label={dgettext("actions", "Delete %{type_name}", type_name: type.name)}
 | 
			
		||||
            >
 | 
			
		||||
              <i class="fa-lg fas fa-trash"></i>
 | 
			
		||||
            </.link>
 | 
			
		||||
@@ -140,15 +132,15 @@
 | 
			
		||||
 | 
			
		||||
<.modal
 | 
			
		||||
  :if={@live_action in [:new, :edit, :clone]}
 | 
			
		||||
  return_to={Routes.ammo_type_index_path(Endpoint, :index)}
 | 
			
		||||
  return_to={Routes.type_index_path(Endpoint, :index)}
 | 
			
		||||
>
 | 
			
		||||
  <.live_component
 | 
			
		||||
    module={CanneryWeb.AmmoTypeLive.FormComponent}
 | 
			
		||||
    id={@ammo_type.id || :new}
 | 
			
		||||
    module={CanneryWeb.TypeLive.FormComponent}
 | 
			
		||||
    id={@type.id || :new}
 | 
			
		||||
    title={@page_title}
 | 
			
		||||
    action={@live_action}
 | 
			
		||||
    ammo_type={@ammo_type}
 | 
			
		||||
    return_to={Routes.ammo_type_index_path(Endpoint, :index)}
 | 
			
		||||
    type={@type}
 | 
			
		||||
    return_to={Routes.type_index_path(Endpoint, :index)}
 | 
			
		||||
    current_user={@current_user}
 | 
			
		||||
    }
 | 
			
		||||
  />
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,10 @@
 | 
			
		||||
defmodule CanneryWeb.AmmoTypeLive.Show do
 | 
			
		||||
defmodule CanneryWeb.TypeLive.Show do
 | 
			
		||||
  @moduledoc """
 | 
			
		||||
  Liveview for showing and editing an Cannery.Ammo.AmmoType
 | 
			
		||||
  Liveview for showing and editing an Cannery.Ammo.Type
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb, :live_view
 | 
			
		||||
  alias Cannery.{ActivityLog, Ammo, Ammo.AmmoType, Containers}
 | 
			
		||||
  alias Cannery.{ActivityLog, Ammo, Ammo.Type, Containers}
 | 
			
		||||
  alias CanneryWeb.Endpoint
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
@@ -13,38 +13,38 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
  def handle_params(%{"id" => id}, _params, socket) do
 | 
			
		||||
    {:noreply, socket |> display_ammo_type(id)}
 | 
			
		||||
    {:noreply, socket |> display_type(id)}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
  def handle_event(
 | 
			
		||||
        "delete",
 | 
			
		||||
        _params,
 | 
			
		||||
        %{assigns: %{ammo_type: ammo_type, current_user: current_user}} = socket
 | 
			
		||||
        %{assigns: %{type: type, current_user: current_user}} = socket
 | 
			
		||||
      ) do
 | 
			
		||||
    %{name: ammo_type_name} = ammo_type |> Ammo.delete_ammo_type!(current_user)
 | 
			
		||||
    %{name: type_name} = type |> Ammo.delete_type!(current_user)
 | 
			
		||||
 | 
			
		||||
    prompt = dgettext("prompts", "%{name} deleted succesfully", name: ammo_type_name)
 | 
			
		||||
    redirect_to = Routes.ammo_type_index_path(socket, :index)
 | 
			
		||||
    prompt = dgettext("prompts", "%{name} deleted succesfully", name: type_name)
 | 
			
		||||
    redirect_to = Routes.type_index_path(socket, :index)
 | 
			
		||||
 | 
			
		||||
    {:noreply, socket |> put_flash(:info, prompt) |> push_navigate(to: redirect_to)}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("toggle_show_used", _params, %{assigns: %{show_used: show_used}} = socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:show_used, !show_used) |> display_ammo_type()}
 | 
			
		||||
    {:noreply, socket |> assign(:show_used, !show_used) |> display_type()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("toggle_table", _params, %{assigns: %{view_table: view_table}} = socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:view_table, !view_table)}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp display_ammo_type(
 | 
			
		||||
  defp display_type(
 | 
			
		||||
         %{assigns: %{live_action: live_action, current_user: current_user, show_used: show_used}} =
 | 
			
		||||
           socket,
 | 
			
		||||
         %AmmoType{name: ammo_type_name} = ammo_type
 | 
			
		||||
         %Type{name: type_name} = type
 | 
			
		||||
       ) do
 | 
			
		||||
    custom_fields? =
 | 
			
		||||
      fields_to_display(ammo_type)
 | 
			
		||||
      fields_to_display(type)
 | 
			
		||||
      |> Enum.any?(fn %{key: field, type: type} ->
 | 
			
		||||
        default_value =
 | 
			
		||||
          case type do
 | 
			
		||||
@@ -52,10 +52,10 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
 | 
			
		||||
            _other_type -> nil
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
        ammo_type |> Map.get(field) != default_value
 | 
			
		||||
        type |> Map.get(field) != default_value
 | 
			
		||||
      end)
 | 
			
		||||
 | 
			
		||||
    packs = ammo_type |> Ammo.list_packs_for_type(current_user, show_used)
 | 
			
		||||
    packs = type |> Ammo.list_packs_for_type(current_user, show_used)
 | 
			
		||||
 | 
			
		||||
    [
 | 
			
		||||
      original_counts,
 | 
			
		||||
@@ -67,10 +67,10 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
 | 
			
		||||
      if show_used do
 | 
			
		||||
        [
 | 
			
		||||
          packs |> Ammo.get_original_counts(current_user),
 | 
			
		||||
          ammo_type |> Ammo.get_used_packs_count_for_type(current_user),
 | 
			
		||||
          ammo_type |> Ammo.get_packs_count_for_type(current_user, true),
 | 
			
		||||
          ammo_type |> ActivityLog.get_used_count_for_ammo_type(current_user),
 | 
			
		||||
          ammo_type |> Ammo.get_historical_count_for_ammo_type(current_user)
 | 
			
		||||
          type |> Ammo.get_used_packs_count_for_type(current_user),
 | 
			
		||||
          type |> Ammo.get_packs_count_for_type(current_user, true),
 | 
			
		||||
          type |> ActivityLog.get_used_count_for_type(current_user),
 | 
			
		||||
          type |> Ammo.get_historical_count_for_type(current_user)
 | 
			
		||||
        ]
 | 
			
		||||
      else
 | 
			
		||||
        [nil, nil, nil, nil, nil]
 | 
			
		||||
@@ -78,8 +78,8 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
 | 
			
		||||
 | 
			
		||||
    page_title =
 | 
			
		||||
      case live_action do
 | 
			
		||||
        :show -> ammo_type_name
 | 
			
		||||
        :edit -> gettext("Edit %{ammo_type_name}", ammo_type_name: ammo_type_name)
 | 
			
		||||
        :show -> type_name
 | 
			
		||||
        :edit -> gettext("Edit %{type_name}", type_name: type_name)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
    containers =
 | 
			
		||||
@@ -90,33 +90,33 @@ defmodule CanneryWeb.AmmoTypeLive.Show do
 | 
			
		||||
    socket
 | 
			
		||||
    |> assign(
 | 
			
		||||
      page_title: page_title,
 | 
			
		||||
      ammo_type: ammo_type,
 | 
			
		||||
      type: type,
 | 
			
		||||
      packs: packs,
 | 
			
		||||
      containers: containers,
 | 
			
		||||
      cprs: packs |> Ammo.get_cprs(current_user),
 | 
			
		||||
      last_used_dates: packs |> ActivityLog.get_last_used_dates(current_user),
 | 
			
		||||
      avg_cost_per_round: ammo_type |> Ammo.get_average_cost_for_ammo_type(current_user),
 | 
			
		||||
      rounds: ammo_type |> Ammo.get_round_count_for_ammo_type(current_user),
 | 
			
		||||
      avg_cost_per_round: type |> Ammo.get_average_cost_for_type(current_user),
 | 
			
		||||
      rounds: type |> Ammo.get_round_count_for_type(current_user),
 | 
			
		||||
      original_counts: original_counts,
 | 
			
		||||
      used_rounds: used_rounds,
 | 
			
		||||
      historical_round_count: historical_round_count,
 | 
			
		||||
      packs_count: ammo_type |> Ammo.get_packs_count_for_type(current_user),
 | 
			
		||||
      packs_count: type |> Ammo.get_packs_count_for_type(current_user),
 | 
			
		||||
      used_packs_count: used_packs_count,
 | 
			
		||||
      historical_packs_count: historical_packs_count,
 | 
			
		||||
      fields_to_display: fields_to_display(ammo_type),
 | 
			
		||||
      fields_to_display: fields_to_display(type),
 | 
			
		||||
      custom_fields?: custom_fields?
 | 
			
		||||
    )
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp display_ammo_type(%{assigns: %{current_user: current_user}} = socket, ammo_type_id) do
 | 
			
		||||
    socket |> display_ammo_type(Ammo.get_ammo_type!(ammo_type_id, current_user))
 | 
			
		||||
  defp display_type(%{assigns: %{current_user: current_user}} = socket, type_id) do
 | 
			
		||||
    socket |> display_type(Ammo.get_type!(type_id, current_user))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp display_ammo_type(%{assigns: %{ammo_type: ammo_type}} = socket) do
 | 
			
		||||
    socket |> display_ammo_type(ammo_type)
 | 
			
		||||
  defp display_type(%{assigns: %{type: type}} = socket) do
 | 
			
		||||
    socket |> display_type(type)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp fields_to_display(%AmmoType{class: class}) do
 | 
			
		||||
  defp fields_to_display(%Type{class: class}) do
 | 
			
		||||
    [
 | 
			
		||||
      %{label: gettext("Cartridge:"), key: :cartridge, type: :string},
 | 
			
		||||
      %{
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +1,22 @@
 | 
			
		||||
<div class="space-y-4 flex flex-col justify-center items-center">
 | 
			
		||||
  <h1 class="title text-2xl title-primary-500">
 | 
			
		||||
    <%= @ammo_type.name %>
 | 
			
		||||
    <%= @type.name %>
 | 
			
		||||
  </h1>
 | 
			
		||||
 | 
			
		||||
  <span
 | 
			
		||||
    :if={@ammo_type.desc}
 | 
			
		||||
    :if={@type.desc}
 | 
			
		||||
    class="max-w-2xl w-full px-8 py-4 rounded-lg
 | 
			
		||||
      text-center title text-lg
 | 
			
		||||
      border border-primary-600"
 | 
			
		||||
  >
 | 
			
		||||
    <%= @ammo_type.desc %>
 | 
			
		||||
    <%= @type.desc %>
 | 
			
		||||
  </span>
 | 
			
		||||
 | 
			
		||||
  <div class="flex space-x-4 justify-center items-center text-primary-600">
 | 
			
		||||
    <.link
 | 
			
		||||
      patch={Routes.ammo_type_show_path(Endpoint, :edit, @ammo_type)}
 | 
			
		||||
      patch={Routes.type_show_path(Endpoint, :edit, @type)}
 | 
			
		||||
      class="text-primary-600 link"
 | 
			
		||||
      aria-label={dgettext("actions", "Edit %{ammo_type_name}", ammo_type_name: @ammo_type.name)}
 | 
			
		||||
      aria-label={dgettext("actions", "Edit %{type_name}", type_name: @type.name)}
 | 
			
		||||
    >
 | 
			
		||||
      <i class="fa-fw fa-lg fas fa-edit"></i>
 | 
			
		||||
    </.link>
 | 
			
		||||
@@ -29,12 +29,10 @@
 | 
			
		||||
        dgettext(
 | 
			
		||||
          "prompts",
 | 
			
		||||
          "Are you sure you want to delete %{name}? This will delete all %{name} type ammo as well!",
 | 
			
		||||
          name: @ammo_type.name
 | 
			
		||||
          name: @type.name
 | 
			
		||||
        )
 | 
			
		||||
      }
 | 
			
		||||
      aria-label={
 | 
			
		||||
        dgettext("actions", "Delete %{ammo_type_name}", ammo_type_name: @ammo_type.name)
 | 
			
		||||
      }
 | 
			
		||||
      aria-label={dgettext("actions", "Delete %{type_name}", type_name: @type.name)}
 | 
			
		||||
    >
 | 
			
		||||
      <i class="fa-fw fa-lg fas fa-trash"></i>
 | 
			
		||||
    </.link>
 | 
			
		||||
@@ -42,14 +40,14 @@
 | 
			
		||||
 | 
			
		||||
  <hr class="hr" />
 | 
			
		||||
 | 
			
		||||
  <%= if @ammo_type.class || @custom_fields? do %>
 | 
			
		||||
  <%= if @type.class || @custom_fields? do %>
 | 
			
		||||
    <div class="grid sm:grid-cols-2 gap-4 text-center justify-center items-center">
 | 
			
		||||
      <h3 class="title text-lg">
 | 
			
		||||
        <%= gettext("Class") %>
 | 
			
		||||
      </h3>
 | 
			
		||||
 | 
			
		||||
      <span class="text-primary-600">
 | 
			
		||||
        <%= case @ammo_type.class do %>
 | 
			
		||||
        <%= case @type.class do %>
 | 
			
		||||
          <% :shotgun -> %>
 | 
			
		||||
            <%= gettext("Shotgun") %>
 | 
			
		||||
          <% :rifle -> %>
 | 
			
		||||
@@ -62,7 +60,7 @@
 | 
			
		||||
      </span>
 | 
			
		||||
 | 
			
		||||
      <%= for %{label: label, key: key, type: type} <- @fields_to_display do %>
 | 
			
		||||
        <%= if @ammo_type |> Map.get(key) do %>
 | 
			
		||||
        <%= if @type |> Map.get(key) do %>
 | 
			
		||||
          <h3 class="title text-lg">
 | 
			
		||||
            <%= label %>
 | 
			
		||||
          </h3>
 | 
			
		||||
@@ -70,9 +68,9 @@
 | 
			
		||||
          <span class="text-primary-600">
 | 
			
		||||
            <%= case type do %>
 | 
			
		||||
              <% :boolean -> %>
 | 
			
		||||
                <%= @ammo_type |> Map.get(key) |> humanize() %>
 | 
			
		||||
                <%= @type |> Map.get(key) |> humanize() %>
 | 
			
		||||
              <% _ -> %>
 | 
			
		||||
                <%= @ammo_type |> Map.get(key) %>
 | 
			
		||||
                <%= @type |> Map.get(key) %>
 | 
			
		||||
            <% end %>
 | 
			
		||||
          </span>
 | 
			
		||||
        <% end %>
 | 
			
		||||
@@ -140,7 +138,7 @@
 | 
			
		||||
    </h3>
 | 
			
		||||
 | 
			
		||||
    <span class="text-primary-600">
 | 
			
		||||
      <.datetime id={"#{@ammo_type.id}-inserted-at"} datetime={@ammo_type.inserted_at} />
 | 
			
		||||
      <.datetime id={"#{@type.id}-inserted-at"} datetime={@type.inserted_at} />
 | 
			
		||||
    </span>
 | 
			
		||||
 | 
			
		||||
    <%= if @avg_cost_per_round do %>
 | 
			
		||||
@@ -184,7 +182,7 @@
 | 
			
		||||
      <%= if @view_table do %>
 | 
			
		||||
        <.live_component
 | 
			
		||||
          module={CanneryWeb.Components.PackTableComponent}
 | 
			
		||||
          id="ammo-type-show-table"
 | 
			
		||||
          id="type-show-table"
 | 
			
		||||
          packs={@packs}
 | 
			
		||||
          current_user={@current_user}
 | 
			
		||||
          show_used={@show_used}
 | 
			
		||||
@@ -228,17 +226,14 @@
 | 
			
		||||
  </div>
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
<.modal
 | 
			
		||||
  :if={@live_action == :edit}
 | 
			
		||||
  return_to={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)}
 | 
			
		||||
>
 | 
			
		||||
<.modal :if={@live_action == :edit} return_to={Routes.type_show_path(Endpoint, :show, @type)}>
 | 
			
		||||
  <.live_component
 | 
			
		||||
    module={CanneryWeb.AmmoTypeLive.FormComponent}
 | 
			
		||||
    id={@ammo_type.id}
 | 
			
		||||
    module={CanneryWeb.TypeLive.FormComponent}
 | 
			
		||||
    id={@type.id}
 | 
			
		||||
    title={@page_title}
 | 
			
		||||
    action={@live_action}
 | 
			
		||||
    ammo_type={@ammo_type}
 | 
			
		||||
    return_to={Routes.ammo_type_show_path(Endpoint, :show, @ammo_type)}
 | 
			
		||||
    type={@type}
 | 
			
		||||
    return_to={Routes.type_show_path(Endpoint, :show, @type)}
 | 
			
		||||
    current_user={@current_user}
 | 
			
		||||
  />
 | 
			
		||||
</.modal>
 | 
			
		||||
 
 | 
			
		||||
@@ -86,19 +86,19 @@ defmodule CanneryWeb.ContainerLive.Show do
 | 
			
		||||
    {:noreply, socket |> assign(:view_table, !view_table) |> render_container()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "rifle"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :rifle) |> render_container()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "shotgun"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :shotgun) |> render_container()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "pistol"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :pistol) |> render_container()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => _all}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :all) |> render_container()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -89,7 +89,7 @@
 | 
			
		||||
    <.form
 | 
			
		||||
      :let={f}
 | 
			
		||||
      for={%{}}
 | 
			
		||||
      as={:ammo_type}
 | 
			
		||||
      as={:type}
 | 
			
		||||
      phx-change="change_class"
 | 
			
		||||
      phx-submit="change_class"
 | 
			
		||||
      class="flex items-center"
 | 
			
		||||
@@ -126,16 +126,16 @@
 | 
			
		||||
      <%= if @view_table do %>
 | 
			
		||||
        <.live_component
 | 
			
		||||
          module={CanneryWeb.Components.PackTableComponent}
 | 
			
		||||
          id="ammo-type-show-table"
 | 
			
		||||
          id="type-show-table"
 | 
			
		||||
          packs={@packs}
 | 
			
		||||
          current_user={@current_user}
 | 
			
		||||
          show_used={false}
 | 
			
		||||
        >
 | 
			
		||||
          <:ammo_type :let={%{name: ammo_type_name} = ammo_type}>
 | 
			
		||||
            <.link navigate={Routes.ammo_type_show_path(Endpoint, :show, ammo_type)} class="link">
 | 
			
		||||
              <%= ammo_type_name %>
 | 
			
		||||
          <:type :let={%{name: type_name} = type}>
 | 
			
		||||
            <.link navigate={Routes.type_show_path(Endpoint, :show, type)} class="link">
 | 
			
		||||
              <%= type_name %>
 | 
			
		||||
            </.link>
 | 
			
		||||
          </:ammo_type>
 | 
			
		||||
          </:type>
 | 
			
		||||
        </.live_component>
 | 
			
		||||
      <% else %>
 | 
			
		||||
        <div class="flex flex-wrap justify-center items-stretch">
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@ defmodule CanneryWeb.PackLive.FormComponent do
 | 
			
		||||
  """
 | 
			
		||||
 | 
			
		||||
  use CanneryWeb, :live_component
 | 
			
		||||
  alias Cannery.Ammo.{AmmoType, Pack}
 | 
			
		||||
  alias Cannery.Ammo.{Pack, Type}
 | 
			
		||||
  alias Cannery.{Accounts.User, Ammo, Containers, Containers.Container}
 | 
			
		||||
  alias Ecto.Changeset
 | 
			
		||||
  alias Phoenix.LiveView.Socket
 | 
			
		||||
@@ -22,17 +22,17 @@ defmodule CanneryWeb.PackLive.FormComponent do
 | 
			
		||||
 | 
			
		||||
  @spec update(Socket.t()) :: {:ok, Socket.t()}
 | 
			
		||||
  def update(%{assigns: %{current_user: current_user}} = socket) do
 | 
			
		||||
    %{assigns: %{ammo_types: ammo_types, containers: containers}} =
 | 
			
		||||
    %{assigns: %{types: types, containers: containers}} =
 | 
			
		||||
      socket =
 | 
			
		||||
      socket
 | 
			
		||||
      |> assign(:pack_create_limit, @pack_create_limit)
 | 
			
		||||
      |> assign(:ammo_types, Ammo.list_ammo_types(current_user, :all))
 | 
			
		||||
      |> assign(:types, Ammo.list_types(current_user, :all))
 | 
			
		||||
      |> assign_new(:containers, fn -> Containers.list_containers(current_user) end)
 | 
			
		||||
 | 
			
		||||
    params =
 | 
			
		||||
      if ammo_types |> List.first() |> is_nil(),
 | 
			
		||||
      if types |> List.first() |> is_nil(),
 | 
			
		||||
        do: %{},
 | 
			
		||||
        else: %{} |> Map.put("ammo_type_id", ammo_types |> List.first() |> Map.get(:id))
 | 
			
		||||
        else: %{} |> Map.put("type_id", types |> List.first() |> Map.get(:id))
 | 
			
		||||
 | 
			
		||||
    params =
 | 
			
		||||
      if containers |> List.first() |> is_nil(),
 | 
			
		||||
@@ -62,9 +62,9 @@ defmodule CanneryWeb.PackLive.FormComponent do
 | 
			
		||||
    containers |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  @spec ammo_type_options([AmmoType.t()]) :: [{String.t(), AmmoType.id()}]
 | 
			
		||||
  defp ammo_type_options(ammo_types) do
 | 
			
		||||
    ammo_types |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
 | 
			
		||||
  @spec type_options([Type.t()]) :: [{String.t(), Type.id()}]
 | 
			
		||||
  defp type_options(types) do
 | 
			
		||||
    types |> Enum.map(fn %{id: id, name: name} -> {name, id} end)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Save Helpers
 | 
			
		||||
@@ -83,9 +83,9 @@ defmodule CanneryWeb.PackLive.FormComponent do
 | 
			
		||||
    changeset =
 | 
			
		||||
      case default_action do
 | 
			
		||||
        :insert ->
 | 
			
		||||
          ammo_type = maybe_get_ammo_type(pack_params, user)
 | 
			
		||||
          type = maybe_get_type(pack_params, user)
 | 
			
		||||
          container = maybe_get_container(pack_params, user)
 | 
			
		||||
          pack |> Pack.create_changeset(ammo_type, container, user, pack_params)
 | 
			
		||||
          pack |> Pack.create_changeset(type, container, user, pack_params)
 | 
			
		||||
 | 
			
		||||
        :update ->
 | 
			
		||||
          pack |> Pack.update_changeset(pack_params, user)
 | 
			
		||||
@@ -107,12 +107,12 @@ defmodule CanneryWeb.PackLive.FormComponent do
 | 
			
		||||
 | 
			
		||||
  defp maybe_get_container(_params_not_found, _user), do: nil
 | 
			
		||||
 | 
			
		||||
  defp maybe_get_ammo_type(%{"ammo_type_id" => ammo_type_id}, user)
 | 
			
		||||
       when is_binary(ammo_type_id) do
 | 
			
		||||
    ammo_type_id |> Ammo.get_ammo_type!(user)
 | 
			
		||||
  defp maybe_get_type(%{"type_id" => type_id}, user)
 | 
			
		||||
       when is_binary(type_id) do
 | 
			
		||||
    type_id |> Ammo.get_type!(user)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp maybe_get_ammo_type(_params_not_found, _user), do: nil
 | 
			
		||||
  defp maybe_get_type(_params_not_found, _user), do: nil
 | 
			
		||||
 | 
			
		||||
  defp save_pack(
 | 
			
		||||
         %{assigns: %{pack: pack, current_user: current_user, return_to: return_to}} = socket,
 | 
			
		||||
 
 | 
			
		||||
@@ -19,11 +19,11 @@
 | 
			
		||||
      <%= changeset_errors(@changeset) %>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <%= label(f, :ammo_type_id, gettext("Ammo type"), class: "title text-lg text-primary-600") %>
 | 
			
		||||
    <%= select(f, :ammo_type_id, ammo_type_options(@ammo_types),
 | 
			
		||||
    <%= label(f, :type_id, gettext("Type"), class: "title text-lg text-primary-600") %>
 | 
			
		||||
    <%= select(f, :type_id, type_options(@types),
 | 
			
		||||
      class: "text-center col-span-2 input input-primary"
 | 
			
		||||
    ) %>
 | 
			
		||||
    <%= error_tag(f, :ammo_type_id, "col-span-3 text-center") %>
 | 
			
		||||
    <%= error_tag(f, :type_id, "col-span-3 text-center") %>
 | 
			
		||||
 | 
			
		||||
    <%= label(f, :count, gettext("Count"), class: "title text-lg text-primary-600") %>
 | 
			
		||||
    <%= number_input(f, :count,
 | 
			
		||||
 
 | 
			
		||||
@@ -122,19 +122,19 @@ defmodule CanneryWeb.PackLive.Index do
 | 
			
		||||
    {:noreply, socket}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "rifle"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :rifle) |> display_packs()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "shotgun"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :shotgun) |> display_packs()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "pistol"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :pistol) |> display_packs()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => _all}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :all) |> display_packs()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@@ -152,13 +152,13 @@ defmodule CanneryWeb.PackLive.Index do
 | 
			
		||||
    # prompts
 | 
			
		||||
    packs_count = Ammo.get_packs_count!(current_user, true)
 | 
			
		||||
    packs = Ammo.list_packs(search, class, current_user, show_used)
 | 
			
		||||
    ammo_types_count = Ammo.get_ammo_types_count!(current_user)
 | 
			
		||||
    types_count = Ammo.get_types_count!(current_user)
 | 
			
		||||
    containers_count = Containers.get_containers_count!(current_user)
 | 
			
		||||
 | 
			
		||||
    socket
 | 
			
		||||
    |> assign(
 | 
			
		||||
      packs: packs,
 | 
			
		||||
      ammo_types_count: ammo_types_count,
 | 
			
		||||
      types_count: types_count,
 | 
			
		||||
      containers_count: containers_count,
 | 
			
		||||
      packs_count: packs_count
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
@@ -14,14 +14,14 @@
 | 
			
		||||
          <%= dgettext("actions", "add a container first") %>
 | 
			
		||||
        </.link>
 | 
			
		||||
      </div>
 | 
			
		||||
    <% @ammo_types_count == 0 -> %>
 | 
			
		||||
    <% @types_count == 0 -> %>
 | 
			
		||||
      <div class="flex justify-center items-center">
 | 
			
		||||
        <h2 class="m-2 title text-md text-primary-600">
 | 
			
		||||
          <%= dgettext("prompts", "You'll need to") %>
 | 
			
		||||
        </h2>
 | 
			
		||||
 | 
			
		||||
        <.link navigate={Routes.ammo_type_index_path(Endpoint, :new)} class="btn btn-primary">
 | 
			
		||||
          <%= dgettext("actions", "add an ammo type first") %>
 | 
			
		||||
        <.link navigate={Routes.type_index_path(Endpoint, :new)} class="btn btn-primary">
 | 
			
		||||
          <%= dgettext("actions", "add a type first") %>
 | 
			
		||||
        </.link>
 | 
			
		||||
      </div>
 | 
			
		||||
    <% @packs_count == 0 -> %>
 | 
			
		||||
@@ -42,7 +42,7 @@
 | 
			
		||||
        <.form
 | 
			
		||||
          :let={f}
 | 
			
		||||
          for={%{}}
 | 
			
		||||
          as={:ammo_type}
 | 
			
		||||
          as={:type}
 | 
			
		||||
          phx-change="change_class"
 | 
			
		||||
          phx-submit="change_class"
 | 
			
		||||
          class="flex items-center"
 | 
			
		||||
@@ -102,11 +102,11 @@
 | 
			
		||||
          current_user={@current_user}
 | 
			
		||||
          show_used={@show_used}
 | 
			
		||||
        >
 | 
			
		||||
          <:ammo_type :let={%{name: ammo_type_name} = ammo_type}>
 | 
			
		||||
            <.link navigate={Routes.ammo_type_show_path(Endpoint, :show, ammo_type)} class="link">
 | 
			
		||||
              <%= ammo_type_name %>
 | 
			
		||||
          <:type :let={%{name: type_name} = type}>
 | 
			
		||||
            <.link navigate={Routes.type_show_path(Endpoint, :show, type)} class="link">
 | 
			
		||||
              <%= type_name %>
 | 
			
		||||
            </.link>
 | 
			
		||||
          </:ammo_type>
 | 
			
		||||
          </:type>
 | 
			
		||||
          <:range :let={pack}>
 | 
			
		||||
            <div class="min-w-20 py-2 px-4 h-full flex flew-wrap justify-center items-center">
 | 
			
		||||
              <button
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
<div class="mx-auto space-y-4 max-w-3xl flex flex-col justify-center items-center">
 | 
			
		||||
  <h1 class="title text-2xl title-primary-500">
 | 
			
		||||
    <%= @pack.ammo_type.name %>
 | 
			
		||||
    <%= @pack.type.name %>
 | 
			
		||||
  </h1>
 | 
			
		||||
 | 
			
		||||
  <div class="space-y-2 flex flex-col justify-center items-center">
 | 
			
		||||
@@ -49,7 +49,7 @@
 | 
			
		||||
  <div class="flex flex-col justify-center items-center">
 | 
			
		||||
    <div class="flex flex-wrap justify-center items-center text-primary-600">
 | 
			
		||||
      <.link
 | 
			
		||||
        navigate={Routes.ammo_type_show_path(Endpoint, :show, @pack.ammo_type)}
 | 
			
		||||
        navigate={Routes.type_show_path(Endpoint, :show, @pack.type)}
 | 
			
		||||
        class="mx-4 my-2 btn btn-primary"
 | 
			
		||||
      >
 | 
			
		||||
        <%= dgettext("actions", "View in Catalog") %>
 | 
			
		||||
 
 | 
			
		||||
@@ -101,19 +101,19 @@ defmodule CanneryWeb.RangeLive.Index do
 | 
			
		||||
    {:noreply, socket |> push_patch(to: Routes.range_index_path(Endpoint, :search, search_term))}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "rifle"}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "rifle"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :rifle) |> display_shot_records()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "shotgun"}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "shotgun"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :shotgun) |> display_shot_records()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => "pistol"}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => "pistol"}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :pistol) |> display_shot_records()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def handle_event("change_class", %{"ammo_type" => %{"class" => _all}}, socket) do
 | 
			
		||||
  def handle_event("change_class", %{"type" => %{"class" => _all}}, socket) do
 | 
			
		||||
    {:noreply, socket |> assign(:class, :all) |> display_shot_records()}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -78,7 +78,7 @@
 | 
			
		||||
      <.form
 | 
			
		||||
        :let={f}
 | 
			
		||||
        for={%{}}
 | 
			
		||||
        as={:ammo_type}
 | 
			
		||||
        as={:type}
 | 
			
		||||
        phx-change="change_class"
 | 
			
		||||
        phx-submit="change_class"
 | 
			
		||||
        class="flex items-center"
 | 
			
		||||
 
 | 
			
		||||
@@ -69,14 +69,14 @@ defmodule CanneryWeb.Router do
 | 
			
		||||
    live "/tags/edit/:id", TagLive.Index, :edit
 | 
			
		||||
    live "/tags/search/:search", TagLive.Index, :search
 | 
			
		||||
 | 
			
		||||
    live "/catalog", AmmoTypeLive.Index, :index
 | 
			
		||||
    live "/catalog/new", AmmoTypeLive.Index, :new
 | 
			
		||||
    live "/catalog/clone/:id", AmmoTypeLive.Index, :clone
 | 
			
		||||
    live "/catalog/edit/:id", AmmoTypeLive.Index, :edit
 | 
			
		||||
    live "/catalog/search/:search", AmmoTypeLive.Index, :search
 | 
			
		||||
    live "/catalog", TypeLive.Index, :index
 | 
			
		||||
    live "/catalog/new", TypeLive.Index, :new
 | 
			
		||||
    live "/catalog/clone/:id", TypeLive.Index, :clone
 | 
			
		||||
    live "/catalog/edit/:id", TypeLive.Index, :edit
 | 
			
		||||
    live "/catalog/search/:search", TypeLive.Index, :search
 | 
			
		||||
 | 
			
		||||
    live "/type/:id", AmmoTypeLive.Show, :show
 | 
			
		||||
    live "/type/:id/edit", AmmoTypeLive.Show, :edit
 | 
			
		||||
    live "/type/:id", TypeLive.Show, :show
 | 
			
		||||
    live "/type/:id/edit", TypeLive.Show, :edit
 | 
			
		||||
 | 
			
		||||
    live "/containers", ContainerLive.Index, :index
 | 
			
		||||
    live "/containers/new", ContainerLive.Index, :new
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user