add ammo type stuff

This commit is contained in:
shibao 2022-02-05 16:22:02 -05:00
parent 4e3292f60b
commit 6f6402cf17
7 changed files with 302 additions and 108 deletions

View File

@ -13,12 +13,25 @@ defmodule Cannery.Ammo.AmmoType do
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "ammo_types" do
field :bullet_type, :string
field :case_material, :string
field :desc, :string
field :manufacturer, :string
field :name, :string
field :grain, :integer
field :desc, :string
# https://en.wikipedia.org/wiki/Bullet#Abbreviations
field :bullet_type, :string
field :bullet_core, :string
field :cartridge, :string
field :caliber, :string
field :case_material, :string
field :grains, :integer
field :pressure, :string
field :rimfire, :boolean, null: false, default: false
field :tracer, :boolean, null: false, default: false
field :incendiary, :boolean, null: false, default: false
field :blank, :boolean, null: false, default: false
field :corrosive, :boolean, null: false, default: false
field :manufacturer, :string
field :sku, :string
has_many :ammo_groups, AmmoGroup
@ -27,12 +40,22 @@ defmodule Cannery.Ammo.AmmoType do
@type t :: %AmmoType{
id: id(),
bullet_type: String.t(),
case_material: String.t(),
desc: String.t(),
manufacturer: String.t(),
name: String.t(),
grain: integer(),
desc: String.t(),
bullet_type: String.t(),
bullet_core: String.t(),
cartridge: String.t(),
caliber: String.t(),
case_material: String.t(),
grains: integer(),
pressure: String.t(),
rimfire: boolean(),
tracer: boolean(),
incendiary: boolean(),
blank: boolean(),
corrosive: boolean(),
manufacturer: String.t(),
sku: String.t(),
ammo_groups: [AmmoGroup.t()] | nil,
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
@ -44,7 +67,24 @@ defmodule Cannery.Ammo.AmmoType do
@spec changeset(t() | new_ammo_type(), attrs :: map()) :: Changeset.t(t() | new_ammo_type())
def changeset(ammo_type, attrs) do
ammo_type
|> cast(attrs, [:name, :desc, :case_material, :bullet_type, :grain, :manufacturer])
|> cast(attrs, [
:name,
:desc,
:bullet_type,
:bullet_core,
:cartridge,
:caliber,
:case_material,
:grains,
:pressure,
:rimfire,
:tracer,
:incendiary,
:blank,
:corrosive,
:manufacturer,
:sku
])
|> validate_required([:name])
end
end

View File

@ -63,26 +63,104 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
<%= error_tag(f, :desc) %>
</div>
<%= label(f, :case_material, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :case_material, class: "text-center col-span-2 input input-primary") %>
<div class="col-span-3 text-center">
<%= error_tag(f, :case_material) %>
</div>
<a
href="https://en.wikipedia.org/wiki/Bullet#Abbreviations"
class="col-span-3 text-center link title text-md text-primary-600"
>
Example bullet type abbreviations
</a>
<%= label(f, :bullet_type, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :bullet_type, class: "text-center col-span-2 input input-primary") %>
<%= text_input(f, :bullet_type,
class: "text-center col-span-2 input input-primary",
placeholder: "FMJ"
) %>
<div class="col-span-3 text-center">
<%= error_tag(f, :bullet_type) %>
</div>
<%= label(f, :grain, class: "mr-4 title text-lg text-primary-500") %>
<%= number_input(f, :grain,
step: "any",
<%= label(f, :bullet_core, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :bullet_core,
class: "text-center col-span-2 input input-primary",
min: 0
placeholder: "Steel"
) %>
<div class="col-span-3 text-center">
<%= error_tag(f, :grain) %>
<%= error_tag(f, :bullet_core) %>
</div>
<%= label(f, :cartridge, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :cartridge,
class: "text-center col-span-2 input input-primary",
placeholder: "5.56x46mm NATO"
) %>
<div class="col-span-3 text-center">
<%= error_tag(f, :cartridge) %>
</div>
<%= label(f, :caliber, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :caliber,
class: "text-center col-span-2 input input-primary",
placeholder: ".223"
) %>
<div class="col-span-3 text-center">
<%= error_tag(f, :caliber) %>
</div>
<%= label(f, :case_material, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :case_material,
class: "text-center col-span-2 input input-primary",
placeholder: "Brass"
) %>
<div class="col-span-3 text-center">
<%= error_tag(f, :case_material) %>
</div>
<%= label(f, :grains, class: "mr-4 title text-lg text-primary-500") %>
<%= number_input(f, :grains,
step: "1",
class: "text-center col-span-2 input input-primary",
min: 1
) %>
<div class="col-span-3 text-center">
<%= error_tag(f, :grains) %>
</div>
<%= label(f, :pressure, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :pressure,
class: "text-center col-span-2 input input-primary",
placeholder: "+P"
) %>
<div class="col-span-3 text-center">
<%= error_tag(f, :pressure) %>
</div>
<%= label(f, :rimfire, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :rimfire, class: "text-center col-span-2 checkbox") %>
<div class="col-span-3 text-center">
<%= error_tag(f, :rimfire) %>
</div>
<%= label(f, :tracer, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :tracer, class: "text-center col-span-2 checkbox") %>
<div class="col-span-3 text-center">
<%= error_tag(f, :tracer) %>
</div>
<%= label(f, :incendiary, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :incendiary, class: "text-center col-span-2 checkbox") %>
<div class="col-span-3 text-center">
<%= error_tag(f, :incendiary) %>
</div>
<%= label(f, :blank, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :blank, class: "text-center col-span-2 checkbox") %>
<div class="col-span-3 text-center">
<%= error_tag(f, :blank) %>
</div>
<%= label(f, :corrosive, class: "mr-4 title text-lg text-primary-500") %>
<%= checkbox(f, :corrosive, class: "text-center col-span-2 checkbox") %>
<div class="col-span-3 text-center">
<%= error_tag(f, :corrosive) %>
</div>
<%= label(f, :manufacturer, class: "mr-4 title text-lg text-primary-500") %>
@ -91,6 +169,12 @@ defmodule CanneryWeb.AmmoTypeLive.FormComponent do
<%= error_tag(f, :manufacturer) %>
</div>
<%= label(f, :sku, class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :sku, class: "text-center col-span-2 input input-primary") %>
<div class="col-span-3 text-center">
<%= error_tag(f, :sku) %>
</div>
<%= submit("Save",
phx_disable_with: "Saving...",
class: "mx-auto col-span-3 btn btn-primary"

View File

@ -22,21 +22,27 @@
<table class="min-w-full table-auto text-center bg-white">
<thead class="border-b border-primary-600">
<tr>
<%= for field <- [
:name,
:bullet_type,
:bullet_core,
:cartridge,
:caliber,
:case_material,
:grains,
:pressure,
:rimfire,
:tracer,
:incendiary,
:blank,
:corrosive,
:manufacturer,
:sku
] do %>
<th class="p-2">
Name
</th>
<th class="p-2">
Case material
</th>
<th class="p-2">
Bullet type
</th>
<th class="p-2">
Grain
</th>
<th class="p-2">
Manufacturer
<%= field |> humanize() %>
</th>
<% end %>
<th class="p-2"></th>
</tr>
@ -44,28 +50,49 @@
<tbody>
<%= for ammo_type <- @ammo_types do %>
<tr id={"ammo_type-#{ammo_type.id}"}>
<%= for field <- [
:name,
:bullet_type,
:bullet_core,
:cartridge,
:caliber,
:case_material,
:grains,
:pressure
] do %>
<td class="p-2">
<%= ammo_type.name %>
</td>
<td class="p-2">
<%= ammo_type.case_material %>
</td>
<td class="p-2">
<%= ammo_type.bullet_type %>
</td>
<td class="p-2">
<%= ammo_type.grain %>
</td>
<td class="p-2">
<%= ammo_type.manufacturer %>
<%= ammo_type |> Map.get(field) %>
</td>
<% end %>
<td class="p-2 w-full h-full space-x-2 flex justify-center items-center">
<%= live_redirect("View", to: Routes.ammo_type_show_path(@socket, :show, ammo_type)) %>
<%= for field <- [
:rimfire,
:tracer,
:incendiary,
:blank,
:corrosive
] do %>
<td class="p-2">
<%= ammo_type |> Map.get(field) |> humanize() %>
</td>
<% end %>
<%= for field <- [:manufacturer, :sku] do %>
<td class="p-2">
<%= ammo_type |> Map.get(field) %>
</td>
<% end %>
<td class="p-2">
<div class="px-4 py-2 space-x-4 flex justify-center items-center">
<%= live_redirect to: Routes.ammo_type_show_path(@socket, :show, ammo_type),
class: "text-primary-500 link" do %>
<i class="fa-lg fas fa-eye"></i>
<% end %>
<%= live_patch to: Routes.ammo_type_index_path(@socket, :edit, ammo_type),
class: "text-primary-500 link" do %>
<i class="fa-fw fa-lg fas fa-edit"></i>
<i class="fa-lg fas fa-edit"></i>
<% end %>
<%= link to: "#",
@ -73,8 +100,9 @@
phx_click: "delete",
phx_value_id: ammo_type.id,
data: [confirm: "Are you sure you want to delete this ammo?"] do %>
<i class="fa-fw fa-lg fas fa-trash"></i>
<i class="fa-lg fas fa-trash"></i>
<% end %>
</div>
</td>
</tr>
<% end %>

View File

@ -1,40 +1,17 @@
<div class="mx-auto space-y-4 max-w-3xl flex flex-col justify-center items-center">
<div class="mx-auto px-4 sm:px-8 space-y-4 max-w-3xl
flex flex-col justify-center items-center">
<h1 class="title text-2xl title-primary-500">
<%= @ammo_type.name %>
</h1>
<div class="space-y-2 flex flex-col justify-center items-center">
<%= if @ammo_type.desc do %>
<span class="rounded-lg title text-lg">
Desc: <%= @ammo_type.desc %>
<span class="max-w-2xl w-full px-8 py-4 rounded-lg
text-center title text-lg
border border-primary-600">
<%= @ammo_type.desc %>
</span>
<% end %>
<%= if @ammo_type.case_material do %>
<span class="rounded-lg title text-lg">
Case material: <%= @ammo_type.case_material %>
</span>
<% end %>
<%= if @ammo_type.bullet_type do %>
<span class="rounded-lg title text-lg">
Bullet type: <%= @ammo_type.bullet_type %>
</span>
<% end %>
<%= if @ammo_type.grain do %>
<span class="rounded-lg title text-lg">
Grain: <%= @ammo_type.grain %>
</span>
<% end %>
<%= if @ammo_type.manufacturer do %>
<span class="rounded-lg title text-lg">
Manufacturer: <%= @ammo_type.manufacturer %>
</span>
<% end %>
</div>
<div class="flex space-x-4 justify-center items-center text-primary-500">
<%= live_patch to: Routes.ammo_type_show_path(@socket, :edit, @ammo_type),
class: "text-primary-500 link" do %>
@ -49,9 +26,61 @@
<% end %>
</div>
<hr class="mb-4 w-full">
<hr class="hr">
<div class="mb-4">
<div class="grid sm:grid-cols-2 text-center justify-center items-center">
<%= for field <- [
:bullet_type,
:bullet_core,
:cartridge,
:caliber,
:case_material,
:grains,
:pressure
] do %>
<%= if @ammo_type |> Map.get(field) do %>
<h3 class="mb-2 sm:mr-4 title text-lg">
<%= field |> humanize() %> :
</h3>
<span class="mb-4 sm:mb-2 text-primary-600">
<%= @ammo_type |> Map.get(field) %>
</span>
<% end %>
<% end %>
<%= for field <- [
:rimfire,
:tracer,
:incendiary,
:blank,
:corrosive
] do %>
<h3 class="mb-2 sm:mr-4 title text-lg">
<%= field |> humanize() %> :
</h3>
<span class="mb-4 sm:mb-2 text-primary-600">
<%= @ammo_type |> Map.get(field) |> humanize() %>
</span>
<% end %>
<%= for field <- [:manufacturer, :sku] do %>
<%= if @ammo_type |> Map.get(field) do %>
<h3 class="mb-2 sm:mr-4 title text-lg">
<%= field |> humanize() %> :
</h3>
<span class="mb-4 sm:mb-2 text-primary-600">
<%= @ammo_type |> Map.get(field) %>
</span>
<% end %>
<% end %>
</div>
<hr class="hr">
<div>
<%= if @ammo_type.ammo_groups |> Enum.empty?() do %>
No ammo for this type
<% else %>

View File

@ -20,15 +20,15 @@ defmodule CanneryWeb.ModalComponent do
phx-target={"#{@id}"}
phx-page-loading
>
<div class="w-full max-w-4xl relative
p-8 flex flex-col justify-start items-center
<div class="w-full max-w-3xl max-h-128 relative overflow-y-auto
flex flex-col justify-start items-center
bg-white border-2 rounded-lg">
<%= live_patch to: @return_to,
class:
"absolute top-8 right-10 text-gray-500 hover:text-gray-800 transition-all duration-500 ease-in-out" do %>
class: "absolute top-8 right-10 text-gray-500 hover:text-gray-800
transition-all duration-500 ease-in-out" do %>
<i class="fa-fw fa-lg fas fa-times"></i>
<% end %>
<div class="w-full flex flex-col space-y-4 justify-center items-center">
<div class="p-8 flex flex-col space-y-4 justify-start items-center">
<%= live_component(@component, @opts) %>
</div>
</div>

View File

@ -1,4 +1,4 @@
<main class="container min-w-full min-h-full">
<main class="container min-w-full">
<header>
<CanneryWeb.Component.Topbar.topbar current_user={assigns[:current_user]}></CanneryWeb.Component.Topbar.topbar>
<div class="mx-8 my-2 flex flex-col space-y-4 text-center">

View File

@ -6,10 +6,23 @@ defmodule Cannery.Repo.Migrations.CreateAmmoTypes do
add :id, :binary_id, primary_key: true
add :name, :string
add :desc, :text
add :case_material, :string
# https://en.wikipedia.org/wiki/Bullet#Abbreviations
add :bullet_type, :string
add :grain, :integer
add :bullet_core, :string
add :cartridge, :string
add :caliber, :string
add :case_material, :string
add :grains, :integer
add :pressure, :string
add :rimfire, :boolean, null: false, default: false
add :tracer, :boolean, null: false, default: false
add :incendiary, :boolean, null: false, default: false
add :blank, :boolean, null: false, default: false
add :corrosive, :boolean, null: false, default: false
add :manufacturer, :string
add :sku, :string
timestamps()
end