Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tag on task using Enter #389

Merged
merged 9 commits into from
Jul 14, 2023
45 changes: 38 additions & 7 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,7 @@ defmodule AppWeb.AppLive do
tag = Tag.get_tag!(value["tag_id"])
tags = Tag.list_person_tags(person_id)

selected_tags =
if Enum.member?(selected_tags, tag) do
List.delete(selected_tags, tag)
else
[tag | selected_tags]
end
|> Enum.sort_by(& &1.text)
selected_tags = toggle_tag(selected_tags, tag)

{:noreply, assign(socket, tags: tags, selected_tags: selected_tags)}
end
Expand All @@ -122,6 +116,34 @@ defmodule AppWeb.AppLive do
{:noreply, assign(socket, tags: tags)}
end

@impl true
def handle_event(
"add-first-tag",
%{"key" => "Enter"},
socket
) do
case socket.assigns.tags do
[] ->
{:noreply, socket}

_ ->
selected_tag =
socket.assigns.tags
|> List.first()
|> Map.get(:id)
|> Tag.get_tag!()

{:noreply,
assign(socket,
selected_tags: toggle_tag(socket.assigns.selected_tags, selected_tag)
)}
end
end

@impl true
def handle_event("add-first-tag", _params, socket),
do: {:noreply, socket}

@impl true
def handle_event("delete", %{"id" => item_id}, socket) do
Item.delete_item(item_id)
Expand Down Expand Up @@ -384,6 +406,15 @@ defmodule AppWeb.AppLive do
end
end

defp toggle_tag(selected_tags, tag) do
if Enum.member?(selected_tags, tag) do
List.delete(selected_tags, tag)
else
[tag | selected_tags]
end
|> Enum.sort_by(& &1.text)
end

def class_footer_link(filter_name, filter_selected) do
if filter_name == filter_selected do
"px-2 py-2 h-9 mr-1 bg-teal-700 text-white rounded-md"
Expand Down
8 changes: 7 additions & 1 deletion lib/app_web/live/app_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@
name="tag"
class="w-full my-2"
x-on:focus="open = true"
x-on:keydown.enter.prevent="
if (event.key === 'Enter') {
event.target.value = '';
}
"
phx-keyup="filter-tags"
phx-debounce="250"
phx-keyup-debounce="250"
phx-keydown="add-first-tag"
autocomplete="off"
placeholder="tags"
/>
Expand Down
46 changes: 46 additions & 0 deletions test/app_web/live/app_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -806,4 +806,50 @@ defmodule AppWeb.AppLiveTest do
{:ok, view, _html} = live(conn, "/")
assert render_hook(view, "filter-tags", %{"key" => "t", "value" => "t"})
end

test "select tag when enter pressed", %{conn: conn} do
{:ok, _tag1} =
Tag.create_tag(%{person_id: 0, text: "tag1", color: "#FCA5A5"})

{:ok, _tag2} =
Tag.create_tag(%{
person_id: 0,
text: "enter_tag_selected",
color: "#FCA5A5"
})

{:ok, view, _html} = live(conn, "/")

assert render_keydown(view, "add-first-tag", %{"key" => "Enter"}) =~
"enter_tag_selected"

assert render_submit(view, :create, %{text: "tag enter pressed"})

assert render(view) =~ "tag enter pressed"
assert render(view) =~ "enter_tag_selected"
end
panoramix360 marked this conversation as resolved.
Show resolved Hide resolved

test "dont select tag if there arent tags created", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")

assert render_keydown(view, "add-first-tag", %{"key" => "Enter"})

assert render_submit(view, :create, %{text: "tag enter pressed"})

last_item_inserted = Item.list_person_items(0) |> List.last()

assert last_item_inserted.tags == []
end

test "dont select tag if other keydown is pressed", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")

assert render_keydown(view, "add-first-tag", %{"key" => "Esc"})

assert render_submit(view, :create, %{text: "tag enter pressed"})

last_item_inserted = Item.list_person_items(0) |> List.last()

assert last_item_inserted.tags == []
end
end