cannery/lib/cannery_web/live/ammo_type_live/form_component.ex

190 lines
7.5 KiB
Elixir
Raw Normal View History

2021-09-02 23:31:14 -04:00
defmodule CanneryWeb.AmmoTypeLive.FormComponent do
2022-01-22 21:40:29 -05:00
@moduledoc """
Livecomponent that can update or create an Cannery.Ammo.AmmoType
"""
2021-09-02 23:31:14 -04:00
use CanneryWeb, :live_component
alias Cannery.Ammo
2022-02-01 00:22:44 -05:00
alias Ecto.Changeset
2021-09-02 23:31:14 -04:00
@impl true
def update(%{ammo_type: ammo_type} = assigns, socket) do
changeset = Ammo.change_ammo_type(ammo_type)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"ammo_type" => ammo_type_params}, socket) do
changeset = socket.assigns.ammo_type |> Ammo.change_ammo_type(ammo_type_params)
2021-09-02 23:31:14 -04:00
{:noreply, socket |> assign(:changeset, changeset)}
end
def handle_event("save", %{"ammo_type" => ammo_type_params}, socket) do
save_ammo_type(socket, socket.assigns.action, ammo_type_params)
end
@impl true
def render(assigns) do
~H"""
<div>
2022-02-05 00:09:22 -05:00
<h2 class="text-center title text-xl text-primary-500">
2022-01-22 17:21:10 -05:00
<%= @title %>
</h2>
<.form
let={f}
for={@changeset}
id="ammo_type-form"
phx-target={@myself}
phx-change="validate"
2022-01-31 20:14:17 -05:00
phx-submit="save"
2022-01-31 23:03:13 -05:00
class="grid grid-cols-3 justify-center items-center space-y-4"
2022-01-31 20:14:17 -05:00
>
<%= if @changeset.action do %>
<div class="invalid-feedback col-span-3 text-center">
<%= changeset_errors(@changeset) %>
</div>
<% end %>
2022-02-09 00:39:27 -05:00
<%= label(f, :name, gettext("Name"), class: "mr-4 title text-lg text-primary-500") %>
2022-01-31 23:03:13 -05:00
<%= text_input(f, :name, class: "text-center col-span-2 input input-primary") %>
<%= error_tag(f, :name, "col-span-3 text-center") %>
2022-01-31 23:03:13 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :desc, gettext("Description"), class: "mr-4 title text-lg text-primary-500") %>
2022-01-31 23:03:13 -05:00
<%= textarea(f, :desc,
class: "text-center col-span-2 input input-primary",
2022-02-01 01:08:18 -05:00
phx_hook: "MaintainAttrs"
) %>
<%= error_tag(f, :desc, "col-span-3 text-center") %>
2022-01-31 23:03:13 -05:00
2022-02-05 16:22:02 -05:00
<a
href="https://en.wikipedia.org/wiki/Bullet#Abbreviations"
class="col-span-3 text-center link title text-md text-primary-600"
>
2022-02-09 00:39:27 -05:00
<%= gettext("Example bullet type abbreviations") %>
2022-02-05 16:22:02 -05:00
</a>
2022-02-09 00:39:27 -05:00
<%= label(f, :bullet_type, gettext("Bullet type"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= text_input(f, :bullet_type,
class: "text-center col-span-2 input input-primary",
2022-02-09 00:39:27 -05:00
placeholder: gettext("FMJ")
2022-02-05 16:22:02 -05:00
) %>
<%= error_tag(f, :bullet_type, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :bullet_core, gettext("Bullet core"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= text_input(f, :bullet_core,
class: "text-center col-span-2 input input-primary",
2022-02-09 00:39:27 -05:00
placeholder: gettext("Steel")
2022-02-05 16:22:02 -05:00
) %>
<%= error_tag(f, :bullet_core, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :cartridge, gettext("Cartridge"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= text_input(f, :cartridge,
class: "text-center col-span-2 input input-primary",
placeholder: "5.56x46mm NATO"
) %>
<%= error_tag(f, :cartridge, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :caliber, gettext("Caliber"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= text_input(f, :caliber,
class: "text-center col-span-2 input input-primary",
placeholder: ".223"
) %>
<%= error_tag(f, :caliber, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :case_material, gettext("Case material"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= text_input(f, :case_material,
class: "text-center col-span-2 input input-primary",
2022-02-09 00:39:27 -05:00
placeholder: gettext("Brass")
2022-02-05 16:22:02 -05:00
) %>
<%= error_tag(f, :case_material, "col-span-3 text-center") %>
2022-01-31 23:03:13 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :grains, gettext("Grains"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= number_input(f, :grains,
step: "1",
class: "text-center col-span-2 input input-primary",
min: 1
) %>
<%= error_tag(f, :grains, "col-span-3 text-center") %>
2022-01-31 23:03:13 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :pressure, gettext("Pressure"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= text_input(f, :pressure,
2022-01-31 23:03:13 -05:00
class: "text-center col-span-2 input input-primary",
2022-02-05 16:22:02 -05:00
placeholder: "+P"
2022-02-01 01:08:18 -05:00
) %>
<%= error_tag(f, :pressure, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :primer_type, gettext("Primer type"), class: "mr-4 title text-lg text-primary-500") %>
<%= text_input(f, :primer_type,
class: "text-center col-span-2 input input-primary",
placeholder: "Boxer"
) %>
<%= error_tag(f, :primer_type, "col-span-3 text-center") %>
2022-02-09 00:39:27 -05:00
<%= label(f, :rimfire, gettext("Rimfire"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= checkbox(f, :rimfire, class: "text-center col-span-2 checkbox") %>
<%= error_tag(f, :rimfire, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :tracer, gettext("Tracer"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= checkbox(f, :tracer, class: "text-center col-span-2 checkbox") %>
<%= error_tag(f, :tracer, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :incendiary, gettext("Incendiary"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= checkbox(f, :incendiary, class: "text-center col-span-2 checkbox") %>
<%= error_tag(f, :incendiary, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :blank, gettext("Blank"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= checkbox(f, :blank, class: "text-center col-span-2 checkbox") %>
<%= error_tag(f, :blank, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :corrosive, gettext("Corrosive"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= checkbox(f, :corrosive, class: "text-center col-span-2 checkbox") %>
<%= error_tag(f, :corrosive, "col-span-3 text-center") %>
2022-01-31 23:03:13 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :manufacturer, gettext("Manufacturer"), class: "mr-4 title text-lg text-primary-500") %>
2022-01-31 23:03:13 -05:00
<%= text_input(f, :manufacturer, class: "text-center col-span-2 input input-primary") %>
<%= error_tag(f, :manufacturer, "col-span-3 text-center") %>
2022-01-31 23:03:13 -05:00
2022-02-09 00:39:27 -05:00
<%= label(f, :sku, gettext("SKU"), class: "mr-4 title text-lg text-primary-500") %>
2022-02-05 16:22:02 -05:00
<%= text_input(f, :sku, class: "text-center col-span-2 input input-primary") %>
<%= error_tag(f, :sku, "col-span-3 text-center") %>
2022-02-05 16:22:02 -05:00
2022-02-09 00:39:27 -05:00
<%= submit(dgettext("actions", "Save"),
phx_disable_with: dgettext("prompts", "Saving..."),
2022-02-01 01:08:18 -05:00
class: "mx-auto col-span-3 btn btn-primary"
) %>
</.form>
</div>
"""
end
2021-09-02 23:31:14 -04:00
defp save_ammo_type(socket, :edit, ammo_type_params) do
case Ammo.update_ammo_type(socket.assigns.ammo_type, ammo_type_params) do
{:ok, _ammo_type} ->
{:noreply,
socket
2022-02-09 00:39:27 -05:00
|> put_flash(:info, dgettext("prompts", "Ammo type updated successfully"))
2021-09-02 23:31:14 -04:00
|> push_redirect(to: socket.assigns.return_to)}
2022-02-01 00:22:44 -05:00
{:error, %Changeset{} = changeset} ->
2021-09-02 23:31:14 -04:00
{:noreply, socket |> assign(:changeset, changeset)}
end
end
defp save_ammo_type(socket, :new, ammo_type_params) do
case Ammo.create_ammo_type(ammo_type_params) do
{:ok, _ammo_type} ->
{:noreply,
socket
2022-02-09 00:39:27 -05:00
|> put_flash(:info, dgettext("prompts", "Ammo type created successfully"))
2021-09-02 23:31:14 -04:00
|> push_redirect(to: socket.assigns.return_to)}
2022-02-01 00:22:44 -05:00
{:error, %Changeset{} = changeset} ->
2021-09-02 23:31:14 -04:00
{:noreply, socket |> assign(changeset: changeset)}
end
end
end