From bb3726d071e675fe4c787df656b7d67e9b2cc8c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Thu, 3 Oct 2024 15:31:19 +0200 Subject: [PATCH] Article search (#16) * Cleanup CSS file * Add free text query to Directus client * Add search form to article list view * Search titles instead of contents Will prevent DDOS slighty. --- assets/app.css | 82 +++++++++---------- lib/mse25/directus.ex | 9 ++ lib/mse25_web/controllers/page_controller.ex | 11 ++- .../controllers/page_html/articles.html.heex | 15 +++- 4 files changed, 73 insertions(+), 44 deletions(-) diff --git a/assets/app.css b/assets/app.css index 11d454e..2f2ef1a 100644 --- a/assets/app.css +++ b/assets/app.css @@ -31,6 +31,45 @@ h3 { font-family: serif; } +main { + background-color: #000; + margin: 0 auto; + max-width: 40em; + box-sizing: border-box; + padding: 3em; +} + +pre { + margin: 2em 0; + background-color: #022; + overflow-y: auto; + padding: 0.33em; + box-shadow: 4px 4px 0 #333; + position: relative; + line-height: 1.2; + font-size: 0.8em; + + > button { + position: absolute; + top: 0.25em; + right: 0.25em; + } +} + +code { + font-family: "JetBrains mono", monaco, menlo, meslo, "Courier New", Courier, + monospace; +} + +section { + position: relative; + + & > h2 { + background: #000; + padding: 1em 0; + } +} + .skiplink { position: absolute; top: -5em; @@ -42,14 +81,6 @@ h3 { } } -main { - background-color: #000; - margin: 0 auto; - max-width: 40em; - box-sizing: border-box; - padding: 3em; -} - .flx { display: flex; justify-content: space-between; @@ -125,52 +156,21 @@ main { } } -pre { - margin: 2em 0; - background-color: #022; - overflow-y: auto; - padding: 0.33em; - box-shadow: 4px 4px 0 #333; - position: relative; - line-height: 1.2; - font-size: 0.8em; - - > button { - position: absolute; - top: 0.25em; - right: 0.25em; - } -} - -code { - font-family: "JetBrains mono", monaco, menlo, meslo, "Courier New", Courier, - monospace; -} - .sticky { position: sticky; top: 0; } -section { - position: relative; -} - .months { - grid-auto-flow: column; + grid-auto-flow: rows; display: grid; list-style: none; - grid-template-rows: repeat(6, 1fr); + grid-template-columns: repeat(3, 1fr); gap: 0.5em; padding-left: 0; margin: 3em 0; } -section > h2 { - background: #000; - padding: 1em 0; -} - .articles { display: grid; grid-template-columns: 1fr 1fr; diff --git a/lib/mse25/directus.ex b/lib/mse25/directus.ex index fc8fcbd..adc703f 100644 --- a/lib/mse25/directus.ex +++ b/lib/mse25/directus.ex @@ -197,6 +197,7 @@ defmodule Mse25.Directus do params |> limit?(options) |> page?(options) + |> query?(options) |> Enum.join("&") defp limit?(params, opts) do @@ -212,4 +213,12 @@ defmodule Mse25.Directus do pg -> ["page=" <> to_string(pg) | params] end end + + defp query?(params, opts) do + case opts[:query] do + nil -> params + "" -> params + pg -> ["filter[title][_icontains]=" <> to_string(pg) | params] + end + end end diff --git a/lib/mse25_web/controllers/page_controller.ex b/lib/mse25_web/controllers/page_controller.ex index b1af2d8..c179238 100644 --- a/lib/mse25_web/controllers/page_controller.ex +++ b/lib/mse25_web/controllers/page_controller.ex @@ -21,11 +21,18 @@ defmodule Mse25Web.PageController do end def articles(conn, _params) do - articles = Directus.get_articles!(limit: 9999) |> group_annually + articles = + case conn.params do + %{"q" => query_string} -> Directus.get_articles!(limit: 9999, query: query_string) + _ -> Directus.get_articles!(limit: 9999) + end + |> group_annually render(conn, :articles, page_title: "Webblogg", - articles: articles + articles: articles, + q: conn.params["q"], + nosearch?: conn.params["q"] == nil or conn.params["q"] == "" ) end diff --git a/lib/mse25_web/controllers/page_html/articles.html.heex b/lib/mse25_web/controllers/page_html/articles.html.heex index 1c7b60f..2a8dd75 100644 --- a/lib/mse25_web/controllers/page_html/articles.html.heex +++ b/lib/mse25_web/controllers/page_html/articles.html.heex @@ -9,7 +9,10 @@

Webblogg

- Inlägg skrivna sedan 2006. Gå direkt till: + Inlägg skrivna sedan 2006. + <%= if @nosearch? do %> + Gå direkt till: + <% end %>

+
+

+ <%= if @nosearch? do %> + Eller + <% end %> + : + + +

+
<%= for {year, articles} <- @articles do %>
year}>

<%= year %>