Site search (#20)
* Return results and count for Timeline search * Add search to page controller * Add search route Also, update scaffold uri on homepage * Add search view
This commit is contained in:
parent
b6164295f3
commit
54bf81b54d
7 changed files with 136 additions and 27 deletions
|
|
@ -44,6 +44,16 @@ defmodule Mse25.Timeline do
|
||||||
Task.async(fn -> Directus.get_links!(limit: 9999, query: query) end),
|
Task.async(fn -> Directus.get_links!(limit: 9999, query: query) end),
|
||||||
Task.async(fn -> Directus.get_events!(limit: 9999, query: query) end)
|
Task.async(fn -> Directus.get_events!(limit: 9999, query: query) end)
|
||||||
])
|
])
|
||||||
|
|
||||||
|
results =
|
||||||
|
items
|
||||||
|
|> List.flatten()
|
||||||
|
|> Enum.sort_by(&sort_key/1)
|
||||||
|
|> Enum.map(&categorize/1)
|
||||||
|
# |> Enum.group_by(fn item -> sort_key(item) |> String.slice(5..6) end)
|
||||||
|
|> Enum.reverse()
|
||||||
|
|
||||||
|
{:ok, %{query: query, results: results, count: length(results)}}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp sort_key(%{"pubDate" => date}), do: date
|
defp sort_key(%{"pubDate" => date}), do: date
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@
|
||||||
</a>
|
</a>
|
||||||
</h3>
|
</h3>
|
||||||
<p><%= item["lead"] %></p>
|
<p><%= item["lead"] %></p>
|
||||||
|
<%= if item["poster"] do %>
|
||||||
<img
|
<img
|
||||||
src={ "https://n.madr.se/assets/" <> item["poster"] <> "?key=poster"}
|
src={ "https://n.madr.se/assets/" <> item["poster"] <> "?key=poster"}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
|
|
@ -60,6 +61,7 @@
|
||||||
width="200"
|
width="200"
|
||||||
/>
|
/>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
<% end %>
|
||||||
<%= if t == :links do %>
|
<%= if t == :links do %>
|
||||||
<h3>
|
<h3>
|
||||||
<%= item["title"] %>
|
<%= item["title"] %>
|
||||||
|
|
@ -89,6 +91,7 @@
|
||||||
<li><%= song["artist"]["name"] <> " - " <> song["title"] %></li>
|
<li><%= song["artist"]["name"] <> " - " <> song["title"] %></li>
|
||||||
<% end %>
|
<% end %>
|
||||||
</ul>
|
</ul>
|
||||||
|
<%= if item["cover"] do %>
|
||||||
<img
|
<img
|
||||||
src={"https://n.madr.se/assets/" <> item["cover"] <> "?key=rectangular"}
|
src={"https://n.madr.se/assets/" <> item["cover"] <> "?key=rectangular"}
|
||||||
alt="Skivomslag"
|
alt="Skivomslag"
|
||||||
|
|
@ -97,6 +100,7 @@
|
||||||
width="75"
|
width="75"
|
||||||
/>
|
/>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
<% end %>
|
||||||
</article>
|
</article>
|
||||||
<% end %>
|
<% end %>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
<article>
|
|
||||||
<header>
|
|
||||||
<date><%= @published_at %></date>
|
|
||||||
<h1><%= @heading %></h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<%= raw @contents %>
|
|
||||||
|
|
||||||
<footer>
|
|
||||||
<p>Skribent: Anders Englöf Ytterström. Publicerad <%= @published_at %> och senast uppdaterad <%= @updated_at %>.</p>
|
|
||||||
</footer>
|
|
||||||
</article>
|
|
||||||
|
|
@ -2,6 +2,7 @@ defmodule Mse25Web.PageController do
|
||||||
use Mse25Web, :controller
|
use Mse25Web, :controller
|
||||||
|
|
||||||
alias Mse25.Directus
|
alias Mse25.Directus
|
||||||
|
alias Mse25.Timeline
|
||||||
|
|
||||||
def home(conn, _params) do
|
def home(conn, _params) do
|
||||||
[most_recent_article, older_article] = Directus.get_articles!(limit: 2)
|
[most_recent_article, older_article] = Directus.get_articles!(limit: 2)
|
||||||
|
|
@ -20,6 +21,30 @@ defmodule Mse25Web.PageController do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def search(conn, %{"q" => ""}) do
|
||||||
|
redirect(conn, to: ~p"/")
|
||||||
|
end
|
||||||
|
|
||||||
|
def search(conn, %{"q" => query}) do
|
||||||
|
{:ok, %{results: results, count: count}} = Timeline.search(query)
|
||||||
|
|
||||||
|
scount =
|
||||||
|
case count do
|
||||||
|
0 -> "Inga"
|
||||||
|
c -> to_string(c)
|
||||||
|
end
|
||||||
|
|
||||||
|
render(conn, :search,
|
||||||
|
q: query,
|
||||||
|
page_title: scount <> " sökresultat för \"" <> query <> "\"",
|
||||||
|
results: results
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def search(conn, _params) do
|
||||||
|
redirect(conn, to: ~p"/")
|
||||||
|
end
|
||||||
|
|
||||||
def articles(conn, params) do
|
def articles(conn, params) do
|
||||||
articles =
|
articles =
|
||||||
case params do
|
case params do
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<main class="landing">
|
<main class="landing">
|
||||||
<img src={~p"/images/aey.svg"} width="300" alt="Anders Englöf Ytterström" />
|
<img src={~p"/images/aey.svg"} width="300" alt="Anders Englöf Ytterström" />
|
||||||
<form metod="get" action="/search">
|
<form metod="get" action="/sok">
|
||||||
<label for="q">Sök innehåll</label>: <input size="9" type="search" id="q" name="q" />
|
<label for="q">Sök innehåll</label>: <input size="9" type="search" id="q" name="q" />
|
||||||
<button>Sök</button>
|
<button>Sök</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
82
lib/mse25_web/controllers/page_html/search.html.heex
Normal file
82
lib/mse25_web/controllers/page_html/search.html.heex
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
<header>
|
||||||
|
<ol class="breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">
|
||||||
|
<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
|
||||||
|
<a href="/" rel="home">
|
||||||
|
<span itemprop="name">madr.se</span>
|
||||||
|
</a>
|
||||||
|
<meta itemprop="position" content="1" />
|
||||||
|
</li>
|
||||||
|
</ol>
|
||||||
|
<form metod="get" action="/sok">
|
||||||
|
<label for="q">Sök innehåll</label>:
|
||||||
|
<input size="9" type="search" id="q" name="q" value={@q} />
|
||||||
|
<button>Sök</button>
|
||||||
|
</form>
|
||||||
|
<h1><%= @page_title %></h1>
|
||||||
|
<%= for item = %{t: t} <- @results do %>
|
||||||
|
<article>
|
||||||
|
<%= if t == :articles do %>
|
||||||
|
<h3>
|
||||||
|
<a href={"/" <> item["slug"]}>
|
||||||
|
<%= item["title"] %>
|
||||||
|
</a>
|
||||||
|
</h3>
|
||||||
|
<% end %>
|
||||||
|
<%= if t == :events do %>
|
||||||
|
<h3>
|
||||||
|
<a href={"/" <> item["slug"]}>
|
||||||
|
<%= item["title"] %>
|
||||||
|
</a>
|
||||||
|
</h3>
|
||||||
|
<p><%= item["lead"] %></p>
|
||||||
|
<%= if item["poster"] do %>
|
||||||
|
<img
|
||||||
|
src={ "https://n.madr.se/assets/" <> item["poster"] <> "?key=poster"}
|
||||||
|
loading="lazy"
|
||||||
|
alt="Affisch"
|
||||||
|
width="200"
|
||||||
|
/>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
<%= if t == :links do %>
|
||||||
|
<h3>
|
||||||
|
<%= item["title"] %>
|
||||||
|
</h3>
|
||||||
|
<p><%= raw(Earmark.as_html!(item["contents"])) %></p>
|
||||||
|
Källa:
|
||||||
|
<a href={"/" <> item["source"]}>
|
||||||
|
<%= item["h1"] %>
|
||||||
|
</a>
|
||||||
|
<% end %>
|
||||||
|
<%= if t == :albums do %>
|
||||||
|
<h3>
|
||||||
|
<%= item["artist"] <>
|
||||||
|
" - " <> item["album"] <> " (" <> to_string(item["year"]) <> ")" %>
|
||||||
|
<a
|
||||||
|
class="permalink"
|
||||||
|
href={"/" <> to_string(@year) <> "/brutal-legend-" <> item["externalId"]}
|
||||||
|
>
|
||||||
|
#
|
||||||
|
</a>
|
||||||
|
</h3>
|
||||||
|
<%= if item["contents"] do %>
|
||||||
|
<p><%= raw(Earmark.as_html!(item["contents"])) %></p>
|
||||||
|
<% end %>
|
||||||
|
<ul>
|
||||||
|
<%= for song <- item["songs"] do %>
|
||||||
|
<li><%= song["artist"]["name"] <> " - " <> song["title"] %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
<%= if item["cover"] do %>
|
||||||
|
<img
|
||||||
|
src={"https://n.madr.se/assets/" <> item["cover"] <> "?key=rectangular"}
|
||||||
|
alt="Skivomslag"
|
||||||
|
loading="lazy"
|
||||||
|
height="75"
|
||||||
|
width="75"
|
||||||
|
/>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
</article>
|
||||||
|
<% end %>
|
||||||
|
</header>
|
||||||
|
|
@ -18,10 +18,10 @@ defmodule Mse25Web.Router do
|
||||||
pipe_through :browser
|
pipe_through :browser
|
||||||
|
|
||||||
get "/", PageController, :home
|
get "/", PageController, :home
|
||||||
|
|
||||||
get "/evenemang", PageController, :events
|
get "/evenemang", PageController, :events
|
||||||
get "/webblogg", PageController, :articles
|
get "/webblogg", PageController, :articles
|
||||||
get "/delningar", PageController, :links
|
get "/delningar", PageController, :links
|
||||||
|
get "/sok", PageController, :search
|
||||||
|
|
||||||
# get "/kommande-evenemang.ics", EventController, :calendar
|
# get "/kommande-evenemang.ics", EventController, :calendar
|
||||||
# get "/event-map.js", EventController, :interactive_map
|
# get "/event-map.js", EventController, :interactive_map
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue