forked from shibao/cannery
rename ammo type to type
This commit is contained in:
@ -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") %>
|
||||
|
Reference in New Issue
Block a user