2022-02-15 17:33:45 -05:00
|
|
|
defmodule CanneryWeb.RangeLive.FormComponent do
|
|
|
|
@moduledoc """
|
2023-03-30 20:43:30 -04:00
|
|
|
Livecomponent that can update a ShotRecord
|
2022-02-15 17:33:45 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
use CanneryWeb, :live_component
|
2023-03-30 20:43:30 -04:00
|
|
|
alias Cannery.{Accounts.User, ActivityLog, ActivityLog.ShotRecord, Ammo, Ammo.Pack}
|
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
|
2023-03-29 22:54:55 -04:00
|
|
|
def mount(socket), do: {:ok, socket |> assign(:pack, nil)}
|
2023-03-18 21:06:00 -04:00
|
|
|
|
2022-02-15 17:33:45 -05:00
|
|
|
@impl true
|
|
|
|
@spec update(
|
|
|
|
%{
|
2023-03-30 20:43:30 -04:00
|
|
|
required(:shot_record) => ShotRecord.t(),
|
2022-02-15 17:33:45 -05:00
|
|
|
required(:current_user) => User.t(),
|
2023-03-29 22:54:55 -04:00
|
|
|
optional(:pack) => Pack.t(),
|
2022-02-15 17:33:45 -05:00
|
|
|
optional(any()) => any()
|
|
|
|
},
|
|
|
|
Socket.t()
|
|
|
|
) :: {:ok, Socket.t()}
|
|
|
|
def update(
|
|
|
|
%{
|
2023-03-30 20:43:30 -04:00
|
|
|
shot_record: %ShotRecord{pack_id: pack_id},
|
2022-02-15 17:33:45 -05:00
|
|
|
current_user: current_user
|
|
|
|
} = assigns,
|
|
|
|
socket
|
2023-03-18 21:06:00 -04:00
|
|
|
)
|
2023-03-29 22:54:55 -04:00
|
|
|
when is_binary(pack_id) do
|
|
|
|
pack = Ammo.get_pack!(pack_id, current_user)
|
|
|
|
{:ok, socket |> assign(assigns) |> assign(:pack, pack) |> assign_changeset(%{})}
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|
|
|
|
|
2023-03-30 20:43:30 -04:00
|
|
|
def update(%{shot_record: %ShotRecord{}} = assigns, socket) do
|
2023-03-18 21:06:00 -04:00
|
|
|
{: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
|
2023-03-30 20:43:30 -04:00
|
|
|
def handle_event("validate", %{"shot_record" => shot_record_params}, socket) do
|
|
|
|
{:noreply, socket |> assign_changeset(shot_record_params, :validate)}
|
2022-02-15 17:33:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event(
|
|
|
|
"save",
|
2023-03-30 20:43:30 -04:00
|
|
|
%{"shot_record" => shot_record_params},
|
|
|
|
%{assigns: %{shot_record: shot_record, current_user: current_user, return_to: return_to}} =
|
2022-02-15 17:33:45 -05:00
|
|
|
socket
|
|
|
|
) do
|
|
|
|
socket =
|
2023-03-30 20:43:30 -04:00
|
|
|
case ActivityLog.update_shot_record(shot_record, shot_record_params, current_user) do
|
|
|
|
{:ok, _shot_record} ->
|
2022-02-15 17:33:45 -05:00
|
|
|
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,
|
2023-03-29 22:54:55 -04:00
|
|
|
pack: pack,
|
2023-03-30 20:43:30 -04:00
|
|
|
shot_record: shot_record
|
2023-03-18 21:06:00 -04:00
|
|
|
}
|
|
|
|
} = socket,
|
2023-03-30 20:43:30 -04:00
|
|
|
shot_record_params,
|
2023-03-18 21:06:00 -04:00
|
|
|
action \\ nil
|
|
|
|
) do
|
|
|
|
default_action =
|
|
|
|
case live_action do
|
2023-03-30 20:43:30 -04:00
|
|
|
:add_shot_record -> :insert
|
|
|
|
editing when editing in [:edit, :edit_shot_record] -> :update
|
2023-03-18 21:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
changeset =
|
|
|
|
case default_action do
|
2023-03-30 20:43:30 -04:00
|
|
|
:insert -> shot_record |> ShotRecord.create_changeset(user, pack, shot_record_params)
|
|
|
|
:update -> shot_record |> ShotRecord.update_changeset(user, shot_record_params)
|
2023-03-18 21:06:00 -04:00
|
|
|
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
|