add basic autocomplete

pull/1696/head
Ayrat Badykov 6 years ago
parent 5ef1e40b8f
commit 8adcd8a92f
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 20
      apps/block_scout_web/lib/block_scout_web/controllers/chain_controller.ex
  2. 4
      apps/block_scout_web/lib/block_scout_web/csp_header.ex
  3. 27
      apps/block_scout_web/lib/block_scout_web/templates/layout/_topnav.html.eex
  4. 2
      apps/explorer/lib/explorer/chain.ex
  5. 2
      apps/explorer/priv/repo/migrations/20190403080447_add_full_text_search_tokens.exs

@ -39,13 +39,21 @@ defmodule BlockScoutWeb.ChainController do
end
end
def token_autocomplete(conn, %{"q" => term}) do
result =
term
|> String.trim()
|> Chain.search_token()
def token_autocomplete(conn, %{"q" => term}) when is_binary(term) do
if term == "" do
json(conn, "{}")
else
result =
term
|> String.trim()
|> Chain.search_token()
json(conn, result)
end
end
json(conn, result)
def token_autocomplete(conn, _) do
json(conn, "{}")
end
def chain_blocks(conn, _params) do

@ -13,8 +13,8 @@ defmodule BlockScoutWeb.CSPHeader do
"content-security-policy" => "\
connect-src 'self' #{websocket_endpoints(conn)}; \
default-src 'self';\
script-src 'self' 'unsafe-inline' 'unsafe-eval';\
style-src 'self' 'unsafe-inline' 'unsafe-eval' https://fonts.googleapis.com;\
script-src 'self' 'unsafe-inline' 'unsafe-eval' http://nico-amsterdam.github.io;\
style-src 'self' 'unsafe-inline' 'unsafe-eval' https://fonts.googleapis.com http://nico-amsterdam.github.io;\
img-src 'self' 'unsafe-inline' 'unsafe-eval' data:;\
font-src 'self' 'unsafe-inline' 'unsafe-eval' https://fonts.gstatic.com data:;\
"

@ -78,14 +78,27 @@
</li>
</ul>
<div class="search-form d-lg-flex d-inline-block">
<%= form_for @conn, chain_path(@conn, :search), [class: "form-inline my-2 my-lg-0", method: :get, enforce_utf8: false], fn f -> %>
<%= form_for @conn, chain_path(@conn, :search), [class: "form-inline my-2 my-lg-0", method: :get, enforce_utf8: false], fn f -> %> <!-- </div> -->
<div class="input-group">
<%= search_input f, :q, class: 'form-control mr-auto', placeholder: gettext("Search by address, token symbol name, transaction hash, or block number"), "aria-describedby": "search-icon", "aria-label": gettext("Search"), "data-test": "search_input" %>
<div class="input-group-append">
<button class="input-group-text" id="search-icon">
<%= render BlockScoutWeb.IconsView, "_search_icon.html" %>
</button>
</div>
<%= awesomplete(f, :q,
[
class: "form-control me auto",
placeholder: gettext("Search by address, token symbol name, transaction hash, or block number"),
"aria-describedby": "search-icon",
"aria-label": gettext("Search"),
"data-test": "search_input"
],
[ url: "#{chain_path(@conn, :token_autocomplete)}?q=",
prepop: true,
minChars: 3,
maxItems: 8,
value: "name"
]) %>
<div class="input-group-append">
<button class="input-group-text" id="search-icon">
<%= render BlockScoutWeb.IconsView, "_search_icon.html" %>
</button>
</div>
</div>
<button class="btn btn-outline-success my-2 my-sm-0 sr-only" type="submit"><%= gettext "Search" %></button>
<% end %>

@ -707,7 +707,7 @@ defmodule Explorer.Chain do
query =
from(token in Token,
where: fragment("to_tsvector('english', name || ' ' || symbol) @@ to_tsquery(?)", ^term),
where: fragment("to_tsvector('english', symbol || ' ' || name ) @@ to_tsquery(?)", ^term),
limit: 5
)

@ -5,7 +5,7 @@ defmodule Explorer.Repo.Migrations.AddFullTextSearchTokens do
execute("CREATE EXTENSION pg_trgm")
execute("""
CREATE INDEX tokens_trgm_idx ON tokens USING GIN (to_tsvector('english', name || ' ' || symbol))
CREATE INDEX tokens_trgm_idx ON tokens USING GIN (to_tsvector('english', symbol || ' ' || name))
""")
end

Loading…
Cancel
Save