cannery/lib/cannery_web/live/ammo_type_live/form_component.ex

98 lines
2.7 KiB
Elixir
Raw Normal View History

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
{:noreply, socket |> assign_changeset(type_params)}
2021-09-02 23:31:14 -04:00
end
def handle_event(
"save",
2023-03-30 21:53:52 -04:00
%{"type" => type_params},
%{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,
type_params
2022-07-04 20:39:21 -04:00
) do
changeset_action =
case action do
create when create in [:new, :clone] -> :insert
:edit -> :update
2022-07-04 20:39:21 -04:00
end
changeset =
case action do
create when create in [:new, :clone] ->
2023-03-30 21:53:52 -04:00
type |> Type.create_changeset(user, type_params)
: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 =
case changeset |> Changeset.apply_action(changeset_action) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
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,
:edit,
2023-03-30 21:53:52 -04:00
type_params
) 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)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
{: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
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)
socket |> put_flash(:info, prompt) |> push_navigate(to: return_to)
{:error, %Changeset{} = changeset} ->
socket |> assign(changeset: changeset)
end
{:noreply, socket}
2021-09-02 23:31:14 -04:00
end
end