2021-09-02 23:31:14 -04:00
|
|
|
defmodule CanneryWeb.AmmoGroupLive.FormComponent do
|
2022-01-22 21:40:29 -05:00
|
|
|
@moduledoc """
|
|
|
|
Livecomponent that can update or create an Cannery.Ammo.AmmoGroup
|
|
|
|
"""
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
use CanneryWeb, :live_component
|
|
|
|
|
2022-02-05 01:59:40 -05:00
|
|
|
alias Cannery.{Ammo, Containers}
|
|
|
|
alias Cannery.{Ammo.AmmoType, Containers.Container}
|
|
|
|
alias Ecto.Changeset
|
2021-09-02 23:31:14 -04:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def update(%{ammo_group: ammo_group} = assigns, socket) do
|
2022-02-05 01:59:40 -05:00
|
|
|
socket = socket |> assign(assigns)
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
changeset = Ammo.change_ammo_group(ammo_group)
|
2022-02-05 01:59:40 -05:00
|
|
|
containers = Containers.list_containers(socket.assigns.current_user)
|
|
|
|
ammo_types = Ammo.list_ammo_types()
|
2021-09-02 23:31:14 -04:00
|
|
|
|
2022-02-05 01:59:40 -05:00
|
|
|
{:ok, socket |> assign(changeset: changeset, containers: containers, ammo_types: ammo_types)}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("validate", %{"ammo_group" => ammo_group_params}, socket) do
|
2022-02-05 01:59:40 -05:00
|
|
|
ammo_group_params = ammo_group_params |> Map.put("user_id", socket.assigns.current_user.id)
|
2022-02-05 23:41:10 -05:00
|
|
|
changeset = socket.assigns.ammo_group |> Ammo.change_ammo_group(ammo_group_params)
|
2021-09-02 23:31:14 -04:00
|
|
|
{:noreply, socket |> assign(:changeset, changeset)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("save", %{"ammo_group" => ammo_group_params}, socket) do
|
2022-02-05 01:59:40 -05:00
|
|
|
ammo_group_params = ammo_group_params |> Map.put("user_id", socket.assigns.current_user.id)
|
2021-09-02 23:31:14 -04:00
|
|
|
save_ammo_group(socket, socket.assigns.action, ammo_group_params)
|
|
|
|
end
|
|
|
|
|
2022-01-22 15:15:23 -05:00
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
|
|
|
<div>
|
2022-02-05 01:59:40 -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}
|
2022-01-22 15:15:23 -05:00
|
|
|
id="ammo_group-form"
|
|
|
|
phx-target={@myself}
|
|
|
|
phx-change="validate"
|
2022-01-31 20:14:17 -05:00
|
|
|
phx-submit="save"
|
2022-02-05 01:59:40 -05:00
|
|
|
class="grid grid-cols-3 justify-center items-center space-y-4"
|
2022-01-31 20:14:17 -05:00
|
|
|
>
|
2022-02-05 23:41:10 -05:00
|
|
|
<%= if @changeset.action do %>
|
|
|
|
<div class="invalid-feedback col-span-3 text-center">
|
|
|
|
<%= changeset_errors(@changeset) %>
|
|
|
|
</div>
|
|
|
|
<% end %>
|
|
|
|
|
2022-02-09 00:20:04 -05:00
|
|
|
<%= label(f, :ammo_type_id, gettext("Ammo type"), class: "mr-4 title text-lg text-primary-500") %>
|
2022-02-06 00:40:36 -05:00
|
|
|
<%= select(f, :ammo_type_id, ammo_type_options(@ammo_types),
|
|
|
|
class: "text-center col-span-2 input input-primary"
|
|
|
|
) %>
|
|
|
|
<%= error_tag(f, :ammo_type_id, "col-span-3 text-center") %>
|
|
|
|
|
2022-02-09 00:20:04 -05:00
|
|
|
<%= label(f, :count, gettext("Count"), class: "mr-4 title text-lg text-primary-500") %>
|
2022-02-05 01:59:40 -05:00
|
|
|
<%= number_input(f, :count,
|
|
|
|
class: "text-center col-span-2 input input-primary",
|
|
|
|
min: 1
|
|
|
|
) %>
|
2022-02-05 23:41:10 -05:00
|
|
|
<%= error_tag(f, :count, "col-span-3 text-center") %>
|
2022-02-05 01:59:40 -05:00
|
|
|
|
2022-02-09 00:20:04 -05:00
|
|
|
<%= label(f, :price_paid, gettext("Price paid"), class: "mr-4 title text-lg text-primary-500") %>
|
2022-02-05 01:59:40 -05:00
|
|
|
<%= number_input(f, :price_paid,
|
|
|
|
step: "0.01",
|
|
|
|
class: "text-center col-span-2 input input-primary"
|
|
|
|
) %>
|
2022-02-05 23:41:10 -05:00
|
|
|
<%= error_tag(f, :price_paid, "col-span-3 text-center") %>
|
2022-02-05 01:59:40 -05:00
|
|
|
|
2022-02-09 00:20:04 -05:00
|
|
|
<%= label(f, :notes, gettext("Notes"), class: "mr-4 title text-lg text-primary-500") %>
|
2022-02-05 01:59:40 -05:00
|
|
|
<%= textarea(f, :notes,
|
|
|
|
class: "text-center col-span-2 input input-primary",
|
|
|
|
phx_hook: "MaintainAttrs"
|
|
|
|
) %>
|
2022-02-05 23:41:10 -05:00
|
|
|
<%= error_tag(f, :notes, "col-span-3 text-center") %>
|
2022-02-05 01:59:40 -05:00
|
|
|
|
2022-02-09 00:20:04 -05:00
|
|
|
<%= label(f, :container, gettext("Container"), class: "mr-4 title text-lg text-primary-500") %>
|
2022-02-05 01:59:40 -05:00
|
|
|
<%= select(f, :container_id, container_options(@containers),
|
|
|
|
class: "text-center col-span-2 input input-primary"
|
|
|
|
) %>
|
2022-02-05 23:41:10 -05:00
|
|
|
<%= error_tag(f, :container_id, "col-span-3 text-center") %>
|
2022-02-05 01:59:40 -05:00
|
|
|
|
2022-02-09 00:20:04 -05:00
|
|
|
<%= submit(dgettext("actions", "Save"),
|
|
|
|
phx_disable_with: dgettext("prompts", "Saving..."),
|
2022-02-05 01:59:40 -05:00
|
|
|
class: "mx-auto col-span-3 btn btn-primary"
|
|
|
|
) %>
|
2022-01-22 15:15:23 -05:00
|
|
|
</.form>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2022-02-05 01:59:40 -05:00
|
|
|
# HTML Helpers
|
|
|
|
@spec container_options([Container.t()]) :: [{String.t(), Container.id()}]
|
|
|
|
defp container_options(containers) do
|
|
|
|
containers |> Enum.map(fn container -> {container.name, container.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 ammo_type -> {ammo_type.name, ammo_type.id} end)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Save Helpers
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
defp save_ammo_group(socket, :edit, ammo_group_params) do
|
2022-02-05 01:59:40 -05:00
|
|
|
socket.assigns.ammo_group
|
|
|
|
|> Ammo.update_ammo_group(ammo_group_params)
|
|
|
|
|> case do
|
2021-09-02 23:31:14 -04:00
|
|
|
{:ok, _ammo_group} ->
|
|
|
|
{:noreply,
|
|
|
|
socket
|
2022-02-09 00:20:04 -05:00
|
|
|
|> put_flash(:info, dgettext("prompts", "Ammo group updated successfully"))
|
2021-09-02 23:31:14 -04:00
|
|
|
|> push_redirect(to: socket.assigns.return_to)}
|
|
|
|
|
2022-02-05 01:59:40 -05:00
|
|
|
{:error, %Changeset{} = changeset} ->
|
2021-09-02 23:31:14 -04:00
|
|
|
{:noreply, socket |> assign(:changeset, changeset)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp save_ammo_group(socket, :new, ammo_group_params) do
|
|
|
|
case Ammo.create_ammo_group(ammo_group_params) do
|
|
|
|
{:ok, _ammo_group} ->
|
|
|
|
{:noreply,
|
|
|
|
socket
|
2022-02-09 00:20:04 -05:00
|
|
|
|> put_flash(:info, dgettext("prompts", "Ammo group created successfully"))
|
2021-09-02 23:31:14 -04:00
|
|
|
|> push_redirect(to: socket.assigns.return_to)}
|
|
|
|
|
2022-02-05 01:59:40 -05:00
|
|
|
{:error, %Changeset{} = changeset} ->
|
2021-09-02 23:31:14 -04:00
|
|
|
{:noreply, socket |> assign(changeset: changeset)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|