From 36f56528ee6d632e7645d255540c245e25cc4275 Mon Sep 17 00:00:00 2001 From: shibao Date: Mon, 4 Jul 2022 20:52:21 -0400 Subject: [PATCH] use container changeset helper --- .../live/container_live/form_component.ex | 39 ++++++++++++++----- priv/gettext/de/LC_MESSAGES/prompts.po | 4 +- priv/gettext/en/LC_MESSAGES/prompts.po | 4 +- priv/gettext/es/LC_MESSAGES/prompts.po | 4 +- priv/gettext/fr/LC_MESSAGES/prompts.po | 4 +- priv/gettext/prompts.pot | 4 +- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/lib/cannery_web/live/container_live/form_component.ex b/lib/cannery_web/live/container_live/form_component.ex index 3ac0ae60..4ea2f7ae 100644 --- a/lib/cannery_web/live/container_live/form_component.ex +++ b/lib/cannery_web/live/container_live/form_component.ex @@ -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, diff --git a/priv/gettext/de/LC_MESSAGES/prompts.po b/priv/gettext/de/LC_MESSAGES/prompts.po index 2ab84dd5..19391a95 100644 --- a/priv/gettext/de/LC_MESSAGES/prompts.po +++ b/priv/gettext/de/LC_MESSAGES/prompts.po @@ -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" diff --git a/priv/gettext/en/LC_MESSAGES/prompts.po b/priv/gettext/en/LC_MESSAGES/prompts.po index 8035b4b0..d756e047 100644 --- a/priv/gettext/en/LC_MESSAGES/prompts.po +++ b/priv/gettext/en/LC_MESSAGES/prompts.po @@ -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" diff --git a/priv/gettext/es/LC_MESSAGES/prompts.po b/priv/gettext/es/LC_MESSAGES/prompts.po index 1499315e..47ea4961 100644 --- a/priv/gettext/es/LC_MESSAGES/prompts.po +++ b/priv/gettext/es/LC_MESSAGES/prompts.po @@ -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" diff --git a/priv/gettext/fr/LC_MESSAGES/prompts.po b/priv/gettext/fr/LC_MESSAGES/prompts.po index 4081a091..01dfa246 100644 --- a/priv/gettext/fr/LC_MESSAGES/prompts.po +++ b/priv/gettext/fr/LC_MESSAGES/prompts.po @@ -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" diff --git a/priv/gettext/prompts.pot b/priv/gettext/prompts.pot index 4d63b19c..116f9d9f 100644 --- a/priv/gettext/prompts.pot +++ b/priv/gettext/prompts.pot @@ -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"