forked from shibao/cannery
use container changeset helper
This commit is contained in:
parent
76bbab1de9
commit
36f56528ee
@ -13,19 +13,13 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
%{:container => Container.t(), :current_user => User.t(), optional(any) => any},
|
||||
Socket.t()
|
||||
) :: {:ok, Socket.t()}
|
||||
def update(%{container: container} = assigns, socket) do
|
||||
changeset = container |> Container.update_changeset(%{})
|
||||
{:ok, socket |> assign(assigns) |> assign(:changeset, changeset)}
|
||||
def update(%{container: _container} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns) |> assign_changeset(%{})}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event(
|
||||
"validate",
|
||||
%{"container" => container_params},
|
||||
%{assigns: %{container: container}} = socket
|
||||
) do
|
||||
changeset = container |> Container.update_changeset(container_params)
|
||||
{:noreply, socket |> assign(:changeset, changeset)}
|
||||
def handle_event("validate", %{"container" => container_params}, socket) do
|
||||
{:noreply, socket |> assign_changeset(container_params)}
|
||||
end
|
||||
|
||||
def handle_event(
|
||||
@ -36,6 +30,31 @@ defmodule CanneryWeb.ContainerLive.FormComponent do
|
||||
save_container(socket, action, container_params)
|
||||
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(
|
||||
%{assigns: %{container: container, current_user: current_user, return_to: return_to}} =
|
||||
socket,
|
||||
|
@ -25,7 +25,7 @@ msgstr ""
|
||||
## effect: edit them in PO (.po) files instead.
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:101
|
||||
msgid "%{name} created successfully"
|
||||
@ -63,7 +63,7 @@ msgstr "%{name} erfolgreich aktualisiert"
|
||||
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:83
|
||||
msgid "%{name} updated successfully"
|
||||
|
@ -13,7 +13,7 @@ msgstr ""
|
||||
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:101
|
||||
msgid "%{name} created successfully"
|
||||
@ -51,7 +51,7 @@ msgstr ""
|
||||
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:83
|
||||
msgid "%{name} updated successfully"
|
||||
|
@ -23,7 +23,7 @@ msgstr ""
|
||||
## effect: edit them in PO (.po) files instead.
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:101
|
||||
msgid "%{name} created successfully"
|
||||
@ -61,7 +61,7 @@ msgstr ""
|
||||
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:83
|
||||
msgid "%{name} updated successfully"
|
||||
|
@ -25,7 +25,7 @@ msgstr ""
|
||||
## effect: edit them in PO (.po) files instead.
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:101
|
||||
msgid "%{name} created successfully"
|
||||
@ -63,7 +63,7 @@ msgstr "%{name} mis à jour avec succès"
|
||||
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:83
|
||||
msgid "%{name} updated successfully"
|
||||
|
@ -12,7 +12,7 @@ msgstr ""
|
||||
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:101
|
||||
msgid "%{name} created successfully"
|
||||
@ -50,7 +50,7 @@ msgstr ""
|
||||
|
||||
#, elixir-autogen, elixir-format
|
||||
#: 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/tag_live/form_component.ex:83
|
||||
msgid "%{name} updated successfully"
|
||||
|
Loading…
Reference in New Issue
Block a user