Browse Source

feat: search for tags, closes #51

main
Inhji Y. 4 months ago
parent
commit
bda29e6bf5
  1. 14
      lib/mirage/search.ex
  2. 4
      lib/mirage_web/live/search_live/index.html.leex
  3. 31
      lib/mirage_web/live/search_live/search_result_component.ex

14
lib/mirage/search.ex

@ -22,6 +22,17 @@ defmodule Mirage.Search do
def search_notes(search_term) do
downcased_term = String.downcase(search_term)
topic_query =
from t in Mirage.Topics.Topic,
where: contains(t.text, ^downcased_term),
select: %{
type: "tag",
id: t.id,
title: t.text,
content: "",
url: ""
}
link_query =
from l in Mirage.Links.Link,
where: contains(l.title, ^downcased_term),
@ -33,7 +44,8 @@ defmodule Mirage.Search do
title: l.title,
content: l.content,
url: l.url
}
},
union: ^topic_query
note_query =
from n in Mirage.Notes.Note,

4
lib/mirage_web/live/search_live/index.html.leex

@ -23,6 +23,10 @@
<%= if result.type == "link" do %>
<%= live_component @socket, MirageWeb.SearchLive.SearchResultComponent, result: result, url: Routes.link_show_path(@socket, :show, result.id) %>
<% end %>
<%= if result.type == "tag" do %>
<%= live_component @socket, MirageWeb.SearchLive.SearchResultComponent, result: result, url: Routes.topic_path(@socket, :show, result.id) %>
<% end %>
<% end %>
<% end %>
</div>

31
lib/mirage_web/live/search_live/search_result_component.ex

@ -1,14 +1,33 @@
defmodule MirageWeb.SearchLive.SearchResultComponent do
use MirageWeb, :live_component
def icon(type) do
case type do
"note" -> "bi-journal-text"
"link" -> "bi-bookmark-star"
"tag" -> "bi-tag"
_ -> ""
end
end
def render(assigns) do
~L"""
<article>
<%= live_redirect to: @url do %>
<h2><strong><%= @result.title || @result.url %></strong></h2>
<% end %>
<p><%= raw @result.found_value %></p>
<footer>Found in <strong><%= @result.found_prop %></strong> <%= inspect(@result.occurrence) %></footer>
<article>
<h2>
<strong>
<%= live_redirect to: @url do %>
<%= @result.title || @result.url %>
<% end %>
</strong>
</h2>
<p>
<%= raw @result.found_value %>
</p>
<footer>
<i class="bi <%= icon(@result.type) %>"></i> * Found in <strong><%= @result.found_prop %></strong> * <%= inspect(@result.occurrence) %>
</footer>
</article>
"""
end

Loading…
Cancel
Save