Skip to content
Permalink
Branch: master
Find file Copy path
1 contributor

Users who have contributed to this file

38 lines (32 sloc) 1.31 KB
defmodule DemoWeb.SearchLive do
use Phoenix.LiveView
def render(assigns) do
~L"""
<form phx-change="suggest" phx-submit="search">
<input type="text" name="q" value="<%= @query %>" list="matches" placeholder="Search..."
<%= if @loading, do: "readonly" %>/>
<datalist id="matches">
<%= for match <- @matches do %>
<option value="<%= match %>"><%= match %></option>
<% end %>
</datalist>
<%= if @result do %><pre><%= @result %></pre><% end %>
</form>
"""
end
def mount(_session, socket) do
{:ok, assign(socket, query: nil, result: nil, loading: false, matches: [])}
end
def handle_event("suggest", %{"q" => query}, socket) when byte_size(query) <= 100 do
{words, _} = System.cmd("grep", ~w"^#{query}.* -m 5 /usr/share/dict/words")
{:noreply, assign(socket, matches: String.split(words, "\n"))}
end
def handle_event("search", %{"q" => query}, socket) when byte_size(query) <= 100 do
send(self(), {:search, query})
{:noreply, assign(socket, query: query, result: "Searching...", loading: true, matches: [])}
end
def handle_info({:search, query}, socket) do
{result, _} = System.cmd("dict", ["#{query}"], stderr_to_stdout: true)
{:noreply, assign(socket, loading: false, result: result, matches: [])}
end
end
You can’t perform that action at this time.