use container changeset helper

This commit is contained in:
shibao 2022-07-04 20:52:21 -04:00
parent 76bbab1de9
commit 36f56528ee
6 changed files with 39 additions and 20 deletions

View File

@ -13,19 +13,13 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
%{:container => Container.t(), :current_user => User.t(), optional(any) => any}, %{:container => Container.t(), :current_user => User.t(), optional(any) => any},
Socket.t() Socket.t()
) :: {:ok, Socket.t()} ) :: {:ok, Socket.t()}
def update(%{container: container} = assigns, socket) do def update(%{container: _container} = assigns, socket) do
changeset = container |> Container.update_changeset(%{}) {:ok, socket |> assign(assigns) |> assign_changeset(%{})}
{:ok, socket |> assign(assigns) |> assign(:changeset, changeset)}
end end
@impl true @impl true
def handle_event( def handle_event("validate", %{"container" => container_params}, socket) do
"validate", {:noreply, socket |> assign_changeset(container_params)}
%{"container" => container_params},
%{assigns: %{container: container}} = socket
) do
changeset = container |> Container.update_changeset(container_params)
{:noreply, socket |> assign(:changeset, changeset)}
end end
def handle_event( def handle_event(
@ -36,6 +30,31 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
save_container(socket, action, container_params) save_container(socket, action, container_params)
end end
defp assign_changeset(
%{assigns: %{action: action, container: container, current_user: user}} = socket,
container_params
) do
changeset_action =
case action do
:new -> :insert
:edit -> :update
end
changeset =
case action do
:new -> container |> Container.create_changeset(user, container_params)
:edit -> container |> Container.update_changeset(container_params)
end
changeset =
case changeset |> Changeset.apply_action(changeset_action) do
{:ok, _data} -> changeset
{:error, changeset} -> changeset
end
socket |> assign(:changeset, changeset)
end
defp save_container( defp save_container(
%{assigns: %{container: container, current_user: current_user, return_to: return_to}} = %{assigns: %{container: container, current_user: current_user, return_to: return_to}} =
socket, socket,

View File

@ -25,7 +25,7 @@ msgstr ""
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/container_live/form_component.ex:85
#: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -63,7 +63,7 @@ msgstr "%{name} erfolgreich aktualisiert"
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/container_live/form_component.ex:67
#: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"

View File

@ -13,7 +13,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/container_live/form_component.ex:85
#: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -51,7 +51,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/container_live/form_component.ex:67
#: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"

View File

@ -23,7 +23,7 @@ msgstr ""
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/container_live/form_component.ex:85
#: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -61,7 +61,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/container_live/form_component.ex:67
#: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"

View File

@ -25,7 +25,7 @@ msgstr ""
## effect: edit them in PO (.po) files instead. ## effect: edit them in PO (.po) files instead.
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/container_live/form_component.ex:85
#: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -63,7 +63,7 @@ msgstr "%{name} mis à jour avec succès"
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/container_live/form_component.ex:67
#: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"

View File

@ -12,7 +12,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: lib/cannery_web/live/ammo_type_live/form_component.ex:85
#: lib/cannery_web/live/container_live/form_component.ex:66 #: lib/cannery_web/live/container_live/form_component.ex:85
#: lib/cannery_web/live/invite_live/form_component.ex:59 #: lib/cannery_web/live/invite_live/form_component.ex:59
#: lib/cannery_web/live/tag_live/form_component.ex:101 #: lib/cannery_web/live/tag_live/form_component.ex:101
msgid "%{name} created successfully" msgid "%{name} created successfully"
@ -50,7 +50,7 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
#: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: lib/cannery_web/live/ammo_type_live/form_component.ex:67
#: lib/cannery_web/live/container_live/form_component.ex:48 #: lib/cannery_web/live/container_live/form_component.ex:67
#: lib/cannery_web/live/invite_live/form_component.ex:41 #: lib/cannery_web/live/invite_live/form_component.ex:41
#: lib/cannery_web/live/tag_live/form_component.ex:83 #: lib/cannery_web/live/tag_live/form_component.ex:83
msgid "%{name} updated successfully" msgid "%{name} updated successfully"