Fix ammo group create form error bug

This commit is contained in:
shibao 2022-03-04 23:16:37 -05:00
parent 9f2cc54738
commit 8bb4aab49c
4 changed files with 36 additions and 26 deletions

View File

@ -3,6 +3,7 @@
- Add link to changelog from version number - Add link to changelog from version number
- Fix some elements flashing with black background - Fix some elements flashing with black background
- Fix bug with moving ammo group to new container - Fix bug with moving ammo group to new container
- Fix bug with no error showing up for create ammo group form
# v0.3.0 # v0.3.0
- Fix ammo type counts not showing when count is 0 - Fix ammo type counts not showing when count is 0

View File

@ -342,7 +342,7 @@ defmodule Cannery.Ammo do
""" """
@spec create_ammo_groups(attrs :: map(), multiplier :: non_neg_integer(), User.t()) :: @spec create_ammo_groups(attrs :: map(), multiplier :: non_neg_integer(), User.t()) ::
{:ok, {count :: non_neg_integer(), [AmmoGroup.t()] | nil}} {:ok, {count :: non_neg_integer(), [AmmoGroup.t()] | nil}}
| {:error, Changeset.t(AmmoGroup.new_ammo_group()) | nil} | {:error, Changeset.t(AmmoGroup.new_ammo_group())}
def create_ammo_groups( def create_ammo_groups(
%{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs, %{"ammo_type_id" => ammo_type_id, "container_id" => container_id} = attrs,
multiplier, multiplier,
@ -361,26 +361,24 @@ defmodule Cannery.Ammo do
end) end)
if changesets |> Enum.all?(fn %{valid?: valid} -> valid end) do if changesets |> Enum.all?(fn %{valid?: valid} -> valid end) do
Multi.new() {count, inserted_ammo_groups} =
|> Multi.insert_all( Repo.insert_all(
:create_ammo_groups, AmmoGroup,
AmmoGroup, changesets
changesets |> Enum.map(fn changeset ->
|> Enum.map(fn changeset -> changeset
changeset |> Map.get(:changes)
|> Map.get(:changes) |> Map.merge(%{inserted_at: now, updated_at: now})
|> Map.merge(%{inserted_at: now, updated_at: now}) end),
end), returning: true
returning: true )
)
|> Repo.transaction() {:ok, {count, inserted_ammo_groups}}
|> case do
{:ok, %{create_ammo_groups: {count, ammo_groups}}} -> {:ok, {count, ammo_groups}}
{:error, :create_ammo_groups, changeset, _changes_so_far} -> {:error, changeset}
{:error, _other_transaction, _value, _changes_so_far} -> {:error, nil}
end
else else
{:error, changesets |> List.first()} changesets
|> Enum.reject(fn %{valid?: valid} -> valid end)
|> List.first()
|> Changeset.apply_action(:insert)
end end
end end

View File

@ -36,10 +36,23 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
def handle_event( def handle_event(
"validate", "validate",
%{"ammo_group" => ammo_group_params}, %{"ammo_group" => ammo_group_params},
%{assigns: %{ammo_group: ammo_group}} = socket %{assigns: %{action: action, ammo_group: ammo_group}} = socket
) do ) do
socket = socket |> assign(:changeset, ammo_group |> Ammo.change_ammo_group(ammo_group_params)) changeset_action =
{:noreply, socket} case action do
:new -> :insert
:edit -> :update
end
changeset = ammo_group |> Ammo.change_ammo_group(ammo_group_params)
changeset =
case changeset |> Changeset.apply_action(changeset_action) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
{:noreply, socket |> assign(:changeset, changeset)}
end end
def handle_event( def handle_event(
@ -142,9 +155,6 @@ defmodule CanneryWeb.AmmoGroupLive.FormComponent do
{:error, %Changeset{} = changeset} -> {:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset) socket |> assign(changeset: changeset)
{:error, nil} ->
socket
end end
end end
end end

View File

@ -69,6 +69,7 @@
) %> ) %>
<%= error_tag(f, :multiplier, "col-span-3 text-center") %> <%= error_tag(f, :multiplier, "col-span-3 text-center") %>
<% :edit -> %> <% :edit -> %>
<%= submit(dgettext("actions", "Save"), <%= submit(dgettext("actions", "Save"),
phx_disable_with: dgettext("prompts", "Saving..."), phx_disable_with: dgettext("prompts", "Saving..."),