diff --git a/lib/cannery/tags.ex b/lib/cannery/tags.ex index 8b3e9605..91069e30 100644 --- a/lib/cannery/tags.ex +++ b/lib/cannery/tags.ex @@ -74,8 +74,8 @@ defmodule Cannery.Tags do """ @spec create_tag(attrs :: map(), User.t()) :: {:ok, Tag.t()} | {:error, Changeset.t(Tag.new_tag())} - def create_tag(attrs, %User{id: user_id}), - do: %Tag{} |> Tag.create_changeset(attrs |> Map.put("user_id", user_id)) |> Repo.insert() + def create_tag(attrs, %User{} = user), + do: %Tag{} |> Tag.create_changeset(user, attrs) |> Repo.insert() @doc """ Updates a tag. @@ -121,20 +121,6 @@ defmodule Cannery.Tags do @spec delete_tag!(Tag.t(), User.t()) :: Tag.t() def delete_tag!(%Tag{user_id: user_id} = tag, %User{id: user_id}), do: tag |> Repo.delete!() - @doc """ - Returns an `%Changeset{}` for tracking tag changes. - - ## Examples - - iex> change_tag(tag) - %Changeset{data: %Tag{}} - - """ - @spec change_tag(Tag.t() | Tag.new_tag()) :: Changeset.t(Tag.t() | Tag.new_tag()) - @spec change_tag(Tag.t() | Tag.new_tag(), attrs :: map()) :: - Changeset.t(Tag.t() | Tag.new_tag()) - def change_tag(tag, attrs \\ %{}), do: Tag.update_changeset(tag, attrs) - @doc """ Get a random tag bg_color in `#ffffff` hex format diff --git a/lib/cannery/tags/tag.ex b/lib/cannery/tags/tag.ex index addeb061..a0dd0d77 100644 --- a/lib/cannery/tags/tag.ex +++ b/lib/cannery/tags/tag.ex @@ -35,10 +35,11 @@ defmodule Cannery.Tags.Tag do @type id() :: UUID.t() @doc false - @spec create_changeset(new_tag(), attrs :: map()) :: Changeset.t(new_tag()) - def create_changeset(tag, attrs) do + @spec create_changeset(new_tag(), User.t(), attrs :: map()) :: Changeset.t(new_tag()) + def create_changeset(tag, %User{id: user_id}, attrs) do tag - |> cast(attrs, [:name, :bg_color, :text_color, :user_id]) + |> change(user_id: user_id) + |> cast(attrs, [:name, :bg_color, :text_color]) |> validate_required([:name, :bg_color, :text_color, :user_id]) end diff --git a/lib/cannery_web/live/tag_live/form_component.ex b/lib/cannery_web/live/tag_live/form_component.ex index 6f51ceaa..6d1b8b81 100644 --- a/lib/cannery_web/live/tag_live/form_component.ex +++ b/lib/cannery_web/live/tag_live/form_component.ex @@ -12,19 +12,44 @@ defmodule CanneryWeb.TagLive.FormComponent do @impl true @spec update(%{:tag => Tag.t(), :current_user => User.t(), optional(any) => any}, Socket.t()) :: {:ok, Socket.t()} - def update(%{tag: tag} = assigns, socket) do - {:ok, socket |> assign(assigns) |> assign(:changeset, Tags.change_tag(tag))} + def update(%{tag: _tag} = assigns, socket) do + {:ok, socket |> assign(assigns) |> assign_changeset(%{})} end @impl true - def handle_event("validate", %{"tag" => tag_params}, %{assigns: %{tag: tag}} = socket) do - {:noreply, socket |> assign(:changeset, tag |> Tags.change_tag(tag_params))} + def handle_event("validate", %{"tag" => tag_params}, socket) do + {:noreply, socket |> assign_changeset(tag_params)} end def handle_event("save", %{"tag" => tag_params}, %{assigns: %{action: action}} = socket) do save_tag(socket, action, tag_params) end + defp assign_changeset( + %{assigns: %{action: action, current_user: user, tag: tag}} = socket, + tag_params + ) do + changeset_action = + case action do + :new -> :insert + :edit -> :update + end + + changeset = + case action do + :new -> tag |> Tag.create_changeset(user, tag_params) + :edit -> tag |> Tag.update_changeset(tag_params) + end + + changeset = + case changeset |> Changeset.apply_action(changeset_action) do + {:ok, _data} -> changeset + {:error, changeset} -> changeset + end + + socket |> assign(:changeset, changeset) + end + @impl true def render(assigns) do ~H""" diff --git a/priv/gettext/actions.pot b/priv/gettext/actions.pot index 0d507688..b94043af 100644 --- a/priv/gettext/actions.pot +++ b/priv/gettext/actions.pot @@ -126,7 +126,7 @@ msgstr "" #: lib/cannery_web/live/container_live/form_component.html.heex:50 #: lib/cannery_web/live/invite_live/form_component.html.heex:28 #: lib/cannery_web/live/range_live/form_component.html.heex:40 -#: lib/cannery_web/live/tag_live/form_component.ex:66 +#: lib/cannery_web/live/tag_live/form_component.ex:91 msgid "Save" msgstr "" diff --git a/priv/gettext/de/LC_MESSAGES/actions.po b/priv/gettext/de/LC_MESSAGES/actions.po index 78497ea3..7a68b808 100644 --- a/priv/gettext/de/LC_MESSAGES/actions.po +++ b/priv/gettext/de/LC_MESSAGES/actions.po @@ -139,7 +139,7 @@ msgstr "Passwort zurücksetzen" #: lib/cannery_web/live/container_live/form_component.html.heex:50 #: lib/cannery_web/live/invite_live/form_component.html.heex:28 #: lib/cannery_web/live/range_live/form_component.html.heex:40 -#: lib/cannery_web/live/tag_live/form_component.ex:66 +#: lib/cannery_web/live/tag_live/form_component.ex:91 msgid "Save" msgstr "Speichern" diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index 2fd51e8e..a407842c 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -65,7 +65,7 @@ msgid "Average Price paid" msgstr "Durchschnittlicher Kaufpreis" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:54 +#: lib/cannery_web/live/tag_live/form_component.ex:79 msgid "Background color" msgstr "Hintergrundfarbe" @@ -287,7 +287,7 @@ msgstr "Meine coole Munitionskiste" #: lib/cannery_web/live/ammo_type_live/index.ex:51 #: lib/cannery_web/live/container_live/form_component.html.heex:20 #: lib/cannery_web/live/invite_live/form_component.html.heex:20 -#: lib/cannery_web/live/tag_live/form_component.ex:50 +#: lib/cannery_web/live/tag_live/form_component.ex:75 msgid "Name" msgstr "Name" @@ -448,7 +448,7 @@ msgid "Tags can be added to your containers to help you organize" msgstr "Tags können zur besseren Ordnung einem Behälter hinzugefügt werden" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:60 +#: lib/cannery_web/live/tag_live/form_component.ex:85 msgid "Text color" msgstr "Textfarbe" diff --git a/priv/gettext/de/LC_MESSAGES/prompts.po b/priv/gettext/de/LC_MESSAGES/prompts.po index 19391a95..cb36e744 100644 --- a/priv/gettext/de/LC_MESSAGES/prompts.po +++ b/priv/gettext/de/LC_MESSAGES/prompts.po @@ -27,7 +27,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:126 msgid "%{name} created successfully" msgstr "%{name} erfolgreich erstellt" @@ -65,7 +65,7 @@ msgstr "%{name} erfolgreich aktualisiert" #: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:108 msgid "%{name} updated successfully" msgstr "%{name} erfolgreich aktualisiert" @@ -173,7 +173,7 @@ msgstr "Registrieren Sie sich, um %{name} zu bearbeiten" #: lib/cannery_web/live/container_live/form_component.html.heex:52 #: lib/cannery_web/live/invite_live/form_component.html.heex:30 #: lib/cannery_web/live/range_live/form_component.html.heex:42 -#: lib/cannery_web/live/tag_live/form_component.ex:68 +#: lib/cannery_web/live/tag_live/form_component.ex:93 msgid "Saving..." msgstr "Speichere..." diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index ef6ed665..44f92c18 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -50,7 +50,7 @@ msgid "Average Price paid" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:54 +#: lib/cannery_web/live/tag_live/form_component.ex:79 msgid "Background color" msgstr "" @@ -272,7 +272,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/index.ex:51 #: lib/cannery_web/live/container_live/form_component.html.heex:20 #: lib/cannery_web/live/invite_live/form_component.html.heex:20 -#: lib/cannery_web/live/tag_live/form_component.ex:50 +#: lib/cannery_web/live/tag_live/form_component.ex:75 msgid "Name" msgstr "" @@ -431,7 +431,7 @@ msgid "Tags can be added to your containers to help you organize" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:60 +#: lib/cannery_web/live/tag_live/form_component.ex:85 msgid "Text color" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/actions.po b/priv/gettext/en/LC_MESSAGES/actions.po index 7802852e..d2919fef 100644 --- a/priv/gettext/en/LC_MESSAGES/actions.po +++ b/priv/gettext/en/LC_MESSAGES/actions.po @@ -127,7 +127,7 @@ msgstr "" #: lib/cannery_web/live/container_live/form_component.html.heex:50 #: lib/cannery_web/live/invite_live/form_component.html.heex:28 #: lib/cannery_web/live/range_live/form_component.html.heex:40 -#: lib/cannery_web/live/tag_live/form_component.ex:66 +#: lib/cannery_web/live/tag_live/form_component.ex:91 msgid "Save" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index a53724ca..ea91d1a2 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -51,7 +51,7 @@ msgid "Average Price paid" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:54 +#: lib/cannery_web/live/tag_live/form_component.ex:79 msgid "Background color" msgstr "" @@ -273,7 +273,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/index.ex:51 #: lib/cannery_web/live/container_live/form_component.html.heex:20 #: lib/cannery_web/live/invite_live/form_component.html.heex:20 -#: lib/cannery_web/live/tag_live/form_component.ex:50 +#: lib/cannery_web/live/tag_live/form_component.ex:75 msgid "Name" msgstr "" @@ -432,7 +432,7 @@ msgid "Tags can be added to your containers to help you organize" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:60 +#: lib/cannery_web/live/tag_live/form_component.ex:85 msgid "Text color" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/prompts.po b/priv/gettext/en/LC_MESSAGES/prompts.po index d756e047..7138934c 100644 --- a/priv/gettext/en/LC_MESSAGES/prompts.po +++ b/priv/gettext/en/LC_MESSAGES/prompts.po @@ -15,7 +15,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:126 msgid "%{name} created successfully" msgstr "" @@ -53,7 +53,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:108 msgid "%{name} updated successfully" msgstr "" @@ -155,7 +155,7 @@ msgstr "" #: lib/cannery_web/live/container_live/form_component.html.heex:52 #: lib/cannery_web/live/invite_live/form_component.html.heex:30 #: lib/cannery_web/live/range_live/form_component.html.heex:42 -#: lib/cannery_web/live/tag_live/form_component.ex:68 +#: lib/cannery_web/live/tag_live/form_component.ex:93 msgid "Saving..." msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/actions.po b/priv/gettext/es/LC_MESSAGES/actions.po index 70292ea0..4b6ce50d 100644 --- a/priv/gettext/es/LC_MESSAGES/actions.po +++ b/priv/gettext/es/LC_MESSAGES/actions.po @@ -137,7 +137,7 @@ msgstr "" #: lib/cannery_web/live/container_live/form_component.html.heex:50 #: lib/cannery_web/live/invite_live/form_component.html.heex:28 #: lib/cannery_web/live/range_live/form_component.html.heex:40 -#: lib/cannery_web/live/tag_live/form_component.ex:66 +#: lib/cannery_web/live/tag_live/form_component.ex:91 msgid "Save" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/default.po b/priv/gettext/es/LC_MESSAGES/default.po index 1bd0ca99..f67c70f4 100644 --- a/priv/gettext/es/LC_MESSAGES/default.po +++ b/priv/gettext/es/LC_MESSAGES/default.po @@ -61,7 +61,7 @@ msgid "Average Price paid" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:54 +#: lib/cannery_web/live/tag_live/form_component.ex:79 msgid "Background color" msgstr "" @@ -283,7 +283,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/index.ex:51 #: lib/cannery_web/live/container_live/form_component.html.heex:20 #: lib/cannery_web/live/invite_live/form_component.html.heex:20 -#: lib/cannery_web/live/tag_live/form_component.ex:50 +#: lib/cannery_web/live/tag_live/form_component.ex:75 msgid "Name" msgstr "" @@ -442,7 +442,7 @@ msgid "Tags can be added to your containers to help you organize" msgstr "" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:60 +#: lib/cannery_web/live/tag_live/form_component.ex:85 msgid "Text color" msgstr "" diff --git a/priv/gettext/es/LC_MESSAGES/prompts.po b/priv/gettext/es/LC_MESSAGES/prompts.po index 47ea4961..08b04c62 100644 --- a/priv/gettext/es/LC_MESSAGES/prompts.po +++ b/priv/gettext/es/LC_MESSAGES/prompts.po @@ -25,7 +25,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:126 msgid "%{name} created successfully" msgstr "" @@ -63,7 +63,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:108 msgid "%{name} updated successfully" msgstr "" @@ -165,7 +165,7 @@ msgstr "" #: lib/cannery_web/live/container_live/form_component.html.heex:52 #: lib/cannery_web/live/invite_live/form_component.html.heex:30 #: lib/cannery_web/live/range_live/form_component.html.heex:42 -#: lib/cannery_web/live/tag_live/form_component.ex:68 +#: lib/cannery_web/live/tag_live/form_component.ex:93 msgid "Saving..." msgstr "" diff --git a/priv/gettext/fr/LC_MESSAGES/actions.po b/priv/gettext/fr/LC_MESSAGES/actions.po index 9a686efb..4bb3a899 100644 --- a/priv/gettext/fr/LC_MESSAGES/actions.po +++ b/priv/gettext/fr/LC_MESSAGES/actions.po @@ -139,7 +139,7 @@ msgstr "Réinitialisé le mot de passe" #: lib/cannery_web/live/container_live/form_component.html.heex:50 #: lib/cannery_web/live/invite_live/form_component.html.heex:28 #: lib/cannery_web/live/range_live/form_component.html.heex:40 -#: lib/cannery_web/live/tag_live/form_component.ex:66 +#: lib/cannery_web/live/tag_live/form_component.ex:91 msgid "Save" msgstr "Sauvegarder" diff --git a/priv/gettext/fr/LC_MESSAGES/default.po b/priv/gettext/fr/LC_MESSAGES/default.po index f8dad2a3..c5094227 100644 --- a/priv/gettext/fr/LC_MESSAGES/default.po +++ b/priv/gettext/fr/LC_MESSAGES/default.po @@ -65,7 +65,7 @@ msgid "Average Price paid" msgstr "Prix acheté moyen" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:54 +#: lib/cannery_web/live/tag_live/form_component.ex:79 msgid "Background color" msgstr "Couleur de fond" @@ -287,7 +287,7 @@ msgstr "Ma superbe boite de munition" #: lib/cannery_web/live/ammo_type_live/index.ex:51 #: lib/cannery_web/live/container_live/form_component.html.heex:20 #: lib/cannery_web/live/invite_live/form_component.html.heex:20 -#: lib/cannery_web/live/tag_live/form_component.ex:50 +#: lib/cannery_web/live/tag_live/form_component.ex:75 msgid "Name" msgstr "Nom" @@ -450,7 +450,7 @@ msgstr "" "organiser" #, elixir-autogen, elixir-format -#: lib/cannery_web/live/tag_live/form_component.ex:60 +#: lib/cannery_web/live/tag_live/form_component.ex:85 msgid "Text color" msgstr "Couleur du texte" diff --git a/priv/gettext/fr/LC_MESSAGES/prompts.po b/priv/gettext/fr/LC_MESSAGES/prompts.po index 01dfa246..a6b1d7a9 100644 --- a/priv/gettext/fr/LC_MESSAGES/prompts.po +++ b/priv/gettext/fr/LC_MESSAGES/prompts.po @@ -27,7 +27,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:126 msgid "%{name} created successfully" msgstr "%{name} créé· avec succès" @@ -65,7 +65,7 @@ msgstr "%{name} mis à jour avec succès" #: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:108 msgid "%{name} updated successfully" msgstr "%{name} mis à jour avec succès" @@ -174,7 +174,7 @@ msgstr "S’enregistrer pour mettre en place %{name}" #: lib/cannery_web/live/container_live/form_component.html.heex:52 #: lib/cannery_web/live/invite_live/form_component.html.heex:30 #: lib/cannery_web/live/range_live/form_component.html.heex:42 -#: lib/cannery_web/live/tag_live/form_component.ex:68 +#: lib/cannery_web/live/tag_live/form_component.ex:93 msgid "Saving..." msgstr "Sauvegarde en cours…" diff --git a/priv/gettext/prompts.pot b/priv/gettext/prompts.pot index 116f9d9f..fe6e9e93 100644 --- a/priv/gettext/prompts.pot +++ b/priv/gettext/prompts.pot @@ -14,7 +14,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/form_component.ex:85 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:126 msgid "%{name} created successfully" msgstr "" @@ -52,7 +52,7 @@ msgstr "" #: lib/cannery_web/live/ammo_type_live/form_component.ex:67 #: 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 +#: lib/cannery_web/live/tag_live/form_component.ex:108 msgid "%{name} updated successfully" msgstr "" @@ -154,7 +154,7 @@ msgstr "" #: lib/cannery_web/live/container_live/form_component.html.heex:52 #: lib/cannery_web/live/invite_live/form_component.html.heex:30 #: lib/cannery_web/live/range_live/form_component.html.heex:42 -#: lib/cannery_web/live/tag_live/form_component.ex:68 +#: lib/cannery_web/live/tag_live/form_component.ex:93 msgid "Saving..." msgstr "" diff --git a/test/cannery/tags_test.exs b/test/cannery/tags_test.exs index 73306b3f..748f476e 100644 --- a/test/cannery/tags_test.exs +++ b/test/cannery/tags_test.exs @@ -68,9 +68,5 @@ defmodule Cannery.TagsTest do assert {:ok, %Tag{}} = Tags.delete_tag(tag, current_user) assert_raise Ecto.NoResultsError, fn -> Tags.get_tag!(tag.id, current_user) end end - - test "change_tag/1 returns a tag changeset", %{tag: tag} do - assert %Changeset{} = Tags.change_tag(tag) - end end end