cannery/lib/cannery_web/components/add_shot_record_component.ex

83 lines
2.3 KiB
Elixir
Raw Normal View History

2023-03-30 20:43:30 -04:00
defmodule CanneryWeb.Components.AddShotRecordComponent do
2022-02-15 17:33:45 -05:00
@moduledoc """
2023-03-30 20:43:30 -04:00
Livecomponent that can create 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.Pack}
alias Ecto.Changeset
2022-11-10 22:01:32 -05:00
alias Phoenix.LiveView.{JS, Socket}
2022-02-15 17:33:45 -05:00
@impl true
@spec update(
%{
required(:current_user) => User.t(),
2023-03-29 22:54:55 -04:00
required(:pack) => Pack.t(),
2022-02-15 17:33:45 -05:00
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
2023-03-29 22:54:55 -04:00
def update(%{pack: pack, current_user: current_user} = assigns, socket) do
2022-02-15 17:33:45 -05:00
changeset =
2023-03-30 20:43:30 -04:00
%ShotRecord{date: Date.utc_today()}
|> ShotRecord.create_changeset(current_user, pack, %{})
2022-02-15 17:33:45 -05:00
{:ok, socket |> assign(assigns) |> assign(:changeset, changeset)}
end
@impl true
def handle_event(
"validate",
2023-03-30 20:43:30 -04:00
%{"shot_record" => shot_record_params},
2023-03-29 22:54:55 -04:00
%{assigns: %{pack: pack, current_user: current_user}} = socket
2022-02-15 17:33:45 -05:00
) do
2023-03-30 20:43:30 -04:00
params = shot_record_params |> process_params(pack)
2022-02-15 17:33:45 -05:00
2023-03-30 20:43:30 -04:00
changeset = %ShotRecord{} |> ShotRecord.create_changeset(current_user, pack, params)
2022-02-15 17:33:45 -05:00
changeset =
case changeset |> Changeset.apply_action(:validate) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
2022-02-15 17:33:45 -05:00
{:noreply, socket |> assign(:changeset, changeset)}
end
def handle_event(
"save",
2023-03-30 20:43:30 -04:00
%{"shot_record" => shot_record_params},
2022-02-15 17:33:45 -05:00
%{
2023-03-29 22:54:55 -04:00
assigns: %{pack: pack, 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
shot_record_params
2023-03-29 22:54:55 -04:00
|> process_params(pack)
2023-03-30 20:43:30 -04:00
|> ActivityLog.create_shot_record(current_user, pack)
2022-02-15 17:33:45 -05:00
|> case do
2023-03-30 20:43:30 -04:00
{:ok, _shot_record} ->
2022-02-15 17:33:45 -05:00
prompt = dgettext("prompts", "Shots recorded successfully")
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
2022-02-15 17:33:45 -05:00
{:error, %Changeset{} = changeset} ->
2022-02-15 17:33:45 -05:00
socket |> assign(changeset: changeset)
end
{:noreply, socket}
end
# calculate count from shots left
2023-03-29 22:54:55 -04:00
defp process_params(params, %Pack{count: count}) do
2023-03-30 20:43:30 -04:00
shot_record_count =
if params |> Map.get("ammo_left", "") == "" do
nil
2022-02-15 17:33:45 -05:00
else
new_count = params |> Map.get("ammo_left") |> String.to_integer()
count - new_count
2022-02-15 17:33:45 -05:00
end
2023-03-30 20:43:30 -04:00
params |> Map.put("count", shot_record_count)
2022-02-15 17:33:45 -05:00
end
end