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 "create tag" in select input #222

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ defmodule AppWeb.AppLive do
filter_tag: nil,
tags: tags,
selected_tags: selected_tags,
text_value: ""
text_value: "",
search_tag: ""
)}
end

Expand Down Expand Up @@ -75,13 +76,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(tag, selected_tags)

{:noreply, assign(socket, tags: tags, selected_tags: selected_tags)}
end
Expand All @@ -96,7 +91,26 @@ defmodule AppWeb.AppLive do
String.contains?(String.downcase(t.text), String.downcase(value))
end)

{:noreply, assign(socket, tags: tags)}
{:noreply, assign(socket, tags: tags, search_tag: value)}
end

@impl true
def handle_event("create-tag", %{"tag" => tag}, socket) do
person_id = get_person_id(socket.assigns)

{:ok, tag} =
Tag.create_tag(%{
person_id: person_id,
text: tag,
color: App.Color.random()
})

selected_tags = socket.assigns.selected_tags
selected_tags = toggle_tag(tag, selected_tags)
tags = Tag.list_person_tags(person_id)

{:noreply,
assign(socket, tags: tags, selected_tags: selected_tags, search_tag: "")}
end

@impl true
Expand Down Expand Up @@ -364,4 +378,13 @@ defmodule AppWeb.AppLive do

"""
def tags_to_string(tags), do: Enum.map_join(tags, ", ", & &1.text)

defp toggle_tag(tag, selected_tags) do
if Enum.member?(selected_tags, tag) do
List.delete(selected_tags, tag)
else
[tag | selected_tags]
end
|> Enum.sort_by(& &1.text)
end
end
11 changes: 11 additions & 0 deletions lib/app_web/live/app_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@
</div>
</li>
<% end %>
<%= if Enum.empty?(@tags) and String.length(String.trim(@search_tag)) != 0 do %>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only show the "create" action when the search for a tag returns 0 results

<li
class="border-b p-2 cursor-pointer hover:bg-slate-200"
phx-click="create-tag"
phx-value-tag={@search_tag}
>
<span class="block w-full text-center overflow-hidden text-ellipsis whitespace-nowrap">
<%= "Create #{@search_tag}" %>
</span>
</li>
<% end %>
<li class="border-b p-2 cursor-pointer hover:bg-slate-200">
<%= link("edit tags", to: "/tags", class: "block w-full text-center") %>
</li>
Expand Down
5 changes: 5 additions & 0 deletions test/app_web/live/app_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ defmodule AppWeb.AppLiveTest do
assert render_hook(view, "filter-tags", %{"key" => "t", "value" => "t"})
end

test "create tag from select input", %{conn: conn} do
{:ok, view, _html} = live(conn, "/")
assert render_hook(view, "create-tag", %{"tag" => "new tag"})
end

defp create_person(_) do
person = Person.create_person(%{"person_id" => 0, "name" => "guest"})
%{person: person}
Expand Down