rename ammo type to type

This commit is contained in:
2023-03-30 21:53:52 -04:00
parent 98775359af
commit c33f15603b
59 changed files with 1616 additions and 1677 deletions

View File

@ -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} ->

View File

@ -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"

View File

@ -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

View File

@ -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}
}
/>

View File

@ -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},
%{

View File

@ -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>

View File

@ -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

View File

@ -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">

View File

@ -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,

View File

@ -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,

View File

@ -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
)

View File

@ -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

View File

@ -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") %>

View File

@ -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

View File

@ -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"