2021-09-02 23:31:14 -04:00
|
|
|
defmodule CanneryWeb.TagLive.Index do
|
2022-01-22 21:40:29 -05:00
|
|
|
@moduledoc """
|
|
|
|
Liveview to show a Cannery.Tags.Tag index
|
|
|
|
"""
|
|
|
|
|
2021-09-02 23:31:14 -04:00
|
|
|
use CanneryWeb, :live_view
|
2022-02-11 22:47:33 -05:00
|
|
|
import CanneryWeb.Components.TagCard
|
2022-02-12 00:36:55 -05:00
|
|
|
alias Cannery.{Tags, Tags.Tag}
|
|
|
|
alias CanneryWeb.Endpoint
|
2021-09-02 23:31:14 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-05-05 23:26:29 -04:00
|
|
|
def mount(_params, _session, socket), do: {:ok, socket |> display_tags()}
|
2021-09-02 23:31:14 -04:00
|
|
|
|
|
|
|
@impl true
|
2022-02-09 23:21:42 -05:00
|
|
|
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
|
2022-02-20 15:01:15 -05:00
|
|
|
{:noreply, apply_action(socket, live_action, params) |> display_tags}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-02-09 23:21:42 -05:00
|
|
|
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
|
2021-09-02 23:31:14 -04:00
|
|
|
socket
|
2022-02-09 00:58:53 -05:00
|
|
|
|> assign(:page_title, gettext("Edit Tag"))
|
2022-02-09 23:21:42 -05:00
|
|
|
|> assign(:tag, Tags.get_tag!(id, current_user))
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :new, _params) do
|
|
|
|
socket
|
2022-02-09 00:58:53 -05:00
|
|
|
|> assign(:page_title, gettext("New Tag"))
|
2022-01-28 21:05:54 -05:00
|
|
|
|> assign(:tag, %Tag{bg_color: Tags.random_bg_color(), text_color: "#ffffff"})
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp apply_action(socket, :index, _params) do
|
2022-02-24 00:52:46 -05:00
|
|
|
socket |> assign(:page_title, gettext("Tags")) |> assign(:tag, nil)
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2022-02-09 23:21:42 -05:00
|
|
|
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
|
|
|
|
%{name: tag_name} = Tags.get_tag!(id, current_user) |> Tags.delete_tag!(current_user)
|
|
|
|
prompt = dgettext("prompts", "%{name} deleted succesfully", name: tag_name)
|
|
|
|
{:noreply, socket |> put_flash(:info, prompt) |> display_tags()}
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
|
2022-02-09 23:21:42 -05:00
|
|
|
defp display_tags(%{assigns: %{current_user: current_user}} = socket) do
|
|
|
|
socket |> assign(tags: Tags.list_tags(current_user))
|
2021-09-02 23:31:14 -04:00
|
|
|
end
|
|
|
|
end
|