commit 202d663aa5f4aa6eb8e069156f6128f357051677
parent 104c2d2b296de6f813e2cf54029aeb40f73ab546
Author: Daniel Barlow <dan@telent.net>
Date: Sat, 9 Aug 2025 14:00:55 +0100
UI to sort topic pages
Diffstat:
| M | main.fnl | | | 31 | ++++++++++++++++++------------- |
1 file changed, 18 insertions(+), 13 deletions(-)
diff --git a/main.fnl b/main.fnl
@@ -35,11 +35,12 @@
[ :static { : path } ])
#(let [{ : path : query } (url.parse $2)
- (field dir) (if query.order
- (string.match query.order "(%g-)_(%g+)"))
- order (and field dir (doto {} (tset field dir)))]
+ { : order : direction } query
+ direction (. { :ascending :ascending
+ :descending :descending } direction)]
(case (string.match path "^/topic/([%w_-]+)$")
- slug [ :topic { : slug : order } ]))
+ slug [ :topic { : slug :order
+ (and order (doto {} (tset order direction))) } ]))
#(case (string.match $2 "^/([%w_]+)$")
slug [ :show { : slug }])
@@ -65,12 +66,9 @@
(expect= (route-request :get "foo/static/README" route-matchers) nil)
(expect= (route-request :get "/topic/cripslock" route-matchers)
- [ :topic {:slug "cripslock" } ])
+ [ :topic {:slug "cripslock" } ])
- (expect= (route-request :get "/topic/cripslock" route-matchers)
- [ :topic {:slug "cripslock" } ])
-
- (expect= (route-request :get "/topic/cripslock?order=date_ascending" route-matchers)
+ (expect= (route-request :get "/topic/cripslock?order=date&direction=ascending" route-matchers)
[ :topic {:slug "cripslock" :order { :date :ascending }} ])
)
@@ -253,10 +251,12 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a>
(page-footer stream)
(stream:write_chunk "" true)))
-(fn send-topic-page [stream collection slug]
+
+(fn send-topic-page [stream collection slug order]
(let [post (collection:get slug)
+ order (or order { :title :ascending })
results (collection:search {:filter {:topic slug}
- :order {:title :ascending}})
+ : order })
s (stream-from-cq-stream stream)]
(server.headers stream { })
(page-header stream (.. "Topic: " slug))
@@ -267,6 +267,11 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a>
(to-html
[:div
[:h2 "In this topic"]
+ [[:div {:style "text-align: right"}]
+ "sort by "
+ [:span [[:a {:href "?order=date&direction=descending"}] "date"]]
+ " | "
+ [:span [[:a {:href "?order=title&direction=ascending"}] "title"]]]
(prepend :ul
(icollect [_ p (ipairs results)]
[:li [[:a { :href p.url-path}] p.title]]))]
@@ -312,8 +317,8 @@ href=\"https://axillae.telent.net/\">@dan@axillae.telent.net</a>
(stream:write_body_from_file f)))
[:home { : limit }]
(send-home-page stream collection)
- [:topic { : slug }]
- (send-topic-page stream collection slug)
+ [:topic { : slug &as p }]
+ (send-topic-page stream collection slug p.order)
))