Articles
- {{#each articles as |article|}} - {{> article_briefly article=article }} + {{> search_articles }} - {{#unless @last}} -- {{/unless}} - {{/each}} +
+ {{/unless}} + {{/each}} +
diff --git a/src/main/blog/db.cljs b/src/main/blog/db.cljs index dc55aa6..2a63804 100644 --- a/src/main/blog/db.cljs +++ b/src/main/blog/db.cljs @@ -46,6 +46,24 @@ (catch js/Error err (js/console.log (ex-cause err))))) channel) +(defn search-articles-briefly + "Search all articles by titles and contents and returns via channel. It gets and returns the same channel. The content will be brief (<=60 characters)." + [client search channel] + (go + (try + (let [res (
js [(str "%" search "%")])))]
+ (>! channel (.-rows res)))
+ (catch js/Error err (js/console.log (ex-cause err)))))
+ channel)
+
(defn get-article
"Reads an article with the id and returns via channel. It gets and returns the same channel. Empty collection if not found."
[client id channel]
diff --git a/src/main/blog/handlers.cljs b/src/main/blog/handlers.cljs
index 59fe555..d00ba62 100644
--- a/src/main/blog/handlers.cljs
+++ b/src/main/blog/handlers.cljs
@@ -112,6 +112,23 @@
(res/status 500)
(res/send (ex-message err))))))))
+(defn htmx-search-articles-handler-factory
+ [client]
+ (fn [req res]
+ (let [channel (chan)
+ search (.-search (.-body req))]
+ (go
+ (try
+ (let [articles ( {:articles articles
+ :layout false}
+ (cljs.core/clj->js))))
+ (catch js/Error err
+ (res/status 500)
+ (res/send (ex-message err))))))))
+
(defn htmx-create-article-handler-factory
[client]
(fn [req res]
diff --git a/src/main/blog/server.cljs b/src/main/blog/server.cljs
index eb588a1..37db67f 100644
--- a/src/main/blog/server.cljs
+++ b/src/main/blog/server.cljs
@@ -70,6 +70,9 @@
(. app get "/admin"
(handlers/admin-panel-handler-factory client))
+ (. app post "/htmx/search/articles/"
+ (handlers/htmx-search-articles-handler-factory client))
+
(. app post "/htmx/articles/"
(handlers/htmx-create-article-handler-factory client))
diff --git a/views/index.hbs b/views/index.hbs
index 28793ea..2d348f0 100644
--- a/views/index.hbs
+++ b/views/index.hbs
@@ -1,11 +1,15 @@
Articles
- {{#each articles as |article|}}
- {{> article_briefly article=article }}
+ {{> search_articles }}
- {{#unless @last}}
-
- {{/unless}}
- {{/each}}
+
+ {{/unless}}
+ {{/each}}
+