2023-03-30 21:53:52 -04:00
|
|
|
defmodule CanneryWeb.TypeLive.FormComponent do
|
2022-01-22 21:40:29 -05:00
|
|
|
@moduledoc """
|
2023-03-30 21:53:52 -04:00
|
|
|
Livecomponent that can update or create an Cannery.Ammo.Type
|
2022-01-22 21:40:29 -05:00
|
|
|
"""
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
use CanneryWeb, :live_component
|
2023-03-30 21:53:52 -04:00
|
|
|
alias Cannery.{Accounts.User, Ammo, Ammo.Type}
|
2022-02-01 00:22:44 -05:00
|
|
|
alias Ecto.Changeset
|
2022-02-10 23:36:31 -05:00
|
|
|
alias Phoenix.LiveView.Socket
|
2021-09-02 23:31:14 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-02-10 23:36:31 -05:00
|
|
|
@spec update(
|
2023-03-30 21:53:52 -04:00
|
|
|
%{:type => Type.t(), :current_user => User.t(), optional(any) => any},
|
2022-02-10 23:36:31 -05:00
|
|
|
Socket.t()
|
|
|
|
) :: {:ok, Socket.t()}
|
2022-07-04 20:39:21 -04:00
|
|
|
def update(%{current_user: _current_user} = assigns, socket) do
|
|
|
|
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2023-03-30 21:53:52 -04:00
|
|
|
def handle_event("validate", %{"type" => type_params}, socket) do
|
2024-07-28 13:17:33 -04:00
|
|
|
{:noreply, socket |> assign_changeset(type_params, :validate)}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-02-10 00:57:56 -05:00
|
|
|
def handle_event(
|
|
|
|
"save",
|
2023-03-30 21:53:52 -04:00
|
|
|
%{"type" => type_params},
|
2022-02-10 00:57:56 -05:00
|
|
|
%{assigns: %{action: action}} = socket
|
|
|
|
) do
|
2023-03-30 21:53:52 -04:00
|
|
|
save_type(socket, action, type_params)
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-07-04 20:39:21 -04:00
|
|
|
defp assign_changeset(
|
2023-03-30 21:53:52 -04:00
|
|
|
%{assigns: %{action: action, type: type, current_user: user}} = socket,
|
2024-07-28 13:17:33 -04:00
|
|
|
type_params,
|
|
|
|
changeset_action \\ nil
|
2022-07-04 20:39:21 -04:00
|
|
|
) do
|
|
|
|
changeset =
|
2023-03-18 21:06:00 -04:00
|
|
|
case action do
|
|
|
|
create when create in [:new, :clone] ->
|
2023-03-30 21:53:52 -04:00
|
|
|
type |> Type.create_changeset(user, type_params)
|
2023-03-18 21:06:00 -04:00
|
|
|
|
|
|
|
:edit ->
|
2023-03-30 21:53:52 -04:00
|
|
|
type |> Type.update_changeset(type_params)
|
2022-07-04 20:39:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
changeset =
|
2024-07-28 13:17:33 -04:00
|
|
|
if changeset_action do
|
|
|
|
case changeset |> Changeset.apply_action(changeset_action) do
|
|
|
|
{:ok, _data} -> changeset
|
|
|
|
{:error, changeset} -> changeset
|
|
|
|
end
|
|
|
|
else
|
|
|
|
changeset
|
2022-07-04 20:39:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
socket |> assign(changeset: changeset)
|
|
|
|
end
|
|
|
|
|
2023-03-30 21:53:52 -04:00
|
|
|
defp save_type(
|
|
|
|
%{assigns: %{type: type, current_user: current_user, return_to: return_to}} = socket,
|
2022-02-10 00:57:56 -05:00
|
|
|
:edit,
|
2023-03-30 21:53:52 -04:00
|
|
|
type_params
|
2022-02-10 00:57:56 -05:00
|
|
|
) do
|
|
|
|
socket =
|
2023-03-30 21:53:52 -04:00
|
|
|
case Ammo.update_type(type, type_params, current_user) do
|
|
|
|
{:ok, %{name: type_name}} ->
|
|
|
|
prompt = dgettext("prompts", "%{name} updated successfully", name: type_name)
|
2022-11-07 22:36:38 -05:00
|
|
|
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
|
2022-02-10 00:57:56 -05:00
|
|
|
|
|
|
|
{:error, %Changeset{} = changeset} ->
|
|
|
|
socket |> assign(:changeset, changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2023-03-30 21:53:52 -04:00
|
|
|
defp save_type(
|
2022-02-14 01:26:51 -05:00
|
|
|
%{assigns: %{current_user: current_user, return_to: return_to}} = socket,
|
2022-11-10 19:10:28 -05:00
|
|
|
action,
|
2023-03-30 21:53:52 -04:00
|
|
|
type_params
|
2022-11-10 19:10:28 -05:00
|
|
|
)
|
|
|
|
when action in [:new, :clone] do
|
2022-02-10 00:57:56 -05:00
|
|
|
socket =
|
2023-03-30 21:53:52 -04:00
|
|
|
case Ammo.create_type(type_params, current_user) do
|
|
|
|
{:ok, %{name: type_name}} ->
|
|
|
|
prompt = dgettext("prompts", "%{name} created successfully", name: type_name)
|
2022-11-07 22:36:38 -05:00
|
|
|
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
|
2022-02-10 00:57:56 -05:00
|
|
|
|
|
|
|
{:error, %Changeset{} = changeset} ->
|
|
|
|
socket |> assign(changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
end
|