use live navigation to update state

This commit is contained in:
shibao 2023-03-16 17:49:45 -04:00
parent 3acecb9a93
commit ce07cc2569
15 changed files with 57 additions and 36 deletions

View File

@ -4,25 +4,40 @@ defmodule CanneryWeb.ContainerLive.EditTagsComponent do
""" """
use CanneryWeb, :live_component use CanneryWeb, :live_component
alias Cannery.{Accounts.User, Containers, Containers.Container, Repo, Tags, Tags.Tag} alias Cannery.{Accounts.User, Containers, Containers.Container, Tags, Tags.Tag}
alias Phoenix.LiveView.Socket alias Phoenix.LiveView.Socket
@impl true @impl true
@spec update( @spec update(
%{:container => Container.t(), :current_user => User.t(), optional(any) => any}, %{
:container => Container.t(),
:current_path => String.t(),
:current_user => User.t(),
optional(any) => any
},
Socket.t() Socket.t()
) :: {:ok, Socket.t()} ) :: {:ok, Socket.t()}
def update(%{container: container, current_user: current_user} = assigns, socket) do def update(
%{container: _container, current_path: _current_path, current_user: current_user} =
assigns,
socket
) do
tags = Tags.list_tags(current_user) tags = Tags.list_tags(current_user)
container = container |> Repo.preload(:tags) {:ok, socket |> assign(assigns) |> assign(:tags, tags)}
{:ok, socket |> assign(assigns) |> assign(tags: tags, container: container)}
end end
@impl true @impl true
def handle_event( def handle_event(
"save", "save",
%{"tag" => %{"tag_id" => tag_id}}, %{"tag" => %{"tag_id" => tag_id}},
%{assigns: %{tags: tags, container: container, current_user: current_user}} = socket %{
assigns: %{
tags: tags,
container: container,
current_user: current_user,
current_path: current_path
}
} = socket
) do ) do
socket = socket =
case tags |> Enum.find(fn %{id: id} -> tag_id == id end) do case tags |> Enum.find(fn %{id: id} -> tag_id == id end) do
@ -32,19 +47,24 @@ defmodule CanneryWeb.ContainerLive.EditTagsComponent do
%{name: tag_name} = tag -> %{name: tag_name} = tag ->
_container_tag = Containers.add_tag!(container, tag, current_user) _container_tag = Containers.add_tag!(container, tag, current_user)
container = container |> Repo.preload(:tags, force: true)
prompt = dgettext("prompts", "%{name} added successfully", name: tag_name) prompt = dgettext("prompts", "%{name} added successfully", name: tag_name)
socket |> put_flash(:info, prompt) |> assign(container: container) socket |> put_flash(:info, prompt) |> push_patch(to: current_path)
end end
{:noreply, socket} {:noreply, socket}
end end
@impl true
def handle_event( def handle_event(
"delete", "delete",
%{"tag-id" => tag_id}, %{"tag-id" => tag_id},
%{assigns: %{tags: tags, container: container, current_user: current_user}} = socket %{
assigns: %{
tags: tags,
container: container,
current_user: current_user,
current_path: current_path
}
} = socket
) do ) do
socket = socket =
case tags |> Enum.find(fn %{id: id} -> tag_id == id end) do case tags |> Enum.find(fn %{id: id} -> tag_id == id end) do
@ -54,9 +74,8 @@ defmodule CanneryWeb.ContainerLive.EditTagsComponent do
%{name: tag_name} = tag -> %{name: tag_name} = tag ->
_container_tag = Containers.remove_tag!(container, tag, current_user) _container_tag = Containers.remove_tag!(container, tag, current_user)
container = container |> Repo.preload(:tags, force: true)
prompt = dgettext("prompts", "%{name} removed successfully", name: tag_name) prompt = dgettext("prompts", "%{name} removed successfully", name: tag_name)
socket |> put_flash(:info, prompt) |> assign(container: container) socket |> put_flash(:info, prompt) |> push_patch(to: current_path)
end end
{:noreply, socket} {:noreply, socket}

View File

@ -167,6 +167,7 @@
title={@page_title} title={@page_title}
action={@live_action} action={@live_action}
container={@container} container={@container}
current_path={Routes.container_index_path(Endpoint, :edit_tags, @container)}
current_user={@current_user} current_user={@current_user}
/> />
</.modal> </.modal>

View File

@ -161,6 +161,7 @@
action={@live_action} action={@live_action}
container={@container} container={@container}
return_to={Routes.container_show_path(Endpoint, :show, @container)} return_to={Routes.container_show_path(Endpoint, :show, @container)}
current_path={Routes.container_show_path(Endpoint, :edit_tags, @container)}
current_user={@current_user} current_user={@current_user}
/> />
</.modal> </.modal>

View File

@ -147,7 +147,7 @@ msgstr "Muss ein @ Zeichen und keine Leerzeichen haben"
msgid "Tag not found" msgid "Tag not found"
msgstr "Tag nicht gefunden" msgstr "Tag nicht gefunden"
#: lib/cannery_web/live/container_live/edit_tags_component.ex:30 #: lib/cannery_web/live/container_live/edit_tags_component.ex:45
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be added" msgid "Tag could not be added"
msgstr "Tag konnte nicht hinzugefügt werden" msgstr "Tag konnte nicht hinzugefügt werden"
@ -165,7 +165,7 @@ msgstr ""
"Sie müssen ihr Nutzerkonto bestätigen und einloggen, um diese Seite " "Sie müssen ihr Nutzerkonto bestätigen und einloggen, um diese Seite "
"anzuzeigen." "anzuzeigen."
#: lib/cannery_web/live/container_live/edit_tags_component.ex:52 #: lib/cannery_web/live/container_live/edit_tags_component.ex:72
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be removed" msgid "Tag could not be removed"
msgstr "Tag konnte nicht gelöscht werden" msgstr "Tag konnte nicht gelöscht werden"

View File

@ -151,7 +151,7 @@ msgstr ""
"Sind Sie sicher, dass sie %{tag_name} Tag von %{container_name} entfernen " "Sind Sie sicher, dass sie %{tag_name} Tag von %{container_name} entfernen "
"wollen?" "wollen?"
#: lib/cannery_web/live/container_live/edit_tags_component.ex:36 #: lib/cannery_web/live/container_live/edit_tags_component.ex:50
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} added successfully" msgid "%{name} added successfully"
msgstr "%{name} erfolgreich hinzugefügt" msgstr "%{name} erfolgreich hinzugefügt"
@ -208,7 +208,7 @@ msgstr "Munition erfolgreich zu %{name} verschoben"
msgid "Copied to clipboard" msgid "Copied to clipboard"
msgstr "Der Zwischenablage hinzugefügt" msgstr "Der Zwischenablage hinzugefügt"
#: lib/cannery_web/live/container_live/edit_tags_component.ex:58 #: lib/cannery_web/live/container_live/edit_tags_component.ex:77
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} removed successfully" msgid "%{name} removed successfully"
msgstr "%{name} erfolgreich entfernt" msgstr "%{name} erfolgreich entfernt"

View File

@ -134,7 +134,7 @@ msgstr ""
msgid "Tag not found" msgid "Tag not found"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:30 #: lib/cannery_web/live/container_live/edit_tags_component.ex:45
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be added" msgid "Tag could not be added"
msgstr "" msgstr ""
@ -150,7 +150,7 @@ msgstr ""
msgid "You must confirm your account and log in to access this page." msgid "You must confirm your account and log in to access this page."
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:52 #: lib/cannery_web/live/container_live/edit_tags_component.ex:72
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Tag could not be removed" msgid "Tag could not be removed"
msgstr "" msgstr ""

View File

@ -130,7 +130,7 @@ msgstr ""
msgid "Are you sure you want to remove the %{tag_name} tag from %{container_name}?" msgid "Are you sure you want to remove the %{tag_name} tag from %{container_name}?"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:36 #: lib/cannery_web/live/container_live/edit_tags_component.ex:50
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} added successfully" msgid "%{name} added successfully"
msgstr "" msgstr ""
@ -187,7 +187,7 @@ msgstr ""
msgid "Copied to clipboard" msgid "Copied to clipboard"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:58 #: lib/cannery_web/live/container_live/edit_tags_component.ex:77
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "%{name} removed successfully" msgid "%{name} removed successfully"
msgstr "" msgstr ""

View File

@ -133,7 +133,7 @@ msgstr ""
msgid "Tag not found" msgid "Tag not found"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:30 #: lib/cannery_web/live/container_live/edit_tags_component.ex:45
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be added" msgid "Tag could not be added"
msgstr "" msgstr ""
@ -149,7 +149,7 @@ msgstr ""
msgid "You must confirm your account and log in to access this page." msgid "You must confirm your account and log in to access this page."
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:52 #: lib/cannery_web/live/container_live/edit_tags_component.ex:72
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be removed" msgid "Tag could not be removed"
msgstr "" msgstr ""

View File

@ -149,7 +149,7 @@ msgstr "debe tener el signo @ y no contener espacios"
msgid "Tag not found" msgid "Tag not found"
msgstr "Etiqueta no encontrada" msgstr "Etiqueta no encontrada"
#: lib/cannery_web/live/container_live/edit_tags_component.ex:30 #: lib/cannery_web/live/container_live/edit_tags_component.ex:45
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be added" msgid "Tag could not be added"
msgstr "No se ha podido añadir la etiqueta" msgstr "No se ha podido añadir la etiqueta"
@ -165,7 +165,7 @@ msgstr "El recuento debe dar al menos 1"
msgid "You must confirm your account and log in to access this page." msgid "You must confirm your account and log in to access this page."
msgstr "Debe confirmar su cuenta e iniciar sesión para acceder a esta página." msgstr "Debe confirmar su cuenta e iniciar sesión para acceder a esta página."
#: lib/cannery_web/live/container_live/edit_tags_component.ex:52 #: lib/cannery_web/live/container_live/edit_tags_component.ex:72
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be removed" msgid "Tag could not be removed"
msgstr "La etiqueta no pudo ser eliminada" msgstr "La etiqueta no pudo ser eliminada"

View File

@ -150,7 +150,7 @@ msgid "Are you sure you want to remove the %{tag_name} tag from %{container_name
msgstr "" msgstr ""
"Está seguro que desea remover la etiqueta %{tag_name} de %{container_name}?" "Está seguro que desea remover la etiqueta %{tag_name} de %{container_name}?"
#: lib/cannery_web/live/container_live/edit_tags_component.ex:36 #: lib/cannery_web/live/container_live/edit_tags_component.ex:50
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} added successfully" msgid "%{name} added successfully"
msgstr "%{name} añadido exitosamente" msgstr "%{name} añadido exitosamente"
@ -207,7 +207,7 @@ msgstr "Munición movida a %{name} exitosamente"
msgid "Copied to clipboard" msgid "Copied to clipboard"
msgstr "Copiado al portapapeles" msgstr "Copiado al portapapeles"
#: lib/cannery_web/live/container_live/edit_tags_component.ex:58 #: lib/cannery_web/live/container_live/edit_tags_component.ex:77
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} removed successfully" msgid "%{name} removed successfully"
msgstr "%{name} eliminado exitosamente" msgstr "%{name} eliminado exitosamente"

View File

@ -148,7 +148,7 @@ msgstr "doit contenir le symbole @ et aucune espace"
msgid "Tag not found" msgid "Tag not found"
msgstr "Tag pas trouvé" msgstr "Tag pas trouvé"
#: lib/cannery_web/live/container_live/edit_tags_component.ex:30 #: lib/cannery_web/live/container_live/edit_tags_component.ex:45
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be added" msgid "Tag could not be added"
msgstr "Le tag na pas pu être ajouté" msgstr "Le tag na pas pu être ajouté"
@ -166,7 +166,7 @@ msgstr ""
"Vous devez dabord confirmer votre compte et vous connecter pour accéder à " "Vous devez dabord confirmer votre compte et vous connecter pour accéder à "
"cette page." "cette page."
#: lib/cannery_web/live/container_live/edit_tags_component.ex:52 #: lib/cannery_web/live/container_live/edit_tags_component.ex:72
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be removed" msgid "Tag could not be removed"
msgstr "Le tag na pas pu être retiré" msgstr "Le tag na pas pu être retiré"

View File

@ -152,7 +152,7 @@ msgstr ""
"Êtes-vous certain·e de vouloir retirer le tag %{tag_name} de " "Êtes-vous certain·e de vouloir retirer le tag %{tag_name} de "
"%{container_name}?" "%{container_name}?"
#: lib/cannery_web/live/container_live/edit_tags_component.ex:36 #: lib/cannery_web/live/container_live/edit_tags_component.ex:50
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} added successfully" msgid "%{name} added successfully"
msgstr "%{name} a été ajouté avec succès" msgstr "%{name} a été ajouté avec succès"
@ -209,7 +209,7 @@ msgstr "Munition déplacée à %{name} avec succès"
msgid "Copied to clipboard" msgid "Copied to clipboard"
msgstr "Copié dans le presse-papier" msgstr "Copié dans le presse-papier"
#: lib/cannery_web/live/container_live/edit_tags_component.ex:58 #: lib/cannery_web/live/container_live/edit_tags_component.ex:77
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} removed successfully" msgid "%{name} removed successfully"
msgstr "%{name} retiré avec succès" msgstr "%{name} retiré avec succès"

View File

@ -149,7 +149,7 @@ msgstr ""
msgid "Tag not found" msgid "Tag not found"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:30 #: lib/cannery_web/live/container_live/edit_tags_component.ex:45
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be added" msgid "Tag could not be added"
msgstr "" msgstr ""
@ -165,7 +165,7 @@ msgstr ""
msgid "You must confirm your account and log in to access this page." msgid "You must confirm your account and log in to access this page."
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:52 #: lib/cannery_web/live/container_live/edit_tags_component.ex:72
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Tag could not be removed" msgid "Tag could not be removed"
msgstr "" msgstr ""

View File

@ -141,7 +141,7 @@ msgstr ""
msgid "Are you sure you want to remove the %{tag_name} tag from %{container_name}?" msgid "Are you sure you want to remove the %{tag_name} tag from %{container_name}?"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:36 #: lib/cannery_web/live/container_live/edit_tags_component.ex:50
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} added successfully" msgid "%{name} added successfully"
msgstr "" msgstr ""
@ -198,7 +198,7 @@ msgstr ""
msgid "Copied to clipboard" msgid "Copied to clipboard"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:58 #: lib/cannery_web/live/container_live/edit_tags_component.ex:77
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} removed successfully" msgid "%{name} removed successfully"
msgstr "" msgstr ""

View File

@ -130,7 +130,7 @@ msgstr ""
msgid "Are you sure you want to remove the %{tag_name} tag from %{container_name}?" msgid "Are you sure you want to remove the %{tag_name} tag from %{container_name}?"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:36 #: lib/cannery_web/live/container_live/edit_tags_component.ex:50
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} added successfully" msgid "%{name} added successfully"
msgstr "" msgstr ""
@ -187,7 +187,7 @@ msgstr ""
msgid "Copied to clipboard" msgid "Copied to clipboard"
msgstr "" msgstr ""
#: lib/cannery_web/live/container_live/edit_tags_component.ex:58 #: lib/cannery_web/live/container_live/edit_tags_component.ex:77
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{name} removed successfully" msgid "%{name} removed successfully"
msgstr "" msgstr ""