bones73k/lib/bones73k_web/live/page_live.ex

41 lines
1.1 KiB
Elixir
Raw Normal View History

2021-02-24 07:49:39 -05:00
defmodule Bones73kWeb.PageLive do
use Bones73kWeb, :live_view
2020-09-12 18:43:17 -04:00
@impl true
2020-09-14 10:17:47 -04:00
def mount(_params, session, socket) do
socket = assign_defaults(session, socket)
2020-09-12 18:43:17 -04:00
{:ok, assign(socket, query: "", results: %{})}
end
@impl true
def handle_event("suggest", %{"q" => query}, socket) do
{:noreply, assign(socket, results: search(query), query: query)}
end
@impl true
def handle_event("search", %{"q" => query}, socket) do
case search(query) do
%{^query => vsn} ->
{:noreply, redirect(socket, external: "https://hexdocs.pm/#{query}/#{vsn}")}
_ ->
{:noreply,
socket
|> put_flash(:error, "No dependencies found matching \"#{query}\"")
|> assign(results: %{}, query: query)}
end
end
defp search(query) do
2021-02-24 07:49:39 -05:00
if not Bones73kWeb.Endpoint.config(:code_reloader) do
2020-09-12 18:43:17 -04:00
raise "action disabled when not in development"
end
for {app, desc, vsn} <- Application.started_applications(),
app = to_string(app),
String.starts_with?(app, query) and not List.starts_with?(desc, ~c"ERTS"),
into: %{},
do: {app, vsn}
end
end