fix changesets
This commit is contained in:
@ -33,7 +33,9 @@ defmodule CanneryWeb.Components.AddShotRecordComponent do
|
||||
) do
|
||||
params = shot_record_params |> process_params(pack)
|
||||
|
||||
changeset = %ShotRecord{} |> ShotRecord.create_changeset(current_user, pack, params)
|
||||
changeset =
|
||||
%ShotRecord{}
|
||||
|> ShotRecord.create_changeset(current_user, pack, params)
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(:validate) do
|
||||
|
@ -19,7 +19,7 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"container" => container_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(container_params)}
|
||||
{:noreply, socket |> assign_changeset(container_params, :validate)}
|
||||
end
|
||||
|
||||
def handle_event(
|
||||
@ -32,14 +32,9 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
|
||||
defp assign_changeset(
|
||||
%{assigns: %{action: action, container: container, current_user: user}} = socket,
|
||||
container_params
|
||||
container_params,
|
||||
changeset_action \\ nil
|
||||
) do
|
||||
changeset_action =
|
||||
case action do
|
||||
create when create in [:new, :clone] -> :insert
|
||||
:edit -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case action do
|
||||
create when create in [:new, :clone] ->
|
||||
@ -50,9 +45,13 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
if changeset_action do
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
else
|
||||
changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
|
@ -19,7 +19,7 @@ defmodule CanneryWeb.InviteLive.FormComponent do
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"invite" => invite_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(invite_params)}
|
||||
{:noreply, socket |> assign_changeset(invite_params, :validate)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"invite" => invite_params}, %{assigns: %{action: action}} = socket) do
|
||||
@ -28,14 +28,9 @@ defmodule CanneryWeb.InviteLive.FormComponent do
|
||||
|
||||
defp assign_changeset(
|
||||
%{assigns: %{action: action, current_user: user, invite: invite}} = socket,
|
||||
invite_params
|
||||
invite_params,
|
||||
changeset_action \\ nil
|
||||
) do
|
||||
changeset_action =
|
||||
case action do
|
||||
:new -> :insert
|
||||
:edit -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case action do
|
||||
:new -> Invite.create_changeset(user, "example_token", invite_params)
|
||||
@ -43,9 +38,13 @@ defmodule CanneryWeb.InviteLive.FormComponent do
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
if changeset_action do
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
else
|
||||
changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
|
@ -22,24 +22,13 @@ defmodule CanneryWeb.PackLive.FormComponent do
|
||||
|
||||
@spec update(Socket.t()) :: {:ok, Socket.t()}
|
||||
def update(%{assigns: %{current_user: current_user}} = socket) do
|
||||
%{assigns: %{types: types, containers: containers}} =
|
||||
socket =
|
||||
socket =
|
||||
socket
|
||||
|> assign(:pack_create_limit, @pack_create_limit)
|
||||
|> assign(:types, Ammo.list_types(current_user))
|
||||
|> assign_new(:containers, fn -> Containers.list_containers(current_user) end)
|
||||
|
||||
params =
|
||||
if types |> List.first() |> is_nil(),
|
||||
do: %{},
|
||||
else: %{} |> Map.put("type_id", types |> List.first() |> Map.get(:id))
|
||||
|
||||
params =
|
||||
if containers |> List.first() |> is_nil(),
|
||||
do: params,
|
||||
else: params |> Map.put("container_id", containers |> List.first() |> Map.get(:id))
|
||||
|
||||
{:ok, socket |> assign_changeset(params)}
|
||||
{:ok, socket |> assign_changeset(%{})}
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -92,9 +81,13 @@ defmodule CanneryWeb.PackLive.FormComponent do
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action || default_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
if changeset_action do
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
else
|
||||
changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
|
@ -71,24 +71,25 @@ defmodule CanneryWeb.RangeLive.FormComponent do
|
||||
}
|
||||
} = socket,
|
||||
shot_record_params,
|
||||
action \\ nil
|
||||
changeset_action \\ nil
|
||||
) do
|
||||
default_action =
|
||||
changeset =
|
||||
case live_action do
|
||||
:add_shot_record -> :insert
|
||||
editing when editing in [:edit, :edit_shot_record] -> :update
|
||||
:add_shot_record ->
|
||||
shot_record |> ShotRecord.create_changeset(user, pack, shot_record_params)
|
||||
|
||||
editing when editing in [:edit, :edit_shot_record] ->
|
||||
shot_record |> ShotRecord.update_changeset(user, shot_record_params)
|
||||
end
|
||||
|
||||
changeset =
|
||||
case default_action do
|
||||
:insert -> shot_record |> ShotRecord.create_changeset(user, pack, shot_record_params)
|
||||
:update -> shot_record |> ShotRecord.update_changeset(user, shot_record_params)
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(action || default_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
if changeset_action do
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
else
|
||||
changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
|
@ -17,7 +17,7 @@ defmodule CanneryWeb.TagLive.FormComponent do
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"tag" => tag_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(tag_params)}
|
||||
{:noreply, socket |> assign_changeset(tag_params, :validate)}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"tag" => tag_params}, %{assigns: %{action: action}} = socket) do
|
||||
@ -26,14 +26,9 @@ defmodule CanneryWeb.TagLive.FormComponent do
|
||||
|
||||
defp assign_changeset(
|
||||
%{assigns: %{action: action, current_user: user, tag: tag}} = socket,
|
||||
tag_params
|
||||
tag_params,
|
||||
changeset_action \\ nil
|
||||
) do
|
||||
changeset_action =
|
||||
case action do
|
||||
:new -> :insert
|
||||
:edit -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case action do
|
||||
:new -> tag |> Tag.create_changeset(user, tag_params)
|
||||
@ -41,9 +36,13 @@ defmodule CanneryWeb.TagLive.FormComponent do
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
if changeset_action do
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
else
|
||||
changeset
|
||||
end
|
||||
|
||||
socket |> assign(:changeset, changeset)
|
||||
|
@ -19,7 +19,7 @@ defmodule CanneryWeb.TypeLive.FormComponent do
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"type" => type_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(type_params)}
|
||||
{:noreply, socket |> assign_changeset(type_params, :validate)}
|
||||
end
|
||||
|
||||
def handle_event(
|
||||
@ -32,14 +32,9 @@ defmodule CanneryWeb.TypeLive.FormComponent do
|
||||
|
||||
defp assign_changeset(
|
||||
%{assigns: %{action: action, type: type, current_user: user}} = socket,
|
||||
type_params
|
||||
type_params,
|
||||
changeset_action \\ nil
|
||||
) do
|
||||
changeset_action =
|
||||
case action do
|
||||
create when create in [:new, :clone] -> :insert
|
||||
:edit -> :update
|
||||
end
|
||||
|
||||
changeset =
|
||||
case action do
|
||||
create when create in [:new, :clone] ->
|
||||
@ -50,9 +45,13 @@ defmodule CanneryWeb.TypeLive.FormComponent do
|
||||
end
|
||||
|
||||
changeset =
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
if changeset_action do
|
||||
case changeset |> Changeset.apply_action(changeset_action) do
|
||||
{:ok, _data} -> changeset
|
||||
{:error, changeset} -> changeset
|
||||
end
|
||||
else
|
||||
changeset
|
||||
end
|
||||
|
||||
socket |> assign(changeset: changeset)
|
||||
|
Reference in New Issue
Block a user