2022-02-15 17:33:45 -05:00
|
|
|
defmodule CanneryWeb.RangeLive.FormComponent do
|
|
|
|
@moduledoc """
|
2022-07-01 21:39:08 -04:00
|
|
|
Livecomponent that can update a ShotGroup
|
2022-02-15 17:33:45 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
use CanneryWeb, :live_component
|
|
|
|
alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotGroup, Ammo, Ammo.AmmoGroup}
|
2023-03-18 21:06:00 -04:00
|
|
|
alias Ecto.Changeset
|
2022-02-15 17:33:45 -05:00
|
|
|
alias Phoenix.LiveView.Socket
|
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
@impl true
|
|
|
|
def mount(socket), do: {:ok, socket |> assign(:ammo_group, nil)}
|
|
|
|
|
2022-02-15 17:33:45 -05:00
|
|
|
@impl true
|
|
|
|
@spec update(
|
|
|
|
%{
|
|
|
|
required(:shot_group) => ShotGroup.t(),
|
|
|
|
required(:current_user) => User.t(),
|
|
|
|
optional(:ammo_group) => AmmoGroup.t(),
|
|
|
|
optional(any()) => any()
|
|
|
|
},
|
|
|
|
Socket.t()
|
|
|
|
) :: {:ok, Socket.t()}
|
|
|
|
def update(
|
|
|
|
%{
|
2023-03-18 21:06:00 -04:00
|
|
|
shot_group: %ShotGroup{ammo_group_id: ammo_group_id},
|
2022-02-15 17:33:45 -05:00
|
|
|
current_user: current_user
|
|
|
|
} = assigns,
|
|
|
|
socket
|
2023-03-18 21:06:00 -04:00
|
|
|
)
|
|
|
|
when is_binary(ammo_group_id) do
|
2022-02-15 17:33:45 -05:00
|
|
|
ammo_group = Ammo.get_ammo_group!(ammo_group_id, current_user)
|
2023-03-18 21:06:00 -04:00
|
|
|
{:ok, socket |> assign(assigns) |> assign(:ammo_group, ammo_group) |> assign_changeset(%{})}
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
def update(%{shot_group: %ShotGroup{}} = assigns, socket) do
|
|
|
|
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
|
|
|
|
end
|
2022-02-15 17:33:45 -05:00
|
|
|
|
2023-03-18 21:06:00 -04:00
|
|
|
@impl true
|
|
|
|
def handle_event("validate", %{"shot_group" => shot_group_params}, socket) do
|
|
|
|
{:noreply, socket |> assign_changeset(shot_group_params, :validate)}
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event(
|
|
|
|
"save",
|
|
|
|
%{"shot_group" => shot_group_params},
|
|
|
|
%{assigns: %{shot_group: shot_group, current_user: current_user, return_to: return_to}} =
|
|
|
|
socket
|
|
|
|
) do
|
|
|
|
socket =
|
|
|
|
case ActivityLog.update_shot_group(shot_group, shot_group_params, current_user) do
|
|
|
|
{:ok, _shot_group} ->
|
|
|
|
prompt = dgettext("prompts", "Shot records updated successfully")
|
2022-11-07 22:36:38 -05:00
|
|
|
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
|
2022-02-15 17:33:45 -05:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
socket |> assign(:changeset, changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
2023-03-18 21:06:00 -04:00
|
|
|
|
|
|
|
defp assign_changeset(
|
|
|
|
%{
|
|
|
|
assigns: %{
|
|
|
|
action: live_action,
|
|
|
|
current_user: user,
|
|
|
|
ammo_group: ammo_group,
|
|
|
|
shot_group: shot_group
|
|
|
|
}
|
|
|
|
} = socket,
|
|
|
|
shot_group_params,
|
|
|
|
action \\ nil
|
|
|
|
) do
|
|
|
|
default_action =
|
|
|
|
case live_action do
|
|
|
|
:add_shot_group -> :insert
|
|
|
|
editing when editing in [:edit, :edit_shot_group] -> :update
|
|
|
|
end
|
|
|
|
|
|
|
|
changeset =
|
|
|
|
case default_action do
|
|
|
|
:insert -> shot_group |> ShotGroup.create_changeset(user, ammo_group, shot_group_params)
|
|
|
|
:update -> shot_group |> ShotGroup.update_changeset(user, shot_group_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
changeset =
|
|
|
|
case changeset |> Changeset.apply_action(action || default_action) do
|
|
|
|
{:ok, _data} -> changeset
|
|
|
|
{:error, changeset} -> changeset
|
|
|
|
end
|
|
|
|
|
|
|
|
socket |> assign(:changeset, changeset)
|
|
|
|
end
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|